I need to set State and StatusCode for a custom object

I need to set State and StatusCode for a custom object using the .NET CRM SDK .
The following code executes, but the StatusCode does not change when I check the entity form.

private void SetState(Entity entity, int statuscode)
{
  SetStateRequest setState = new SetStateRequest
  {
    EntityMoniker = new EntityReference(
      entity.LogicalName, new Guid(entity.Id.ToString())),
    State = new OptionSetValue(0),
    Status = new OptionSetValue(statuscode)
  };
  SetStateResponse myres = (SetStateResponse)svc.Execute(setState);
}
+3
source share
1 answer

You can try the following code, I use this code to set the state.

Microsoft.Xrm.Sdk.EntityReference moniker = new EntityReference();
moniker.LogicalName = "contract";
moniker.Id = newContractId;

Microsoft.Xrm.Sdk.OrganizationRequest request 
  = new Microsoft.Xrm.Sdk.OrganizationRequest() { RequestName = "SetState" };
request["EntityMoniker"] = moniker;
OptionSetValue state = new OptionSetValue(1);
OptionSetValue status = new OptionSetValue(2);
request["State"] = state;
request["Status"] = status;

_service.Execute(request);

Or you can set this status:

int statusCode = 123456;
entity["statuscode"] = new OptionSetValue(statusCode);
_service.Update(entity);
+8
source

Source: https://habr.com/ru/post/1535047/


All Articles