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:
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:
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:
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.
The Customer column populated with the correct data from the parent Invoice:
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