web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

how to pass notes from lead to opportunity after qualifying the lead

(0) ShareShare
ReportReport
Posted on by

Hi,

When we qualify the lead,then how to notes pass from lead to opportunity.

Please tell me step by step using plugins because i am new in CRM.

Please suggest me solution.

Thanks in advance!

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at
  • Verified answer
    Shahbaaz Ansari Profile Picture
    6,211 on at

    Hi FinanceOperation,

    I have done this kind of work through plugin, the only issue is that who ever qualify the lead, all the notes in the opportunity will be create by that person.

    Below is the full code, you need to register plugin on pre-operation and as sync

    using Microsoft.Xrm.Sdk;

    using Microsoft.Xrm.Sdk.Query;

    using System;

    namespace CopyNotesFromLeadToOpportunity

    {

       public class CopyNotes : IPlugin

       {

           public void Execute(IServiceProvider serviceprovider)

           {

               // Extract the tracing service for use in debugging sandboxed plug-ins.

               ITracingService tracing = (ITracingService)serviceprovider.GetService(typeof(ITracingService));

               //get execution context from service provider

               IPluginExecutionContext context = (IPluginExecutionContext)serviceprovider.GetService(typeof(IPluginExecutionContext));

               // Get a reference to the Organization service.

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

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

               //Check context contain entity

               if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)

               {

                   //Check weather the Target entity have the originating lead filed or not

                   Entity selectedEntity = (Entity)context.InputParameters["Target"];

                   if (selectedEntity.Attributes.Contains("originatingleadid") == false)

                   {

                       return;

                   }

                   else

                   {

                       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' />                                              

                                                   <attribute name='modifiedby' />

                                                   <attribute name='modifiedon' />                                              

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

                                       // last modified by user

                                       EntityReference modifiedBy = (EntityReference)entNote.Attributes["modifiedby"];

                                       string Modifiedby = modifiedBy.Name;

                                       Guid modifiedByUser = modifiedBy.Id;

                                       // Last modified date of notes (getting time in UTC)

                                       DateTime ModifiedOn = Convert.ToDateTime(entNote.Attributes["modifiedon"].ToString());

                                       // get the time zone of the user who last modified the notes

                                       QueryExpression fetchTimeZone = new QueryExpression()

                                       {

                                           EntityName = "usersettings",

                                           ColumnSet = new ColumnSet("localeid", "timezonecode"),

                                           Criteria =

                                                       {

                                                         Conditions =

                                                           {

                                                               new ConditionExpression("systemuserid", ConditionOperator.Equal,modifiedBy.Id)

                                                           },

                                                       },

                                       };

                                       // Join CRM timezonedefinition with usersettings

                                       LinkEntity linkTimeZoneInfo = new LinkEntity("usersettings", "timezonedefinition", "timezonecode", "timezonecode", JoinOperator.Inner);

                                       linkTimeZoneInfo.Columns = new ColumnSet("standardname");

                                       linkTimeZoneInfo.EntityAlias = "timeZone";

                                       fetchTimeZone.LinkEntities.Add(linkTimeZoneInfo);

                                       DataCollection<Entity> Timezone = service.RetrieveMultiple(fetchTimeZone).Entities;

                                       //get the timezone of the user

                                       string timeZoneOfUser = ((Microsoft.Xrm.Sdk.AliasedValue)(Timezone[0].Attributes["timeZone.standardname"])).Value.ToString();

                                       // convert UTC time to User time

                                       TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById(timeZoneOfUser);

                                       DateTime userConvertedTime = TimeZoneInfo.ConvertTimeFromUtc(ModifiedOn, tz);

                                       string modifiedDateTime = userConvertedTime.ToString("MM/dd/yy H:mm:ss tt") + " " + ("(" + timeZoneOfUser + ")");

                                       //string modifiedDateTime = userConvertedTime.ToString("MM/dd/yy H:mm:ss tt");

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

                                       entNewAnnotation["notetext"] = entNote.GetAttributeValue<string>("notetext") + "\r\n" + "Lead Note By : " + Modifiedby + "\r\n" + "Lead Note On : " + modifiedDateTime;

                                       service.Create(entNewAnnotation);

                                       #endregion

                                   }//End for notes count

                               }//End entities count

                           }//End of create contex

                       }

                       catch (Exception ex)

                       {

                           tracing.Trace("Exception : " + ex.Message);

                           throw new InvalidPluginExecutionException(ex.Message.ToString());

                       }

                   }

               }

           }

           #region Clone Record

           /// <summary>

           /// Creates Cloned copy of soure entity for the given entity object

           /// </summary>

           /// <param name="targetEntityName">Schemaname of the cloned copy target entity</param>

           /// <param name="sourceEntity">Entity object of the record to be Cloned</param>

           /// <param name="strAttributestoRemove">Array of records not to include in the cloned copy</param>

           /// <returns>Entity object (Cloned Copy)</returns>

           private Entity CloneRecordForEntity(string targetEntityName, Entity sourceEntity, string[] strAttributestoRemove)

           {

               //Initiate target entity (cloned copy)

               Entity clonedEntity = new Entity(targetEntityName);

               //read the attributes from source entity

               AttributeCollection attributeKeys = sourceEntity.Attributes;

               //Loop through each of the key and check and add that in the target entity

               foreach (string key in attributeKeys.Keys)

               {

                   //Check if key is not there in the list of removed keys

                   if (Array.IndexOf(strAttributestoRemove, key) == -1)

                   {

                       //add key from source in the destination

                       clonedEntity[key] = sourceEntity[key];

                   } //end if checking removed attributes

               }//end foreach keys

               return clonedEntity;

           }

           #endregion

       }

    }

    Mark My answer as verified if it helps,

    Thanks,

    Shahbaaz

  • Suggested answer
    Shahbaaz Ansari Profile Picture
    6,211 on at

    One more thing i had the issue about the timezone and old owner of the notes that were on lead, let say example User A create lead Lead1 and create notes N1 in Lead L1 and that user is in USA means in Different timezone, now User B qualify Lead L1, so Opportunity Opp1 will get created, with note N1 (that was in Lead L1) with owner as User2 and Note N1 creation date and create by will get append in Note N1.

    Please give a try to above code.

    Thanks,

    Shahbaaz

  • Suggested answer
    Shahbaaz Ansari Profile Picture
    6,211 on at

    Hi FinanceOperation,

    Did you tried above code??

    Thanks,

    Shahbaaz

  • Suggested answer
    Shahbaaz Ansari Profile Picture
    6,211 on at

    Hi Finance,

    You need to register your plugin on the post-operation and keep it as sysn.

    Hope this helps!!!

    Best Regards,

    Shahbaaz

  • Community Member Profile Picture
    on at

    Hi Shahbaaz,

    Thanks your code is working1

  • Community Member Profile Picture
    on at

    Hi Shahbaaz,

    I also want to notes in account and contact, please tell me how to do this.

  • Suggested answer
    DynamicsExperts Profile Picture
    on at

    Instead of programming a solution try using Microsoft Flow, the steps are explained here: www.crminnovation.com/.../use-microsoft-flow-to-copy-a-qualified-leads-notes-to-the-new-opportunity if you still need help reach out to me.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
JS-09031509-0 Profile Picture

JS-09031509-0 3

#2
AS-17030037-0 Profile Picture

AS-17030037-0 2

#2
Mark Eckert Profile Picture

Mark Eckert 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans