Skip to main content

Notifications

Customer experience | Sales, Customer Insights,...
Unanswered

Getting error while generate Invoice from Opportunity when Closing as Won

Posted on by 1,558

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>

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans