locked
copying notes from one entity to another RRS feed

  • Question

  • Hi,

    I've created a button in the case form and when clicked it a javascript function gets triggered and opens a custom entity form which also has notes enabled.

    I am trying to copy notes from case entity to custom entity on button click.

    So far, I've done this plugin and registered it on create message post operation. But when I tried to create a note in case entity, and hit 'Done' button, nothing gets created. Note is missing and it is not getting created in custom entity as well.

    Can someone please take a look and let me know what is the problem?

    protected void ExecutePostNoteCreate(LocalPluginContext localContext)
            {
                if (localContext == null)
                {
                    throw new ArgumentNullException("localContext");
                }
    
                // TODO: Implement your custom Plug-in business logic.
                // get the plugin context 
                IPluginExecutionContext context = localContext.PluginExecutionContext;
    
                //Get the IOrganizationService
                IOrganizationService service = localContext.OrganizationService;
    
                if (context.InputParameters.Contains("Target") &&
               context.InputParameters["Target"] is Entity)
                {
                    // Obtain the target entity from the input parmameters.
                    Entity entity = (Entity)context.InputParameters["Target"];
    
                    if (entity.LogicalName != "annotation")
    
                        return;
                    try
                    {
                       // string notetextValue = entity["notetext"].ToString();
    
                        //Source object Identifier
                        Guid noteId = ((EntityReference)entity["objectid"]).Id;
    
                        //target Identifier
                        Guid historynoteid = (Guid)context.OutputParameters["Id"];
    
                        //retrieve annotation file
                        QueryExpression Notes = new QueryExpression { EntityName = "annotation", ColumnSet = new ColumnSet(true) };
                        Notes.Criteria.AddCondition("objectid", ConditionOperator.Equal, noteId);
                        Notes.Criteria.AddCondition("objecttypecode", ConditionOperator.Equal, 112);
                        EntityCollection retrieved = service.RetrieveMultiple(Notes);
                        
                        foreach (var c in retrieved.Entities)
                        {
                            //Remove key field of annotation  
                            c.Attributes["annotationid"] = null;
    
                            //Replace referencing object lookup with newly created historynote lookup
                            c.Attributes["objectid"] = new EntityReference("new_noteshistory", historynoteid);
                            c.Attributes["objecttypecode"] = 10072;
    
                            service.Create(c);
                        }
    
                    }
                    catch (Exception generalException)
                    {
                        throw new InvalidPluginExecutionException("Plugin Failed - Execute :-(" + generalException.Message);
                    }
                }
            }

    Thanks for any help.

    Tuesday, May 5, 2015 6:42 PM

Answers

  • This plugin is on annotation? So this linie is strange:

    historynoteid = (Guid)context.OutputParameters["Id"];

    You didn't create any historynote in this plugin... so target Identifier  is not historynote guid

    I think problem in this plugin is with historynote entity.

    You should add plugin to historynote on post create (historynote have to exist before annotations movement) -> then find case -> find annotations from case -> move/copy to historynote.

    here you have some example about copy annotation:

    http://a-crm.blogspot.com/2007/03/how-to-copy-annotation.html

    • Marked as answer by ReignFan Saturday, May 16, 2015 2:06 PM
    Tuesday, May 5, 2015 10:12 PM
  • Hello,

    noteId should be entity.Id in your case (since the plugin is registered on the annotation entity).



    Cornel Croitoriu - Senior Software Developer

    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    Biz-Forward.comCroitoriu.NET

    • Marked as answer by ReignFan Saturday, May 16, 2015 2:05 PM
    Thursday, May 7, 2015 6:48 AM

All replies

  • This plugin is on annotation? So this linie is strange:

    historynoteid = (Guid)context.OutputParameters["Id"];

    You didn't create any historynote in this plugin... so target Identifier  is not historynote guid

    I think problem in this plugin is with historynote entity.

    You should add plugin to historynote on post create (historynote have to exist before annotations movement) -> then find case -> find annotations from case -> move/copy to historynote.

    here you have some example about copy annotation:

    http://a-crm.blogspot.com/2007/03/how-to-copy-annotation.html

    • Marked as answer by ReignFan Saturday, May 16, 2015 2:06 PM
    Tuesday, May 5, 2015 10:12 PM
  • Hello,

    noteId should be entity.Id in your case (since the plugin is registered on the annotation entity).



    Cornel Croitoriu - Senior Software Developer

    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    Biz-Forward.comCroitoriu.NET

    • Marked as answer by ReignFan Saturday, May 16, 2015 2:05 PM
    Thursday, May 7, 2015 6:48 AM