We have CRM 2011, on-premises. In a WCF Service (C#) I am programmatically creating Contracts and ContractDetails. After creating the Contract, I set its State = 'Invoiced' using this code:
try
{
SetStateRequest setStateRequest = new SetStateRequest()
{
EntityMoniker = new EntityReference
{
Id = gNewContractId,
LogicalName = Xrm.Contract.EntityLogicalName
},
State = new OptionSetValue((int)Xrm.ContractState.Invoiced),
Status = new OptionSetValue((int)Xrm.ContractState.Invoiced + 1)
};
_service.Execute(setStateRequest);
}
This process used to work but sometimes now I get this error, as I did today:
"System.ServiceModel.FaultException`1[Microsoft.Xrm.Sdk.OrganizationServiceFault]: The state is invalid, this contract cannot be set to invoice state. (Fault Detail is equal to Microsoft.Xrm.Sdk.OrganizationServiceFault)."
The State of the contract when this snippet was executed (and which failed today) was 'Draft'.
This Contract has child Contract Details, and its ActiveOn date is 6/1/2015, so it should have been made Active - and it was. So I am not understanding the error or what I need to do to prevent it.
Thanks for all help and advice.
First this line of your code
Should be something like
or the name for the Status Reason enum that was generated, this because you are writing 1+1, so it reduce the function of the early bound.
Regarding the Contract, looks like that the SetStateRequest acts differently based on the Start and End Dates of the Contract. If the range falls inside current date, the request set the Contract to Active, if the range falls outside current date, the contract is invoiced. Please check your contract dates and see if this was the case.