When user converts lead to contact - all attachements and notes must be copied to retrieved contact.
Code of the plugin:
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.SdkTypeProxy;
using Microsoft.Crm.Sdk.Query;
namespace LeadConversionPlugin
{
public class ContactCreationHandler : IPlugin
{
#region IPlugin Members
public void Execute(IPluginExecutionContext context)
{
if (context.MessageName == MessageName.Create &&
context.InputParameters.Contains("Target") &&
context.InputParameters["Target"] is DynamicEntity)
{
DynamicEntity entity = (DynamicEntity)context.InputParameters["Target"];
//Check that target entity is contact
if (entity.Name != EntityName.contact.ToString())
return;
//Check that this contact is created in the way of
//convertion from lead
if (!entity.Properties.Contains("originatingleadid"))
return;
//Source Lead Identifier
Guid leadid = ((Lookup)entity["originatingleadid"]).Value;
//Target Contact Identifier
Guid contactid = (Guid)context.OutputParameters["Id"];
ICrmService crmservice = context.CreateCrmService(true);
//Just build the query which will be used to retrieve
//all child annotations (notes and attachements)
QueryByAttribute query = new QueryByAttribute();
query.ColumnSet = new AllColumns();
query.EntityName = EntityName.annotation.ToString();
query.Attributes = new string[] { "objectid" };
query.Values = new object[] { leadid };
RetrieveMultipleRequest request = new RetrieveMultipleRequest();
request.Query = query;
request.ReturnDynamicEntities = false;
RetrieveMultipleResponse response = (RetrieveMultipleResponse)crmservice.Execute(request);
foreach (annotation note in response.BusinessEntityCollection.BusinessEntities)
{
//Just remove Key field of annotation
note.annotationid = null;
//Replace referencing object lookup with newly created contact lookup
note.objectid = new Lookup(EntityName.contact.ToString(), contactid);
note.objecttypecode = new EntityNameReference(EntityName.contact.ToString());
//And Create the annotation record
crmservice.Create(note);
}
}
}
#endregion IPlugin Members
}
}
Step for this plugin must be registered as a Post Create Syncronous Parent Pipeline.
*This post is locked for comments