locked
Update plugin code review please... RRS feed

  • Question

  • Can someone please review my code.

    My scenario is:

    I need to update a note section of a form with the instructions saved in an entity of task form.

    I tried writing the code looking at the sample update plugins of sdk.

    So I am not confident about it like using _seviceProxy and retrieving the columnset into correct entity and all such things.

    Here is my code please let me know the correct code if I am missing something.

    Thank you in advance.

    protected void ExecutePostTasksIntUpdate(LocalPluginContext localContext)

           {

               if (localContext == null)

               {

                   throw new ArgumentNullException("localContext");

               }

               // TODO: Implement your custom Plug-in business logic.

               //Obtain the execution context from the service provider

               IPluginExecutionContext context = localContext.PluginExecutionContext;

               IOrganizationService service = localContext.OrganizationService;

               //The input parameters collections contains all the data passed in the message request

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

               {

                   //Obtain the target entity from the input parameters

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

                   if (entity.LogicalName == "new_tasksint") //your task entity?

                   {

                       try

                       {

                           if (entity.Attributes.Contains("new_instructions")) //your instruction field in the task entity

                           {

                               //assign the NoteText from new_instruction field

                               string entityNoteText = (string)entity.Attributes["new_instructions"];

                               //Retrieve the Note entity

                               ColumnSet cols = new ColumnSet(new String[] { "new_owner", "new_instructions" });

                               new_tasksint retrievedTasksint = (Tasksint)_serviceProxy.Retrieve("new_tasksint", new_call, cols);

                               //if Note is not exists then create, otherwise update

                               if (retrievedTasksint.entities.count > 0)

                               {

                                   //update the notetext

                                   entityNoteText = _serviceProxy.Update(retrievedTasksint);

                               }

                           }

                       }

                   }

               }

           }

    Monday, June 9, 2014 5:14 PM

All replies

  • If you're adding to the out of the box notes control on a form, you need to create a record under the 'annotation' entity. Each annotation represents a single note on your form. the annotation has a few fields relating to the subject, description, and record it relates to. I would think based on your requirement you would want to create a new note when your instructions field changes.

    The annotation entity contains Subject, NoteText, ObjectId, and ObjectTypeCode.

    Your code to create the note should look something like this (assuming the note is being added to your new_tasksint entity):

    Entity note = new Entity("annotation");
    note["subject"] = "Instructions";
    note["notetext"] = entityNoteText;
    note["objectid"] = retrievedTasksint.ToEntityReference();
    note["objecttypecode"] = retrievedTasksint.LogicalName;
    
    _serviceProxy.Create(note);

    Hope that helps

    Paul


    If my response helped you find your answer please show your thanks by taking the time to "Mark As Answer" and "Vote As Helpful".

    Twitter LinkedIn Facebook Blog Magnetism


    Monday, June 9, 2014 9:49 PM
  • Thanks Paul..

    I really appreciate it.

    I would like to know what is objectId and objecttypecode?

    And after creating the note how can I update the Notes section on my farm.

    service.Update(note); Is this correct?

    Thank you

    Tuesday, June 10, 2014 4:11 AM
  • Hello,

    While creating notes you need to associate it with some entity so in above case we are associating notes with task entity, so objectid is task records id and Objecttypecode will be name of the entity (in this case it is task).

    You can refer annotation entity metadata for more details.


    Our Website| Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Tuesday, June 10, 2014 4:23 AM
    Moderator
  • Thank you..

    Got it..

    And also what does EntityReference do?

    Tuesday, June 10, 2014 4:27 AM
  • EntityReference is a datatype, which is used for lookup fields (to get/set)

    Our Website| Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Tuesday, June 10, 2014 4:52 AM
    Moderator