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.

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.

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

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:

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:

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

Method 2

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

Method 2

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

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

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:

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:

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

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

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.
41 pages of step-by-step instructions for 6 different key tasks in Dynamics 365 CRM apps. Includes interactions with Power Apps and Power Automate!