Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I need to set the State and StatusCode of a custom entity

I need to set the State and StatusCode of a custom entity using the .Net CRM SDK.
The following code executes but the StatusCode isn't changed when I check on 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);
}
like image 814
Lets Stack It Avatar asked Dec 12 '22 22:12

Lets Stack It


1 Answers

You may try the following code, I use this code to set 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 status like this:

int statusCode = 123456;
entity["statuscode"] = new OptionSetValue(statusCode);
_service.Update(entity);
like image 116
nixjojo Avatar answered Dec 31 '22 13:12

nixjojo