Open New Record Form with Pre-populated Fields Using Xrm.Navigation.navigateTo

This post will explain how you can pre-populate fields with various data types (lookup, partylist, etc.) when a new table form is opened using the Xrm.Navigation.navigateTo method.


There is a navigateTo (Client API reference) in the Microsoft Learn but it lacks a code example for the implementation of data object parameter to set the default values to the fields when a form is opened in the create mode. The documentation simply referred to see Set column values using parameters passed to a form which only contains an example to set column values using parameters passed to a form with Xrm.Navigation.openForm.

Setting column values using parameters with Xrm.Navigation.navigateTo is different from Xrm.Navigation.openForm and it could take some trial and error to get it right without the code example or the documentation of expected parameters. With the code example below, I hope you can easily pre-populated any field using Xrm.Navigation.navigateTo method.

 function OpenNewContact(primaryControl)  
 {  
     let formContext = primaryControl; // primaryControl parameter of the command button is same as formContext and NOT executionContext  
   
     // Define the data object with key-value pairs  
     var data = {  
       
         // Text column  
         "subject": "Do people still use fax anymore?",  
   
         // Number column  
         "numberofpages": 3,  
   
         // Yes/No column  
         "directioncode": true,  
   
         // Date column  
         "scheduledend": new Date("2024-03-02 10:54"), // Replace with your desired date  
   
         // Choice column  
         "prioritycode": 2, // Replace with the option value from your Choice options  
   
         // Lookup column (single)  
         "ownerid": {  
             "entityType": "team",  
             "id": "91a4e4ab-8c67-eb11-a812-000d3a6a350f",  
             "name": "Test Owner Team"  
         },  
   
         // Lookup column (multiple)  
         "to": [  
         {  
             "entityType": "account",  
             "id": "6e060750-ab16-eb11-a812-000d3a6aa8dc",  
             "name": "A. Datum Corporation (sample)"},  
         {  
             "entityType": "contact",  
             "id": "cc139876-a2f3-4308-829e-e5599a566142",  
             "name": "Jim Glynn (sample)"}]  
     };  
   
     // Define the parameters for the form  
     var pageInput = {  
         pageType: "entityrecord",  
         entityName: "fax", // Replace "fax" with your target entity logical name  
         data: data,  
         formId: "fa741227-f468-4864-b2a1-e0379d387e71", // Replace with your desired form ID or remove the parameter if there is only one form  
         createFromEntity: { // This will populate the lookup to the current record e.g. regardingobjectid and other mapping fields  
             "entityType": formContext.data.entity.getEntityName(),  
             "id": formContext.data.entity.getId(),  
             "name": formContext.data.entity.getPrimaryAttributeValue()  
         }  
     };  
   
     // Define the navigation options  
     var navOptions = {  
         target: 2,  
         height: {value: 80, unit:"%"},  
         width: {value: 70, unit:"%"},  
         position: 1  
     };  
   
     // Perform the navigation  
     Xrm.Navigation.navigateTo(pageInput, navOptions).then(  
         function success(result) {  
             Xrm.Navigation.openAlertDialog(  
             {  
                 text: "Record created with ID: " + result.savedEntityReference[0].id + " Name: " + result.savedEntityReference[0].name  
             });  
             // Handle dialog closed  
         },  
         function (error) {  
             Xrm.Navigation.openAlertDialog(  
             {  
                 text: error.message  
             });  
         }  
     );  
 }  


Thanks Arjun Musuvathy for the suggestion and motivation to come up with this post.

Comments

  1. What would be a requirement / use case for this?

    ReplyDelete
    Replies
    1. Based on my experience, most of the use cases are around opening a new record form dialog on click of the custom command button and populate the fields dynamically (some of which cannot be achievable with field mapping).
      E.g. Open a draft email form to all Approvers (Approver 1 to Approver 5) of the case
      - Clone the current record by opening the new form as a dialog and populate all fields
      - Custom subgrid + Add New button to open the new form as a dialog and populate complex logic field population which cannot be achieved via field mapping

      Delete
    2. Thank you! I would like to try this, do you have any examples which can’t be achieved by mapping but can be achieved by this?

      Delete
    3. If the size of data is greater than the url length, it fails to send the complete data with this approach, if the length of the data is less than the url length it works…

      Delete
    4. In terms for "examples which can’t be achieved by mapping but can be achieved by this", I'd say all of the examples in my comment above especially opening the draft email with populating some lookup values as recipients on the email.

      Delete
    5. If I understand correctly, the URL length only impacts setting column values using extraqs parameter in the URL address.
      https://learn.microsoft.com/en-us/power-apps/developer/model-driven-apps/set-field-values-using-parameters-passed-form#pass-parameters-to-set-column-record-values

      For this Xrm.Navigation.navigateTo method, I tried to set the muliline text column with the data of more than 1 million characters and it is still working.

      Delete

Post a Comment

Popular Posts