Hello All,
I am trying to create an invoice from the opportunity while it will be WON, can anyone please help me to understand what's wrong with the plugin code?
Plugin Code - Register in WON - message/Opportunity - Entity :
public void Execute(IServiceProvider serviceProvider)
{
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
// Obtain the execution context from the service provider.
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
tracingService.Trace("Out");
Guid opportunityId = Guid.Empty;
if (context.InputParameters.Contains("OpportunityClose") && context.InputParameters["OpportunityClose"] is Entity)
{
Entity entity = (Entity)context.InputParameters["OpportunityClose"];
if (entity.Attributes.Contains("opportunityid") && entity.Attributes["opportunityid"] != null)
{
EntityReference entityRef = (EntityReference)entity.Attributes["opportunityid"];
if (entityRef.LogicalName == "opportunity")
{
Guid OppID = entityRef.Id;
tracingService.Trace("id "+ OppID.ToString());
// Prepare Generate Invoice from Opportunity Request
GenerateInvoiceFromOpportunityRequest generateInvoiceFromOpportunityRequest = new GenerateInvoiceFromOpportunityRequest();
generateInvoiceFromOpportunityRequest.OpportunityId = OppID;
generateInvoiceFromOpportunityRequest.ColumnSet = new Microsoft.Xrm.Sdk.Query.ColumnSet("name", "customerid");
tracingService.Trace("Run before generate invoice " );
// Generate the Invoice
GenerateInvoiceFromOpportunityResponse generateInvoiceFromOpportunityResponse = (GenerateInvoiceFromOpportunityResponse)service.Execute(generateInvoiceFromOpportunityRequest);
}
}
}
}
Getting below error -
Exception type: System.ServiceModel.FaultException`1[Microsoft.Xrm.Sdk.OrganizationServiceFault]
Message: The given key was not present in the dictionary.Detail:
<OrganizationServiceFault xmlns:i="">www.w3.org/.../XMLSchema-instance" xmlns="">schemas.microsoft.com/.../Contracts">
<ActivityId>469d75db-5481-412e-b1e5-0e231b72bd2b</ActivityId>
<ErrorCode>-2147220956</ErrorCode>
<ErrorDetails xmlns:d2p1="">schemas.datacontract.org/.../System.Collections.Generic">
<KeyValuePairOfstringanyType>
<d2p1:key>StepErrorDetails</d2p1:key>
<d2p1:value xmlns:d4p1="">www.w3.org/.../XMLSchema" i:type="d4p1:string">[{"id":"CREATE_QOI","status":3},{"id":"CREATE_QOI_PRODUCTS","status":0},{"id":"TRIGGER_PRICE_CALCULATIONS","status":0},{"id":"COPY_CONTACTS","status":0},{"id":"COPY_SHAREPOINT_DOCS","status":0}]</d2p1:value>
</KeyValuePairOfstringanyType>
</ErrorDetails>
<HelpLink>go.microsoft.com/.../
<Message>The given key was not present in the dictionary.</Message>
<Timestamp>2023-01-04T21:18:33</Timestamp>
<ExceptionRetriable>false</ExceptionRetriable>
<ExceptionSource>PluginExecution</ExceptionSource>
<InnerFault i:nil="true" />
<OriginalException>PluginExecution</OriginalException>
<TraceText i:nil="true" />
</OrganizationServiceFault>