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);
}
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With