Appendig Lead Notes to Oppourtunity Notes

Appending Lead Notes to Opportunity Notes :-

// Code

using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using Microsoft.Crm.Sdk;
 using Microsoft.Xrm.Sdk;
 using Microsoft.Xrm.Sdk.Query;

namespace AppendingNotestoOpportunity
 public class Class1 : IPlugin

public void Execute(IServiceProvider serviceProvider)
 Guid leadid;

//IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
 //IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IPluginExecutionContext));
 //IOrganizationService service = factory.CreateOrganizationService(context.UserId);
 //ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

IExecutionContext context = (IExecutionContext)serviceProvider.GetService(typeof(IExecutionContext));

ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

IOrganizationService service = factory.CreateOrganizationService(context.UserId);


if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
 Entity entity = (Entity)context.InputParameters["Target"];
 if (entity.LogicalName == "opportunity")

tracingService.Trace("lead has name");
 // leadid = ((EntityReference)entity.Attributes["originatingleadid"]).Id;
 //tracingService.Trace("traceid:{0}", leadid);
 Entity opp = service.Retrieve(context.PrimaryEntityName, context.PrimaryEntityId, new ColumnSet(true));
 leadid = ((EntityReference)opp.Attributes["originatingleadid"]).Id;
 Entity lead = service.Retrieve("lead", leadid, new ColumnSet(true));

// = service.RetrieveMultiple(new FetchExpression(string.Format(Resource1.LineItems, new Guid("2F9C9212-04BE-E611-8103-C4346BDD8041"))));

EntityCollection notes = service.RetrieveMultiple(new FetchExpression(string.Format(Resource1.String1, leadid)));

foreach (var note in notes.Entities)
 Entity Annotation = new Entity("annotation");
 Entity leadnote = new Entity("annotation");
 leadnote = service.Retrieve("annotation", note.Id, new ColumnSet(true));
 Annotation.Attributes["objectid"] = new EntityReference(context.PrimaryEntityName, context.PrimaryEntityId);
 Annotation.Attributes["objecttypecode"] = context.PrimaryEntityName;
 if (leadnote.Attributes.Contains("subject"))
 Annotation.Attributes["subject"] = leadnote.Attributes["subject"].ToString();
 // Annotation.Attributes["body"] = encodedData;
 Annotation.Attributes["mimetype"] = @"text/plain";
 if (leadnote.Attributes.Contains("notetext"))
 Annotation.Attributes["notetext"] = leadnote.Attributes["notetext"];
 if (leadnote.Attributes.Contains("filename"))
 Annotation.Attributes["filename"] = leadnote.Attributes["filename"].ToString();
 if (leadnote.Attributes.Contains("documentbody") && leadnote.Attributes["documentbody"] != null)
 Annotation.Attributes["documentbody"] = leadnote.Attributes["documentbody"].ToString();
 //leadnote.Attributes = note.Attributes;
 //leadnote.Attributes["annotationid"] = new Guid();
 //leadnote.Attributes["objectid"] = new EntityReference(context.PrimaryEntityName, context.PrimaryEntityId);
 //leadnote.Attributes["objecttypecode"] = context.PrimaryEntityName;
 Annotation.Attributes["ownerid"] = (EntityReference)leadnote.Attributes["ownerid"];
 Guid rec = service.Create(Annotation);
 tracingService.Trace("recordid:{0}", rec);

 catch (Exception ex)
 throw new InvalidPluginExecutionException(ex.Message);

Calling Actions from Plugin in CRM 2016

Calling Actions from Plugin in CRM 2016:

Interesting features Updated  in CRM  is Actions. Actions are the Custom messages which can be created for any entity or globally to perform business operations like create/update/Assign etc. Looking on  first time we get a feeling that it is very similar to Workflow but hold on it’s very different from workflow  or Plugins.

Can be associated with single entity or globally.
Actions support Arguments – Input and Output Arguments.
Can be only called through web service calls Like  JavaScript/Plugins/Web through API calls.
Always runs under the context of the calling user.

 Input Arguments:

These are the parameters which need to be passed during action call. These can be Optional/Mandatory. For Mandatory argument it is necessary to provide some value while calling this action.

Output Arguments:

These are the parameters which can be returned when the action is called. These can be Optional/Mandatory.




Let me create a simple Action on Enquiry entity which has one Input Argument: ProjectName [string]

Flow: Settings > Processes > select Category as Actions > In primary entity > Global.                    Input Parameter- InputData.

new_gb” is the name of the Action which should be referred while calling this through API.


Calling Actions from Plugins

Call this Action from our Plugin. I have a Plugin which fires on Post Create of account  am passing the Input Argument Inputdata Parameter before calling this Action. Once this is called It creates a new contact record.

Flow – Account Created – Plugin Triggered(Create Message-post operation)-Plugin triggered then action will be called passing account name to input data- Action step will create the contact record.

// Calling the Action - new_EnquiryCreateProject
OrganizationRequest req = new OrganizationRequest("new_gb");
req["inputdata"] = account.Attributes["name"].toString(); or "Accountname";
for Global Action No need of Target.
//req["Target"] = new EntityReference("contact", contact.Id);

//execute the request
OrganizationResponse response = service.Execute(req);