I have a plugin that executes post operation on a custom entity and fails.
Unhandled exception: Exception type: System.ServiceModel.FaultException`1[Microsoft.Xrm.Sdk.OrganizationServiceFault] Message: An unexpected error occurred from ISV code. (ErrorType = ClientError) Unexpected exception from plug-in (Execute): ACEAssign.FO.cpp_franchiseprofileCreate: System.Exception: The given key was not present in the dictionary.Detail: e3151f36-5bb0-4d98-a548-9e934081be26 -2147220956 An unexpected error occurred from ISV code. (ErrorType = ClientError) Unexpected exception from plug-in (Execute): ACEAssign.FO.cust_ProfileUpdate: System.Exception: The given key was not present in the dictionary. 2021-02-10T15:41:25.8742193Z false PluginExecution PluginExecution Entered ACEAssign.FO.cust_ProfileUpdate.Execute(), Correlation Id: 90aa13cf-2889-4fd9-a01d-e994f790b401, Initiating User: 009284ef-7b36-e811-a972-000d3a1a7a9b Plugin has started. cust_Profile_Target Guid is 1bd91169-b66b-eb11-a812-000d3a9e00dc cust_Profile_PostOp Guid is 1bd91169-b66b-eb11-a812-000d3a9e00dc cust_Profile_PreOp Guid is 1bd91169-b66b-eb11-a812-000d3a9e00dc targetEntity.LogicalName is: cust_Profile postImage.LogicalName Contains: cust_primarycontactlookup postImage.LogicalName Contains: cust_rootlookup Primary Contact Lookup ID: b3d9a536-a6c4-e711-a952-000d3a1a711c Exiting ACEAssign.FO.cust_ProfileUpdate.Execute(), Correlation Id: 90aa13cf-2889-4fd9-a01d-e994f790b401, Initiating User: 009284ef-7b36-e811-a972-000d3a1a7a9b
I suspect the failure is not as easy as I was hoping it would be so let me lay out the scenario.
- A D365 Administrator creates a Profile Record in the cust_profile
- On the profile record are two lookup fields, cust_primarycontactlookup and cust_rootlookup. One or both will be blank on create. For the purposes of testing, the cust_primarycontactlookup will always have data that does not change.
- A workflow creates the cust_root record using data from the newly created cust_profile
- A 3rd party plugin executes on creation of the cust_root record and updates the cust_rootlookup field on the cust_profile record with the newly created cust_root
My plugin executes post operation on update of the cust_profile record specifically when the cust_rootlookup field or the cust_primarycontact field is updated as it uses the filtering attributes of cust_primarycontactlookup and cust_rootlookup
I also have a preImage and postImage registered with the same filtering attributes.
The cust_primarycontactlookup is already populated with data.
When the 3rd plugin mentioned in # 4 above runs and updates the cust_rootlookup field on the cust_profile record, my plugin executes but is unable to read or pull any data from the cust_rootlookup field due to “The given key was not present in the dictionary.” error.
The relevant code is shown below.
f (postImage.Attributes.Contains("cust_primarycontactlookup")) { TracingSvc.Trace("postImage.LogicalName Contains: cust_primarycontactlookup"); } //THE LINE DIRECTLY ABOVE IS THE LAST SUCCESSFUL LINE THAT LOGS if (postImage.Attributes.Contains("cust_rootlookup")) { TracingSvc.Trace("postImage.LogicalName Contains: cust_rootlookup"); } //THE LINE ABOVE DOES NOT LOG BECAUSE FOR SOME REASON THE DATA IN cust_rootlookup is not accessible. if (postImage.Attributes.Contains("cust_primarycontactlookup") && postImage.Attributes.Contains("cust_rootlookup")) { ColumnSet PrimaryContactreturnedAttributes = new ColumnSet(new String[] { "cust_primarycontactlookup" }); Entity EntityWithLookupFieldPrimaryContact = service.Retrieve(postImage.LogicalName, postImage_Guid, PrimaryContactreturnedAttributes); var PrimaryContactLookup_Guid = ((Microsoft.Xrm.Sdk.EntityReference)(EntityWithLookupFieldPrimaryContact.Attributes["cust_primarycontactlookup"])).Id; TracingSvc.Trace("Primary Contact Lookup ID: " PrimaryContactLookup_Guid); ColumnSet RootreturnedAttributes = new ColumnSet(new String[] { "cust_rootlookup" }); Entity EntityWithLookupFieldRoot = service.Retrieve(postImage.LogicalName, postImage_Guid, RootreturnedAttributes); var RootLookup_Guid = ((Microsoft.Xrm.Sdk.EntityReference)(EntityWithLookupFieldPrimaryContact.Attributes["cust_rootlookup"])).Id; TracingSvc.Trace("Root Lookup ID: " RootLookup_Guid);
The last line my trace log is able to log is the data related to the first lookup field.
TracingSvc.Trace("postImage.LogicalName Contains: cust_primarycontactlookup");
The code shown below is unable to get data from the lookup field and is the source of the failure.
However, the code is syntactically correct and only deals with the lookup field that was just populated (which in turn triggered my plugin).
if (postImage.Attributes.Contains("cpp_franchise")) { TracingSvc.Trace("postImage.LogicalName Contains: cpp_franchise"); } if (postImage.Attributes.Contains("cpp_primarycontact") && postImage.Attributes.Contains("cpp_franchise")) { ColumnSet FranchisereturnedAttributes = new ColumnSet(new String[] { "cpp_franchise" }); Entity EntityWithLookupFieldFranchise = service.Retrieve(postImage.LogicalName, postImage_Guid, FranchisereturnedAttributes); var FranchiseLookup_Guid = ((Microsoft.Xrm.Sdk.EntityReference)(EntityWithLookupFieldPrimaryContact.Attributes["cpp_franchise"])).Id; TracingSvc.Trace("Franchise Lookup ID: " FranchiseLookup_Guid);
What am I missing with respect to being able to access the data that’s triggering my plugin by being saved to the cust_rootlookup field?
My plugin pulls data fine from the cust_primarycontactlookup field.
What is the problem with pulling data from the cust_rootlookup field?
How can I access the data in the cust_rootlookup field on update in the scenario detailed above?