Populating products from opportunity to quote
Hi!
I have created a ribbon button to create quote from opportunity after adding products. I want to auto populate products using plug in.
Here's the which I have used
using System;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
namespace MyPlugin
{
public class ProdPlugin : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
//Extract the tracing service for use in debugging sandboxed plug-ins.
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
// Obtain the execution context from the service provider.
IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));
// Obtain the organization service reference.
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
// The InputParameters collection contains all the data passed in the message request.
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity entity = (Entity)context.InputParameters["Target"];
if (entity.LogicalName == "quote")
{
if (context.PostEntityImages.Contains("PostImage"))
{
// Here is your Quote entity data
Entity quote = (Entity)context.PostEntityImages["PostImage"];
try
{
QueryExpression query = new QueryExpression
{
EntityName = "quotedeatailsGrid",
ColumnSet = new ColumnSet(),
Criteria = new FilterExpression
{
Conditions =
{
new ConditionExpression
{
AttributeName = "products",
Operator = ConditionOperator.Equal,
Values = { (Guid)entity.Id }
}
}
}
};
// Here is your Quote Product(s) data
EntityCollection prodList = service.RetrieveMultiple(query);
if (prodList.Entities.Count > 0)
{
foreach (Entity quoteProd in prodList.Entities)
{
var quoteprodId = quoteProd.Id;
//etc
}
}
if (quote.Attributes.Contains("opportunityid"))
{
// Here is your Opportunity entity data
Entity opportunity =
service.Retrieve("opportunity", ((EntityReference)quote["opportunityid"]).Id, new ColumnSet(true));
}
}
catch (Exception ex)
{
}
}
}
}
}
}
}
But I am getting error when I am excuting.
Can someone please help by telling where the code is wrong as I am new to coding.
Thank you!