Hello,
I am trying to write my first plug-in for CRM 2015. The basic idea is to qualify a lead without creating a contact. I am using the SDK example "WorkingWithLeads" but when I get to the IOrganizationService.execute it restarts my plug in. I feel like I am missing some basic knowledge on how to submit/close out my plug in. Below is my code.
using System;
using System.ServiceModel;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Query;
using Plugins.ProxyClasses;
using Plugins.SDKClasses;
public class LeadQualify : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
//throw new InvalidPluginExecutionException()
//{
// //trest
//};
// Extract the tracing service for use in debugging sandboxed plug-ins.
// If you are not registering the plug-in in the sandbox, then you do
// not have to add any tracing service related code.
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
// Obtain the execution context from the service provider.
IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));
// The InputParameters collection contains all the data passed in the message request.
if (context.InputParameters.Contains("LeadId") &&
context.InputParameters["LeadId"] is EntityReference)
{
// Obtain the target entity from the input parameters.
EntityReference entityRef = (EntityReference)context.InputParameters["LeadId"];
// Verify that the target entity represents an entity type you are expecting.
// For example, an account. If not, the plug-in was not registered correctly.
if (entityRef.LogicalName != "lead")
return;
// 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
{
// Plug-in business logic goes here.
//QUalifyLead
var qualifyIntoAccountContactReq = new QualifyLeadRequest
{
CreateAccount = true,
CreateContact = false,
CreateOpportunity = true,
LeadId = new EntityReference(entityRef.LogicalName, entityRef.Id),
Status = new OptionSetValue((int)lead_statuscode.Qualified)
};
var qualifyIntoAccountContactRes = (QualifyLeadResponse)service.Execute(qualifyIntoAccountContactReq);
}
catch (FaultException<OrganizationServiceFault> ex)
{
throw new InvalidPluginExecutionException("An error occurred in LeadQualify.", ex);
}
catch (Exception ex)
{
tracingService.Trace("MyPlugin: {0}", ex.ToString());
throw;
}
}
}
}
*This post is locked for comments
I have the same question (0)