Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Updating an annotation(note) in a plugin.

Posted on by Microsoft Employee

Hi

I have a plugin firing on "Create", "Preoperation" on "annotation".

It is very simple I want to modify the "notetext" and that is it.

When the plugin runs, and I confirm this by throwing an InvalidPluginExecutionException, the record is not modified.

I don't throw the exception when testing to see if the record is modified.

Is there any restriction on updating an annotation from a plugin?

Has anyone else experienced a similar problem?

Thanks in advanced.

Paul

The code also includes some other things I have tried and are commented out.

    public partial class TagNoteWithWeb : BasePlugin
    {
        public TagNoteWithWeb(string unsecureConfig, string secureConfig) : base(unsecureConfig, secureConfig)
        {
            // Register for any specific events by instantiating a new instance of the 'PluginEvent' class and registering it
            base.RegisteredEvents.Add(new PluginEvent()
            {
                Stage = eStage.PreOperation,
                MessageName = "Create",
                EntityName = "annotation",
                PluginAction = ExecutePluginLogic
            });
        }
        public void ExecutePluginLogic(IServiceProvider serviceProvider)
        {
            // Use a 'using' statement to dispose of the service context properly
            // To use a specific early bound entity replace the 'Entity' below with the appropriate class type
            using (var localContext = new LocalPluginContext<Entity>(serviceProvider))
            {
                // Todo: Place your logic here for the plugin
                var context = localContext.PluginExecutionContext;
                Entity entity = new Entity();
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    entity = (Entity)context.InputParameters["Target"];

                    if (entity.LogicalName != "annotation")
                    {
                        localContext.Log("Plug-in not registered correctly, entity is " + entity.LogicalName + ".", System.Diagnostics.EventLogEntryType.Error);
                        localContext.Trace("Plug-in not registered correctly, entity is " + entity.LogicalName + ".");
                    }
                }
                if (entity.Attributes.Contains("notetext"))
                {
                    if (!entity.Attributes["notetext"].ToString().Contains("*WEB*"))
                    {
                        string tempText = entity.Attributes["notetext"].ToString() + "  *WEB*";
                        //string tempText = entity.Attributes["notetext"].ToString() + Environment.NewLine + "*WEB*";
                        //entity.Attributes.Remove("notetext");
                        //entity.Attributes.Add("notetext", tempText);

                        entity.Attributes["notetext"] = tempText;
                        entity.Attributes["subject"] = tempText;
                        //throw new InvalidPluginExecutionException(entity.Attributes["notetext"].ToString());
                    }
                }
                else
                {
                    entity.Attributes.Add("notetext", "*WEB*");
                }
            }
        }
    }

    
public partial class TagNoteWithWeb : BasePlugin
    {
        public TagNoteWithWeb(string unsecureConfig, string secureConfig) : base(unsecureConfig, secureConfig)
        {
            // Register for any specific events by instantiating a new instance of the 'PluginEvent' class and registering it
            base.RegisteredEvents.Add(new PluginEvent()
            {
                Stage = eStage.PreOperation,
                MessageName = "Create",
                EntityName = "annotation",
                PluginAction = ExecutePluginLogic
            });
        }
        public void ExecutePluginLogic(IServiceProvider serviceProvider)
        {
            // Use a 'using' statement to dispose of the service context properly
            // To use a specific early bound entity replace the 'Entity' below with the appropriate class type
            using (var localContext = new LocalPluginContext<Entity>(serviceProvider))
            {
                // Todo: Place your logic here for the plugin
                var context = localContext.PluginExecutionContext;
                Entity entity = new Entity();
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    entity = (Entity)context.InputParameters["Target"];
 
                    if (entity.LogicalName != "annotation")
                    {
                        localContext.Log("Plug-in not registered correctly, entity is " + entity.LogicalName + ".", System.Diagnostics.EventLogEntryType.Error);
                        localContext.Trace("Plug-in not registered correctly, entity is " + entity.LogicalName + ".");
                    }
                }
                if (entity.Attributes.Contains("notetext"))
                {
                    if (!entity.Attributes["notetext"].ToString().Contains("*WEB*"))
                    {
                        string tempText = entity.Attributes["notetext"].ToString() + "  *WEB*";
                        //string tempText = entity.Attributes["notetext"].ToString() + Environment.NewLine + "*WEB*";
                        entity.Attributes.Remove("notetext");
                        entity.Attributes.Add("notetext", tempText);
 
                        //entity.Attributes["notetext"] = tempText;
                        //entity.Attributes["subject"] = tempText;
                        //throw new InvalidPluginExecutionException(entity.Attributes["notetext"].ToString());
                    }
                }
                else
                {
                    entity.Attributes.Add("notetext", "*WEB*");
                }
            }
        }
    }

*This post is locked for comments

  • Suggested answer
    Nick Plourde Profile Picture
    Nick Plourde on at
    RE: Updating an annotation(note) in a plugin.

    I don't know any limitations. Your code seems fine and you are doing the right by updating the notetext in the PreOperation.

  • Suggested answer
    Dynamics_Alok Profile Picture
    Dynamics_Alok 1,746 on at
    RE: Updating an annotation(note) in a plugin.

    Code looks good but make some changes to avoid any chance of error  like

    you are checking NoteText by 

                   if (entity.Attributes.Contains("notetext"))
                    {
                        if (!entity.Attributes["notetext"].ToString().Contains("*WEB*"))
                        {
                             //Code here 
                        }
                    }

    But in case of null notetext ,it will throw error .So in place of 

    entity.Attributes["notetext"].ToString()  write 
    Convert.ToString(entity.Attributes["notetext"]).
  • Tunc S Profile Picture
    Tunc S on at
    RE: Updating an annotation(note) in a plugin.

    Did you figure out a solution by any chance?  We're having the same issue... 

  • Yadnyesh Kuvalekar Profile Picture
    Yadnyesh Kuvalekar 4,102 on at
    RE: Updating an annotation(note) in a plugin.

    Hi,

    What error are you getting? Can you paste the error?

  • Tim Dutcher Profile Picture
    Tim Dutcher 2,100 on at
    RE: Updating an annotation(note) in a plugin.

    I'd also create a version of your annotation plug-in that simply sets the notetext to "ABC" or whatever on pre-operation of a new note, removing all other logic. This will show that it's possible to override the note text. Then it's a matter of determining why the current plug-in isn't working.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Updating an annotation(note) in a plugin.

    Hi Tim

    Thanks for the reply.

    I have several other plugins similarly created for other entities and they all work as required.

    I will try the trace thing and see if that can point to anything amiss.

    Paul

  • Tim Dutcher Profile Picture
    Tim Dutcher 2,100 on at
    RE: Updating an annotation(note) in a plugin.

    I've done a similar thing in the past with the annotation entity without a problem. And your code looks ok.

    Do you have other plug-ins that update an entity record pre-operation?  I'm asking because it looks like you are using a plug-in framework (BasePlugin) and it could be that something there has been modified or is possibly trapping an exception.

    If you create another plug-in that simply puts an "X" or something after a contact's last name when you create a contact (assuming you're in a dev environment) then that will either work, or not, and that will give you more information to work with.  Also, in your current plug-in, you can add Trace statements to help view the path that your code is taking.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans