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

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Create Child Records in Pre-Operation Plugin

(0) ShareShare
ReportReport
Posted on by 28,983 Moderator

Hi All,

Recently while going through OrganizationService SDK Doc, I came across RelatedEntities concept where we can create Parent and Child records in one operation. 

So I wanted to check this in my Pre-Operation plugin to see if it works. Requirement is to Create one Contact and 3 tasks when Account is created. I wrote below C# and registered plugin step on Create - Pre Operation stage. But none of the child records are getting created.

if (entity.LogicalName == "account")
                {
                    // An accountnumber attribute should not already exist because
                    // it is system generated.
                    if (entity.Attributes.Contains("accountnumber") == false)
                    {
                        // Create a new accountnumber attribute, set its value, and add
                        // the attribute to the entity's attribute collection.
                        Random rndgen = new Random();
                        entity.Attributes.Add("accountnumber", rndgen.Next().ToString());

                        // Create Primary contact
                        var primaryContact = new Entity("contact");
                        primaryContact["firstname"] = "John";
                        primaryContact["lastname"] = "Smith";

                        // Add the contact to an EntityCollection
                        EntityCollection primaryContactCollection = new EntityCollection();
                        primaryContactCollection.Entities.Add(primaryContact);

                        // Set the value to the relationship
                        entity.RelatedEntities[new Relationship("account_primary_contact")] = primaryContactCollection;

                        // Add related tasks to create
                        var taskList = new List() {
                                       new Entity("task") { ["subject"] = "Task 1" },
                                       new Entity("task") { ["subject"] = "Task 2" },
                                       new Entity("task") { ["subject"] = "Task 3" }
                        };

                        // Add the tasks to an EntityCollection
                        EntityCollection tasks = new EntityCollection(taskList);

                        // Set the value to the relationship
                        entity.RelatedEntities[new Relationship("Account_Tasks")] = tasks;
                    }
                    else
                    {
                        // Throw an error, because account numbers must be system generated.
                        // Throwing an InvalidPluginExecutionException will cause the error message
                        // to be displayed in a dialog of the Web application.
                        throw new InvalidPluginExecutionException("The account number can only be set by the system.");
                    }
                }

I can achieve this with Post Operation plugin but wanted to reduce service calls to CRM Dynamics.

Please help me to understand why this is not creating any records. I don't get any error, it seems CRM system is ignoring child record code.

FYI - Account number is getting auto populated when I create New Account.

I have the same question (0)
  • Suggested answer
    Steven RH JIANG Profile Picture
    on at
    RE: Create Child Records in Pre-Operation Plugin

    Hi Bipin,

    It's expected behavior. In the execution pipeline, pre-operation occurs before core-operation, and finally post-operation.

    When you tried to create child records, you need to make sure parent record exists firstly. Parent record would be created at core-operation stage, and in your pre-operation, you cannot access it yet.

  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at
    RE: Create Child Records in Pre-Operation Plugin

    Hello,

    Thank you for your reply.

    If you see my code, I am not creating any record as such. I am just adding RelatedEntity to the same Target Entity object. I am not specifically using create sdk message to create child records in Dynamics.

    As part of core operation, entire Entity object should passed to the web service and it should create first Parent entity record and then child entity record.

    More details - docs.microsoft.com/.../entity-operations-create

  • Verified answer
    Steven RH JIANG Profile Picture
    on at
    RE: Create Child Records in Pre-Operation Plugin

    Hi Bipin,

    In the example article, it's passing the entire entity object to svc.Create(), including all related entities. So CRM server will create account and related records.

    But in the plugin execution pipeline, it's a different story.

    1. Firstly, CRM server received a request to create an account record

    2. So you can update this record at pre-operation stage, but just the attributes of that record

    3. At the core-operation stage, CRM server will create this account record accordingly. Other data are invisible, and will be ignored.

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 69 Super User 2025 Season 2

#2
CA Neeraj Kumar Profile Picture

CA Neeraj Kumar 61

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 54 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans