In this blog post, I will show you how to set field values using parameters passed to a form in Dynamics 365 using JavaScript. This is a useful feature that allows you to set default values for new records created by users by specifying values in the URL that is used to open the form. By default, these values are set in the form, but can be changed by users before they save the record.
Pass parameters to set column record values
To pass parameters to set field values, you need to use the following syntax in the URL:
https://{organizationurl}/main.aspx?etn=&pagetype=entityrecord&extraqs=
where is the name of your organization, is the name of the entity for which you want to create a new record, and is a string of key-value pairs separated by ampersands (&).
For example, if you want to create a new account
record and set the name
and telephone1
columns, you can use the following URL:
https://contoso.crm.dynamics.com/main.aspx?etn=account&pagetype=entityrecord&extraqs=name%3DContoso%26telephone1%3D123-456-7890
where name and telephone1 are the logical names of the columns, and %3D
is the URL-encoded character for =
. Note that you need to URL-encode any special characters in the parameter values.
Set the value for lookup columns
To set the value for lookup columns, you need to pass the GUID (Globally Unique Identifier) of the related record as well as the type and name of the lookup column. For example, if you want to create a new contact
record and set the parentcustomerid
column, you can use the following URL:
https://contoso.crm.dynamics.com/main.aspx?etn=contact&pagetype=entityrecord&extraqs=parentcustomerid%3D%7B9A5C8F8F-4E9E-E911-A81E-000D3A1A9402%7D%26parentcustomeridtype%3Daccount%26parentcustomeridname%3DContoso
where parentcustomerid is the logical name of the lookup column, %7B
and %7D
are the URL-encoded characters for {
and }
, and parentcustomeridtype
and parentcustomeridname
are optional parameters that specify the type and name of the related entity.
Set the value for date columns
To set the value for date columns, you need to pass the date value in ISO 8601 format (YYYY-MM-DDTHH:MM:SSZ)
. For example, if you want to create a new task record and set the scheduledend
column, you can use the following URL:
https://contoso.crm.dynamics.com/main.aspx?etn=task&pagetype=entityrecord&extraqs=scheduledend%3D2020-01-31T23:59:59Z
where scheduledend is the logical name of the date column.
Set the value for choice columns
To set the value for choice columns, you need to pass the integer value of the option that you want to select. For example, if you want to create a new case record and set the priority
column, you can use the following URL:
https://contoso.crm.dynamics.com/main.aspx?etn=incident&pagetype=entityrecord&extraqs=prioritycode%3D1
where prioritycode
is the logical name of the choice column, and 1
is the value of the High
option.
Use Xrm.Navigation.openForm to open a new window
Another way to set column values using parameters passed to a form is to use the Xrm.Navigation.openForm
method in client API. This method allows you to open a new window with a form and pass an object with parameters as an argument. For example, if you want to open a new window with a contact
form and set some column values, you can use the following code:
var parameters = {};
parameters["firstname"] = "John";
parameters["lastname"] = "Doe";
parameters["emailaddress1"] = "johndoe@example.com";
parameters["parentcustomerid"] = "9A5C8F8F-4E9E-E911-A81E-000D3A1A9402";
parameters["parentcustomeridtype"] = "account";
parameters["parentcustomeridname"] = "Contoso";
var entityFormOptions = {};
entityFormOptions["entityName"] = "contact";
entityFormOptions["useQuickCreateForm"] = true;
Xrm.Navigation.openForm(entityFormOptions, parameters).then(
function (success) {
console.log(success);
},
function (error) {
console.log(error);
}
);
This code will open a new window with a quick create form for contact and set the values for the columns specified in the parameters object. Note that you need to use the GUID of the related record for lookup columns, and you can optionally specify the type and name of the related entity.
Conclusion
In this blog post, I explained how to set column values using parameters passed to a form in Dynamics 365 using client API. This feature can help you make data entry smoother and improve the user experience. You can use either the URL syntax or the Xrm.Navigation.openForm
method to do this. I hope you found this post helpful. If you have any questions or feedback, please leave a comment below. Thank you for reading!
For more in-depth insights and tutorials on Programming or Dynamics 365 features and customization, check out our posts to further enhance your proficiency and maximize the potential of your skills.
Leave a comment