Open new form with fields pre-populated using Xrm.Navigation in D365
Introduction:
Xrm.Navigation provides navigation related methods like openAlertDialog, openConfirmDialog, openErrorDialog, openFile etc. In this blog we will be discussing about openForm method which opens an entity form or a quick create form.
There might be requirements to open a new form with certain fields pre-populated on certain conditions or to open a specific record.
Implementation:
Step 1:
Syntax:Xrm.Navigation.openForm(entityFormOptions,formParameters).then(successCallback, errorCallback)
Here the parameter entityFormOptions is an Object and is a required parameter.
Step 2:
In the below example taken, we have written a script on the contact form to trigger on Save which will open up the quick create form with values filled in some of the fields.
As we can see in the below image we have passed the execution context as the first parameter.
Step 3:
The code for the same is given below:
scripting={ myfunction:function(executionContext){ debugger; var formContext = executionContext.getFormContext(); var entityFormOptions = {}; entityFormOptions["entityName"] = "contact"; entityFormOptions["useQuickCreateForm"] = true; var formParameters = {}; formParameters["firstname"] = formContext.getAttribute("firstname").getValue(); formParameters["lastname"] = formContext.getAttribute("lastname").getValue(); formParameters["fullname"] = formContext.getAttribute("fullname").getValue(); formParameters["jobtitle"] = "Developer"; Xrm.Navigation.openForm(entityFormOptions, formParameters).then( function (success) { //Any Specific action to be performed console.log(success); }, function (error) { //Any specific action to be performed console.log(error); }); } };
Step 4 :
After updating the value and saving the form a quick create form opens up with some of the fields filed out. We get the value of the fields using the formContext.getAttribute(“fieldname”).getValue();
The successCallback and errorCallback are optional. In the Unified Interface the successCallback and the errorCallback function will be executed only if the we are opening the quick create form.
Step 5:
As seen in the image below we make some changes to the contact form and save the changes. After saving a new contact quick create form opens up with the first name, last name and job title fields filled.
Here in our case we have specified entityFormOptions[“useQuickCreateForm”] = true; If this property is not set a normal form open’s up with the values filled in the fields in the same window. If we want to open the new record in a new window we should specify entityFormOptions[“openInNewWindow”]= true which is a Boolean value and it is an optional field.
Step 6:
If we want to open a specific record we just specific the following
entityFormOptions[“entityName”]=”account” entityFormOptions[“entityId”]=”Specify Guid Here”
Hope this helped!