C# Code · Customization and Configuration · Dynamics Plugins · Functional Info

{Dynamics 365} Set default values from the primary entity while creating new record from C#

When we create a record from Dynamics web application in the context of Parent entity then fields which are defined in relationship mapping will be automatically populated in Child record.

For example – If we create Contact record from Account entity then fields such as Telephone 1 and address field auto populated.

We can do the same thing from our SDK code to create a record in the cootext of parent entity record. As a developer, you can use the InitializeFromRequest class to generate an entity with those default values already set.

//The account that the contact will be associated with:
                var parentAccount = new EntityReference("account", new Guid("1462dec1-eef0-ea11-a817-000d3a8ccd71"));

                // Initialize a new contact entity with default values from the account.
                var requestFormRequest = new InitializeFromRequest()
                {
                    EntityMoniker = parentAccount,
                    TargetEntityName = "contact"

                };

                var responseFormContext = (InitializeFromResponse)cdsClient.Execute(requestFormRequest);
                //Get the Entity from the response
                Entity contact = responseFormContext.Entity;

                // Set values that are not from the account
                contact["firstname"] = "Joe";
                contact["lastname"] = "Smith";

                // Create the contact
                Guid contactid = cdsClient.Create(contact);

The values that will be copied over when a new record is created this way is controlled by configurations applied to the Common Data Service environment, so it can vary between environments.

Map entity fields

Once we run above code from our console application, we should see contact record created with configured mapping fields mapped.

Hope this helps!

One thought on “{Dynamics 365} Set default values from the primary entity while creating new record from C#

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s