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

Linn Zaw WinDyn365CE1 year ago15 Views

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.

Original Post http://linnzawwin.blogspot.com/2024/03/open-new-record-form-with-pre-populated.html

0 Votes: 0 Upvotes, 0 Downvotes (0 Points)

Leave a reply

Follow
Sign In/Sign Up Sidebar Search
Loading

Signing-in 3 seconds...

Signing-up 3 seconds...