locked
Retrieve an entity using a relationship in a Plugin. RRS feed

  • Question

  • Hello all,

    I have two entities, Entity A and Entity B, joined by a N:1 relationship (B can have many As).

    My plugin will fire upon creation of an Entity A, it needs to update an option set attribute in Entity B.

    I'm having a lot of trouble implementing this, as simple as it seems. Since I'm using CRM online I cannot use Microsoft.Xrm.Client which would make this trivial. At the moment I'm trying to use retrieve to get the entity and update it. Here's my code:

    using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Query; //using Microsoft.Xrm.Client; NOT ALLOWED TO USE namespace UpdateB { public class UpdateB : IPlugin { public void Execute(IServiceProvider serviceProvider) { // Obtain the execution context from the service provider. IPluginExecutionContext context = (IPluginExecutionContext) serviceProvider.GetService(typeof(IPluginExecutionContext)); IOrganizationServiceFactory serveFactory = (IOrganizationServiceFactory) serviceProvider.GetService(typeof(IOrganizationServiceFactory)); IOrganizationService service = (IOrganizationService)serveFactory.CreateOrganizationService(context.UserId); //get Entity A Entity entity = (Entity)context.InputParameters["Target"];

    //define it's guid ID Guid guid = new Guid("2BC2B4EA-8715-E311-8E90-B4B52F67D682");

    //define the attributes I want ColumnSet cols = new ColumnSet(new string[] {"B_Attribute"})

    //Retrieve entity B

    Entity B = service.Retrieve("Entity_B", guid ,cols);

    //update Entity B Attribute

    citation.Attributes["B_Attribute"] = 100000002; } }





    • Edited by rbwildeman Monday, September 16, 2013 3:57 PM
    Monday, September 16, 2013 3:53 PM

Answers

All replies

  • Hi,

    1. Get entity b guid from proptery bag of entity a (as you have b's lookup in entity a).

    2. Not sure why you are retrieving data from entity b.

    3. You can directly write update statement and update entity b attribute and call update method. like below

    Entity _EntityB= new Entity();
    _EntityB["B_Attribute"] =100000002;

    // Is it a optionset if yes you need to use like below

    //_EntityB["B_Attribute"] =new OptionSetValue(100000002);
    _EntityB.Id = _EntityBID;
    service.Update(_EntityB);

    HTH


    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.

    Monday, September 16, 2013 4:04 PM
    Moderator
  • Thank you for your help! Could you explain this line to me?

    _EntityB.Id = _EntityBID;

    I don't understand it's purpose.

    Thanks!

    EDIT:

    Per your advice I changed my code as follows:

                Guid guid = new Guid("2BC2B4EA-8715-E311-8E90-B4B52F67D682");
                ColumnSet cols = new ColumnSet(new string[] {"new_status"});
    
                Entity _EntityB= new Entity();
                _EntityB= service.Retrieve("EntityB", guid, cols);
    
    
    
                _EntityB["Attribute_B"] = new OptionSetValue(100000002);
                _EntityB.ID = _EntityB.ID;
                service.Update(_EntityB);

    And it's running completely through, but the attributeB is not updating. Did I miss something?

    Thanks!


    • Edited by rbwildeman Monday, September 16, 2013 6:16 PM
    Monday, September 16, 2013 5:13 PM
  • Please check my previous response, I asked you why you are using retrieve, as you already have guid you should directly assign GUID like below

    Entity _EntityB= new Entity();
    _EntityB["B_Attribute"] =100000002;

    // Is it a optionset if yes you need to use like below

    //_EntityB["B_Attribute"] =new OptionSetValue(100000002);
    _EntityB.Id = new Guid("2BC2B4EA-8715-E311-8E90-B4B52F67D682");   //to set key field
    service.Update(_EntityB);

    and if you still want to use retrieve you should not assign id because that is already there in entity object this line

        Guid guid = new Guid("2BC2B4EA-8715-E311-8E90-B4B52F67D682");
                ColumnSet cols = new ColumnSet(new string[] {"new_status"});
                Entity _EntityB= new Entity();
                _EntityB= service.Retrieve("EntityB", guid, cols);
                _EntityB["Attribute_B"] = new OptionSetValue(100000002);
                 service.Update(_EntityB);

    Let me know if you need other information


    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.

    • Proposed as answer by HIMBAPModerator Tuesday, September 17, 2013 5:36 AM
    Tuesday, September 17, 2013 5:36 AM
    Moderator
  • I think I see the issue. I should not have hardcoded the Guid. I misunderstood exactly what it referred to and should be using the Guid  for EntityB from the EntityA property bag. How do I access that?
    Tuesday, September 17, 2013 2:49 PM
  • Hi,

    Yes that's what you need to do, you can use below code to get guid

    Guid _EntityBId=EntityA.GetAttributeValue<EntityReference>("lookupfieldname").Id;


    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.

    • Proposed as answer by HIMBAPModerator Tuesday, September 17, 2013 4:30 PM
    • Marked as answer by rbwildeman Tuesday, September 17, 2013 5:42 PM
    Tuesday, September 17, 2013 4:30 PM
    Moderator