Search code examples
pluginsdynamics-crmcrm

Plugin update field from a related entity


I want to build a plugin for updating a field from a related entity. On Entity Agreement I have two fields: field A(look-up Account) and field B(look-up Contact)

I have to check if an option set field from Account(field A) is one specific value, then to update an option set field from Contact(field B).

That's all. Thanks!


Solution

  • Entity agreement = (Entity)context.InputParameters["Target"]; // this is your target entity from which plugin is firing.
    
    // Now do whatever your check you wish to perform
    
    // once you are ready to update lookupAccount Record below code will work
    
      Entity lookupAccount = new Entity();
                        lookupAccount.LogicalName = "account";
                        lookupAccount.Id = agreement.GetAttributeValue<EntityReference>("field A").Id;
                        lookupAccount["optionSetFieldToUpdate"]= new OptionSetValue(1234);
    
                        orgService.Update(lookupAccount);
    
    
    
    // In similar way you can perform update for ook-up Contact)
    
    
     Entity lookupContact = new Entity();
                        lookupContact.LogicalName = "contact";
                        lookupContact.Id = agreement.GetAttributeValue<EntityReference>("field B").Id;
                        lookupContact["optionSetFieldToUpdate"]= new OptionSetValue(1234);
    
                        orgService.Update(lookupContact);