I have a plugin registered on quote that when a quote is re-set to draft, any previously created orders will be cancelled.
My problem is the plugin is failing on the cancellation of the orders.
The relevant portion of the code is shown below.
I basically pass it the entity collection which contains all the orders that need to be updated with statecode 2.
The service.update portion is failing.
An unexpected error occurred from ISV code.
(ErrorType = ClientError) Unexpected exception from plug-in (Execute):
ACEQuoteCleanUp.CleanOrdersOnClosedQuotes.PostOperationquoteUpdate:
System.Exception: Error in top level UPDATE at TRY: Error in IF/TRY Failed to Update Entities:
Error converting attribute value to Property: Attribute [statuscode],
Attribute type [status] of Entity [salesorder]
with value of type [System.Int32]: [System.InvalidCastException:
Unable to cast object of type 'System.Int32' to type 'Microsoft.Xrm.Sdk.OptionSetValue'.
at Microsoft.Crm.BusinessEntities.StatusPropertyConverter.InternalFromEntity(ICrmConversionContext
conversionContext, AttributeMetadata attributeMetadata,
Object value, String formattedValue) at Microsoft.Crm.BusinessEntities.PropertyConverterBase.<>c__DisplayClass7_0.b__0()
at Microsoft.Crm.BusinessEntities.PropertyConverterBase.InternalConvertFrom[T](Guid orgId,
AttributeMetadata attributeMetadata, Object value, F
Any help or guidance would be greatly appreciated.
protected void Update_ReturnedOrders(IPluginExecutionContext ContextReturnedOrders, IOrganizationService OrgSvcReturnedOrders, ITracingService TracingSvc, LocalPluginContext ReturnedOrdersContext, EntityCollection ReturnedOrdersEntityCollection, string StateCodeFieldNameToUpdate, int NewVal)
{
ContextReturnedOrders = ReturnedOrdersContext.PluginExecutionContext;
OrgSvcReturnedOrders = ReturnedOrdersContext.OrganizationService;
TracingSvc = ReturnedOrdersContext.TracingService;
TracingSvc.Trace("StateCodeFieldNameToUpdate: " StateCodeFieldNameToUpdate);
TracingSvc.Trace("Int Value of OptionSet: " NewVal);
if (ContextReturnedOrders.InputParameters.Contains("Target") && ContextReturnedOrders.InputParameters["Target"] is Entity)
{
TracingSvc.Trace("Entered Final If Statement.");
try
{
TracingSvc.Trace("Entered Final Try Statement.");
foreach (Entity e in ReturnedOrdersEntityCollection.Entities)
{
TracingSvc.Trace("Tracelog Entry Immediately Pre-Ceeding The Update");
e[StateCodeFieldNameToUpdate] = (int)(NewVal);
OrgSvcReturnedOrders.Update(e);
}
If I change the code from this:
e[StateCodeFieldNameToUpdate] = (int)(NewVal);
to this:
e[StateCodeFieldNameToUpdate] = new OptionSetValue(NewVal);
I get this:
An unexpected error occurred from ISV code.
(ErrorType = ClientError) Unexpected exception from plug-in (Execute):
ACEQuoteCleanUp.CleanOrdersOnClosedQuotes.PostOperationquoteUpdate:
System.Exception: Error in top level UPDATE at TRY:
Error in IF/TRY Failed to Update Entities:
Error converting attribute value to Property:
Attribute [quoteid], Attribute type [lookup] of Entity [salesorder] with value of type [Microsoft.Xrm.Sdk.OptionSetValue]:
[System.InvalidCastException: Unable to cast object of type 'Microsoft.Xrm.Sdk.OptionSetValue' to type
'Microsoft.Xrm.Sdk.EntityReference'. at Microsoft.Crm.BusinessEntities.LookupPropertyConverter.InternalFromEntity(ICrmConversionContext conversionContext,
AttributeMetadata attributeMetadata,
Object value, String formattedValue) at Microsoft.Crm.BusinessEntities.PropertyConverterBase.<>c__DisplayClass7_0.b__0()
at Microsoft.Crm.BusinessEntities.PropertyConverterBase.InternalConvertFrom[T]
(Guid orgId, AttributeMetadata attributeMetadata, Object value, Func`1 converterFunction)]
Based on the content of the error messages, it appears the following code is the actual problem.
e[StateCodeFieldNameToUpdate]
What do I need to do to get this working?
Any advice or recommendations would be greatly appreciated.