web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

Deep Insert - Parent record lookup column is Null in Plugin

(0) ShareShare
ReportReport
Posted on by 307

Dear Experts,

We have 2 tables A and B. A -->Parent and B-->Child. We are doing Deep Insert method and creating A and B records.

We have plugins On create of B (child) and Plugins gives null value for Parent lookup column. Is this know issue in Deep insert? Any solution please.

Thanks in Advance.

I have the same question (0)
  • Verified answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    Hi,

    Platform does not set lookup of parent entity on child entity while create. Instead system will update the lookup field in separate call. To verify my statement I use below code to Create Employee and Building using deep insert code -

    // Use Entity class with entity logical name

                   var account = new Entity("msft_employee");

                   // Set attribute values

                   // string primary name

                   account["msft_employeenumber"] = "Sample Employee";

                   // Create Building

                   var primaryContact = new Entity("bc_building");

                   primaryContact["bc_name"] = "Test Plugin";

                   // Add the contact to an EntityCollection

                   EntityCollection primaryContactCollection = new EntityCollection();

                   primaryContactCollection.Entities.Add(primaryContact);

                   // Set the value to the relationship

                   account.RelatedEntities[new Relationship("cr127_bc_Building_Employee_msft_Employee")] = primaryContactCollection;

                   // Create the account

                   Guid accountid = crmSvc.Create(account);

    Now when I look at Audit history of Child Entity, I see two record -

    One for Create with all field for Building Entity(Child)

    Another for Update to set the lookup field.

    CR106.PNG

    Please mark my answer verified if this is helpful!

    Regards,

    Bipin Kumar

    Follow my Blog: xrmdynamicscrm.wordpress.com/

  • Seenivasan M Profile Picture
    307 on at

    Dear Bipin Kumar,

    Thanks for your response.

    You are correct. I mark your answer as verified.

     I don't know why they would create the record and then do the update afterwards. Its not useful when we have plugin validation. We need to change all plugins to Update message again. So Deep insert is not useful when we use Dynamics 365 WEB API. I am disappointed with this feature.

    Thanks,

    Seeni

  • Haw_Isaac Profile Picture
    4 on at
    I was able to implement a check that I believe will work for the on create messages to check to see if it was part of a deep insert or not.
     
    In my plugin code that runs on create that checks to see if the parent lookup exists, I check the parent context. If the parent context PrimaryEntityName = the parent entity I am deep inserting from and the MessageName = Create then I am skipping the requirement. You could potentially also update the Target here with a reference built from the parent context if it matters down stream as you have access to the Id and Entity Name. Note, you may have to check higher up than just the ParentContext, such as the ParentContext.ParentContext to get to your top level entity. I created a function on my custom base class called "GetTopParentContext" and then use the result to check to see if it was a create of the parent entity that triggered the plugin.
    if (target.parentRef == null)
    {
        var topParentContext = customBaseContext.GetTopParentContext();
        if (topParentContext?.PrimaryEntityName == "new_parententity" && topParentContext?.MessageName == PluginMessageNames.Create)
        {
            parentId = topParentContext.PrimaryEntityId.ToString();
        }
        else {
            throw new InvalidPluginExecutionException("Parent Lookup Cannot Be Blank.");
        }
        
    }
     

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 74 Super User 2025 Season 2

#2
Daniyal Khaleel Profile Picture

Daniyal Khaleel 32 Most Valuable Professional

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 31 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans