If have rewritten your code to make sure I'll understand.
using System;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Messages;
using System.Collections.Generic;
using System.Linq;
namespace MGTest.Plugins
{
public class QuoteWin : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
tracingService.Trace("Tracing: EXECUTE");
if (context.InputParameters.Contains("QuoteClose") && context.InputParameters["QuoteClose"] is Entity)
{
var entity = (Entity)context.InputParameters["QuoteClose"];
if (entity.LogicalName != "quoteclose")
return;
//throw new InvalidPluginExecutionException("Check");
// Obtain the organization service reference which you will need for
// web service calls.
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
try
{
var quoteid = ((EntityReference)entity["quoteid"]).Id;
var quoteDetails = RetrieveQuoteDetails(service, quoteid);
if (quoteDetails != null)
{
foreach (var qd in quoteDetails)
{
var project = new Entity("msdyn_project");
if (qd.Contains("productdescription"))
{
project.Attributes.Add("msdyn_subject", qd.GetAttributeValue<string>("productdescription"));
}
Guid projectId = service.Create(project);
UpdateQuoteDetail(service, qd.Id, projectId);
}
}
}
catch (FaultException<OrganizationServiceFault> ex)
{
throw new InvalidPluginExecutionException(ex.Message);
//throw new InvalidPluginExecutionException("An error occurred in MyPlug-in.", ex);
}
catch (Exception ex)
{
tracingService.Trace("MyPlugin: {0}", ex.ToString());
throw;
}
}
}
private List<Entity> RetrieveQuoteDetails(IOrganizationService service, Guid quoteId)
{
var query = new QueryExpression("quotedetail");
query.ColumnSet.AddColumn("productdescription");
query.Criteria.AddCondition("quoteid", ConditionOperator.Equal, quoteId);
var result = service.RetrieveMultiple(query);
if (result != null && result.Entities != null && result.Entities.Any())
{
return result.Entities.ToList();
}
return null;
}
private void UpdateQuoteDetail(IOrganizationService service, Guid quoteDetailId, Guid projectId)
{
var quoteDetail = new Entity("quotedetail", quoteDetailId);
quoteDetail.Attributes.Add("msdyn_project",new EntityReference("msdyn_project", projectId));
service.Update(quoteDetail);
}
}
}
But I cannot trigger the win event on Quote. If I press Create order on quote It should be executing my plugin.