locked
Update Related Entity from Plugin RRS feed

  • Question

  • I have a plugin registered against one entity A. I want to update a field in another entity B when a field in entity changes.

    Can I use the same plugin to update entity B even though it is registered against entity A ?


    • Edited by JMcCon Wednesday, October 28, 2015 7:36 PM
    Wednesday, October 28, 2015 2:35 PM

Answers

  • Hi JMcCon,

    Yes, you can update another entity from plugin. All you need to do is get the organization service from your plugin context and use it to execute a query which returns the related entity. Then you can update the entity with another service call.

    Regards,

    Adam

    Monday, November 2, 2015 2:53 PM
  • Hi,

    How are the entities related to each other?

    In short the code is something similar to this:

    Entity objB = new Entity("objB");
    objB.Id = <GUID for object>;
    objB.attributes.Add("<fieldname>", <value>);
    service.Update(objB);

    You need to find the id for the object you want to update, other than that it's pretty easy.

    Regards


    Rickard Norström Developer CRM-Konsulterna
    http://www.crmkonsulterna.se
    Swedish Dynamics CRM Forum: http://www.crmforum.se
    My Blog: http://rickardnorstrom.blogspot.se

    • Proposed as answer by Mayank Pujara Wednesday, November 4, 2015 10:57 AM
    • Marked as answer by HIMBAPModerator Saturday, November 7, 2015 11:05 AM
    Tuesday, November 3, 2015 11:21 AM

All replies

  • Hi JMcCon,

    Yes, you can update another entity from plugin. All you need to do is get the organization service from your plugin context and use it to execute a query which returns the related entity. Then you can update the entity with another service call.

    Regards,

    Adam

    Monday, November 2, 2015 2:53 PM
  • Thanks for the reply Adam.

    How can I do what you suggest within a plugin?

    Tuesday, November 3, 2015 9:06 AM
  • Hi,

    How are the entities related to each other?

    In short the code is something similar to this:

    Entity objB = new Entity("objB");
    objB.Id = <GUID for object>;
    objB.attributes.Add("<fieldname>", <value>);
    service.Update(objB);

    You need to find the id for the object you want to update, other than that it's pretty easy.

    Regards


    Rickard Norström Developer CRM-Konsulterna
    http://www.crmkonsulterna.se
    Swedish Dynamics CRM Forum: http://www.crmforum.se
    My Blog: http://rickardnorstrom.blogspot.se

    • Proposed as answer by Mayank Pujara Wednesday, November 4, 2015 10:57 AM
    • Marked as answer by HIMBAPModerator Saturday, November 7, 2015 11:05 AM
    Tuesday, November 3, 2015 11:21 AM