locked
MSCRM 2011 - Transfer All Notes and Attachments from Lead to newly created Account and Contact when Lead is Resolved. RRS feed

  • Question

  • Hi Team,

    I have a requirement in which I have to Transfer All Notes and Attachments from Lead to newly created Account and Contact when Lead is Resolved. 

    I am planning to write a Plugin on Post Event of SetStateEntity and SetStateDynamicEntity Messages.

    Can someone Please share the code to do so. Kindly share MSCRM 2011 Code only.

    Any other Approach is also Appreciated.

    Thanks - Abbas.

    Tuesday, October 30, 2012 4:28 AM

Answers

  • Hi,

    this is the code you need to clone notes from the lead record to the contact record:

    protected void Execute(LocalPluginContext localContext)
            {
                if (localContext == null)
                {
                    throw new ArgumentNullException("localContext");
                }
    
                var leadId = localContext.PluginExecutionContext.PrimaryEntityId;
    
                var notesQuery = new QueryExpression("annotation");
                var notesFilter = new FilterExpression(LogicalOperator.And);
                notesFilter.AddCondition("objectid", ConditionOperator.Equal, leadId.ToString());
                notesQuery.Criteria = notesFilter;
                notesQuery.ColumnSet = new ColumnSet(true);
    
                var leadNotes = localContext.OrganizationService.RetrieveMultiple(notesQuery);
    
                if (leadNotes.Entities != null && leadNotes.Entities.Count > 0)
                {
                    foreach (var note in leadNotes.Entities)
                    {
                        note["objectid"] = new EntityReference("contact", new Guid("contactGuid"));
                        note.Attributes.Remove("objecttypecode");
                        note.Attributes.Remove("annotationid");
                        note.Id = Guid.NewGuid();
                        localContext.OrganizationService.Create(note);
                    }
                }
            }

    Greetings,

    Pavlos


    Please mark this reply as an answer and vote it as helpful if it helps you find a resolution to your problem.
    View my latest gallery contribution here.
    Visit my blog here.

    Wednesday, October 31, 2012 7:46 AM

All replies

  • Hi,

    you can use something like this in your plugin code:

    protected void Execute(LocalPluginContext localContext)
            {
                if (localContext == null)
                {
                    throw new ArgumentNullException("localContext");
                }
    
                var leadId = localContext.PluginExecutionContext.PrimaryEntityId;
    
                var notesQuery = new QueryExpression("annotation");
                var notesFilter = new FilterExpression(LogicalOperator.And);
                notesFilter.AddCondition("objectid", ConditionOperator.Equal, leadId.ToString());
                notesQuery.Criteria = notesFilter;
    
                var leadNotes = localContext.OrganizationService.RetrieveMultiple(notesQuery);
    
                if (leadNotes.Entities != null && leadNotes.Entities.Count > 0)
                {
                    foreach (var note in leadNotes.Entities)
                    {
                        note["objectid"] = new EntityReference("contact", new Guid("contactGuid"));
                        localContext.OrganizationService.Update(note);
                    }
                }
            }

    Using this code, you can transfer all annotations from a lead record to a contact record.

    Greetings,

    Pavlos


    Please mark this reply as an answer and vote it as helpful if it helps you find a resolution to your problem.
    View my latest gallery contribution here.
    Visit my blog here.



    Tuesday, October 30, 2012 7:39 AM
  • Hi Pavlos,

    Thanks for your prompt reply.

    I have a query, On which Message I have to register?

    Also if I need to Copy Notes from Lead to Contact instead of Transfer, can you please share the code.

    Thanks - Abbas


    Thanks and Regards, Abbas

    Wednesday, October 31, 2012 4:44 AM
  • Hi,

    this is the code you need to clone notes from the lead record to the contact record:

    protected void Execute(LocalPluginContext localContext)
            {
                if (localContext == null)
                {
                    throw new ArgumentNullException("localContext");
                }
    
                var leadId = localContext.PluginExecutionContext.PrimaryEntityId;
    
                var notesQuery = new QueryExpression("annotation");
                var notesFilter = new FilterExpression(LogicalOperator.And);
                notesFilter.AddCondition("objectid", ConditionOperator.Equal, leadId.ToString());
                notesQuery.Criteria = notesFilter;
                notesQuery.ColumnSet = new ColumnSet(true);
    
                var leadNotes = localContext.OrganizationService.RetrieveMultiple(notesQuery);
    
                if (leadNotes.Entities != null && leadNotes.Entities.Count > 0)
                {
                    foreach (var note in leadNotes.Entities)
                    {
                        note["objectid"] = new EntityReference("contact", new Guid("contactGuid"));
                        note.Attributes.Remove("objecttypecode");
                        note.Attributes.Remove("annotationid");
                        note.Id = Guid.NewGuid();
                        localContext.OrganizationService.Create(note);
                    }
                }
            }

    Greetings,

    Pavlos


    Please mark this reply as an answer and vote it as helpful if it helps you find a resolution to your problem.
    View my latest gallery contribution here.
    Visit my blog here.

    Wednesday, October 31, 2012 7:46 AM