XRM Web API | Dynamically Retrieve and Set Customer Field in D365 CE (CRM)

In the Microsoft Dynamics 365 CRM solution (otherwise known as CE), the Power Platform provides the Customer column which is a dual-purpose field that can be one of two tables in the Dataverse, and must be handled differently when setting the value in client scripting. A business requirement can surface where the Customer column needs to be set with client scripting in a JavaScript Web Resource. You cannot set the Customer column like you can other lookups like Accounts, Contacts, or Opportunities; the Customer column is unique in that it can be either an Account or a Contact reference.

Solution

The below code snippets and scenario follows a business requirement to clone a record that is initiated from a custom button. Through the Xrm.WebApi methods, the Invoice data will be retrieved and then create a new Invoice. It will then open the newly created record and refresh the buttons on the ribbon.

Retrieving Data Via Xrm.WebApi.retrieveRecord

Since the inception of the Xrm.WebApi object, methods to retrieve or set data is different with CRM client scripting. Here is the Microsoft documentation on the retrieveRecord method. For this particular scenario, the code needs the ID of the current record to retrieve all information of that record, which may or may not be on the form.

XRM

Once the ID of the record is established, we call the retrieveRecord method, passing the logical name of the table and passing in the record ID. From there, we pass the responsibility to a different function, named createInvoice.

XRM

The createInvoice function will set certain variables and then create a brand new record. Here is the skeleton structure of the function:

XRM

Retrieving Non-Customer Lookup Values

To provide a fuller explanation of lookups in general, I will add instructions on how to retrieve and set values of lookup columns. Below is an example of how to retrieve the ID of an Opportunity:

XRM

Notice the column is prefaced with an underscore, and it is suffixed by _value. opportunityid is the logical name of the column.

Retrieving Customer Values

Retrieving a Customer value is the same as retrieving data from any other lookup in that the column name is surrounded with the needed information like this:

XRM

Setting Non-Lookup Values

Setting column values in client scripting is different, depending on the column type. With a text column, below is a good example on how to set the value:

Method 1

XRM

Method 2

XRM

Setting Lookup Values

With lookup columns, we need to add more data to the name and value pair. The example below is setting the opportunity ID of the lookup:

Method 1

XRM

Method 2

XRM

opportunityid is the name of the column on the specified table (Invoice):

XRM

opportunities is the plural name of the table in which the field points:

XRM

Setting Customer Value

Now we will cover the most important point of the blog, as everything previously mentioned provided a base understanding of the concepts.

Because the Customer column can be either an Account or Contact, we need to factor that into our coding strategy with a Customer Type like this:

XRM

The above code is retrieving another piece of information that we will use to determine how we will set the Customer value.

For our scenario, it will produce one of two results:

“_customerid_value@Microsoft.Dynamics.CRM.associatednavigationproperty”: “customerid_account”

OR

“_customerid_value@Microsoft.Dynamics.CRM.associatednavigationproperty”: “customerid_contact”,

We assign it to the customer_type variable and then assign the Customer value to the newly created record like this:

XRM

Adding in all the other code (contact us for code), we are now ready to test it. When the custom button is clicked, a new record will be created with the Customer lookup populated.

Custom Button

XRM

The Customer column populated with the correct data from the parent Invoice:

XRM

Please contact us for the full code to go along with this solution. If you have any questions about this topic or D365 CE (CRM) please get in touch with us.

Dynamics 365 CE (CRM) How-To eGuide

41 pages of step-by-step instructions for 6 different key tasks in Dynamics 365 Customer Engagement (CRM). Includes interactions with PowerApps and Flow!

Get the eGuide

Dynamics 365 CE (CRM) How-To eGuide

Get the eGuide