I had created a charge entity. I was creating charge entities of certain values when certain things had happened on regarding cases. When a case was created it created a charge entity linked to the case, account and product etc for a certain value.
Then when other status were reached a workflow would fire off and create more charge entity’s. Then at a certain point the user wanted to create an invoice for all the charge entities which had not been assigned to an invoice.
So I created a plugin which when a new invoice is created it goes off and searches for all the charge entities which are not been associated with an invoice. It then assigns the newly created invoice, adds a date and then I wanted to make the charge entity inactive. I created a new inactive state called Invoiced. The reason I wanted to do this was because I didn’t want the charge entity being associated with any other invoices and you can’t change inactive records.
updating the charge entity was no problem, I did this by doing a service.update(entity) but I got errors if I tried to change the state of the entity. I recalled the state being some what different.
To change the state and status you have to a setStateRequest, it took me a while to work this out.
Another unusualish thing is the state of active is 0 and inactive is 1. I’m not sure that’s unusual or not.
The status is the status value you want, I had to look up my new status and then assign the value. The reason you use numbers is because the state and status are optionSet which are set using numbers. My new status had the value of 951850001 and the default of inactive I think has the value of -1.
Here is the code
public void updateChargeState(Guid chargeId)
SetStateRequest setState = new SetStateRequest();
setState.EntityMoniker = new EntityReference();
setState.EntityMoniker.Id = chargeId;
setState.EntityMoniker.Name = "Charge";
setState.EntityMoniker.LogicalName = new_charge.EntityLogicalName;
setState.State = new OptionSetValue(1);
setState.Status = new OptionSetValue(951850001);
SetStateResponse setStateResponse = (SetStateResponse)service.Execute(setState);