Quantcast
Channel: Microsoft Dynamics CRM Forum - Recent Threads
Viewing all articles
Browse latest Browse all 46379

How to copy notes from lead to opportunity using plugin?

$
0
0

Hi,

I want to copy notes from lead to opportunity using plugin code.

I apply this code:

protected override void ExecuteCrmPlugin(LocalPluginContext serviceProvider)
{
if (serviceProvider == null)
{
throw new InvalidPluginExecutionException("serviceProvider");
}

// TODO: Implement your custom Plug-in business logic.
//Get the Plugin Contex from service provider
IPluginExecutionContext context = serviceProvider.PluginExecutionContext;

//Check weather the "Traget" exists in the inpu parameter
if (context.InputParameters.Contains("Target") == false) return;

//Check weather the Input Parameter is a entity or not
if (context.InputParameters["Target"] is Entity == false) return;

//Get service and service factory from service provider
IOrganizationService service = serviceProvider.OrganizationService;

//Extract the tracing service for use in debugging and to do tracing
ITracingService tracingService = serviceProvider.TracingService;

//Check weather the Target entity have the originating lead filed or not
Entity selectedEntity = (Entity)context.InputParameters["Target"];


//Here my code is not working,selectedEntity does not find "originatingleadid",please help me how to solve this.
if (selectedEntity.Attributes.Contains("originatingleadid") == false) return;


EntityReference entLead = (EntityReference)selectedEntity.Attributes["originatingleadid"];
Guid LeadGuid = entLead.Id;
try
{
if (context.MessageName == "Create")
{
#region Get Originatimg Lead Notes and attachment details from Lead Entity
string strFetchNotes = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='annotation'>
<attribute name='subject' />
<attribute name='notetext' />
<attribute name='documentbody' />
<attribute name='filename' />
<attribute name='annotationid' />
<order attribute='subject' descending='false' />
<link-entity name='lead' from='leadid' to='objectid' alias='aa'>
<filter type='and'>
<condition attribute='leadid' operator='eq' value='{0}' />
</filter>
</link-entity>
</entity>
</fetch>";
#endregion
strFetchNotes = string.Format(strFetchNotes, LeadGuid);
EntityCollection entNotes = (EntityCollection)service.RetrieveMultiple(new FetchExpression(strFetchNotes));
if (entNotes != null && entNotes.Entities.Count > 0)
{
for (int i = 0; i < entNotes.Entities.Count; i++)
{
Entity entNote = (Entity)entNotes.Entities[i];

#region Creating new Notes record from Existing Notes Record

//array to remove from notes record
string[] strAttributesNotestoRemove = new string[] { "createdon", "createdby", "modifiedon", "modifiedby", "annotationid", "objecttypecode", "objectid" };
//Clone new notes object from existing notes record
Entity entNewAnnotation = CloneRecordForEntity("annotation", entNote, strAttributesNotestoRemove);
//Add object id to attach this new notes to Invoice sponsor item record
entNewAnnotation["objectid"] = new EntityReference(context.PrimaryEntityName, context.PrimaryEntityId);
entNewAnnotation["objecttypecode"] = context.PrimaryEntityName;
service.Create(entNewAnnotation);

#endregion
}//End for notes count
}//End entities count

}//End of create contex
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException(
String.Format("An error occurred in the {0} plug-in.", this.GetType().ToString()), ex);
}

}

Thanks in advance.


Viewing all articles
Browse latest Browse all 46379

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>