I am writing a custom workflow / plugin to be fired for a custom entity when a record that is of type custom_entity is deactivated. Custom_entity is related to Account and the relationship is many to one (or looking at it from Account's perspective, Account has a one to many relationship with Custom_entity).
When a record of type Custom_entity is deactivated, I need to check to see if the owning Account can be deactivated. First step in that process is to lay hands on the owning Account, which would you think would be simple.
Apparently it's not. It appears to take herculean effort to accomplish that one minimal thing.
Here is what I have tried:
relatedAccountReference = custom_Entity.GetAttributeValue<EntityReference>("new_Account");
relatedAccount = service.Retrieve("account", relatedAccountReference.Id, columnsAccount);
relatedAccountId = relatedAccountReference.Id;
The plugin is failing on the first line with error: Object reference not set to an instance of an object.
Earlier in the code I'm using the org service to retrieve custom_Entity. Since it's GUID is available through the context, retrieving this record is straight forward. The column set it uses does not contain "new_Account". If I try to add new_Account as a column in the columnset, I get a different error: The given key is not present in the dictionary.
I'm about to pull my hair out. All I need to do is access the related account that an instance of custom_Entity is bound to. The lookup field in CRM for this linkage is called "new_Account".