Posted in CRM, Plugin

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);

tracingService.Trace("one");
 try
 {

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)));
 tracingService.Trace(notes.Entities.Count.ToString());

foreach (var note in notes.Entities)
 {
 tracingService.Trace("loopenter");
 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"))
 {
 tracingService.Trace("subjecthasdata");
 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();
 }
 tracingService.Trace("allfieldsloaded");
 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);
 }
 tracingService.Trace("outofloop");

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