Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM forum
Answered

Error accessing lookup field post operation on update via plugin

Posted on by 1,579

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.

 

  1. A D365 Administrator creates a Profile Record in the cust_profile

 

  1. 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.

 

  1. A workflow creates the cust_root record using data from the newly created cust_profile

 

  1. 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?

  • ACECORP Profile Picture
    ACECORP 1,579 on at
    RE: Error accessing lookup field post operation on update via plugin

    You response contained the miracle solution.

    Thank you so much.

  • Verified answer
    PabloCRP Profile Picture
    PabloCRP 1,086 on at
    RE: Error accessing lookup field post operation on update via plugin

    Hi ACECORP,

    If your plugin is executing in cust_profile context then you should get the values from postImage and don't query them

    if (postImage.Contains("cust_primarycontactlookup") && postImage.Contains("cust_rootlookup"))
    {
    var PrimaryContactLookup_Guid = postImage.GetAttributeValue("cust_primarycontactlookup").Id;
    TracingSvc.Trace("Primary Contact Lookup ID: "   PrimaryContactLookup_Guid);
    
    var RootLookup_Guid =postImage.GetAttributeValue("cust_rootlookup").Id; 
    TracingSvc.Trace("Root Lookup ID: "   RootLookup_Guid);
    
    }else
        TracingSvc.Trace("cust_primarycontactlookup or cust_rootlookup is missing");

    then make sure that postImage has cust_primarycontactlookup and cust_rootlookup

    Regards.

    please consider marking as an answer if it was helpful

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,554 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,588 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans