In my previous post I showed how using a simple JScript function you can easily show and hide tabs based on the selection of a radio button. In this blog post I will be showing another helpful JScript function: how to prefill fields with default text.
Basically what this means is when you create a new record, this field will be prefilled with some standard text. You can then choose to override the text if you wish. This can prefill the text in a read only field or just a standard editable text field. Using this one function you can set a value in any field, of any type. This includes single line, multiple lines, two options, option sets, whole numbers, currency, and date fields. For each field you wish to prefill, all you need to set is the field to prefill, and what you want it prefilled with.
To start using it, you will first need to create a web resource to hold the JScript function. Create a new Web Resource called ‘prefillField’ and then click on the Text Editor Icon.
Within the text editor, paste in the following JScript:
function prefillField(fieldName, value) {
var isCreateForm = Xrm.Page.ui.getFormType() == 1;
var field = Xrm.Page.getAttribute(fieldName);
if (isCreateForm) {
field.setValue(value);
field.setSubmitMode("always");
}
}
Once you have added the JScript, close the text editor, and then Publish the Web Resource.
To use this now on an entity you need to open the Form Editor for your entity, and add this web resource to the Form Libraries, under Form Properties.
You can then configure a new Event Handler for each field you want prefilled on this form. The event handlers can be added from the Form Properties, below Form Libraries. All you need to do is set is the Library and the Function to ‘prefillField’. Finally you need to set the parameters, containing the field name and default value, e.g:
"new_singleline", "This Field Contains Default Text..."
The parameters format will be slightly different depending on what field type you are prefilling. I’ve added a table below of the different options you have for prefilling fields of different types.
Field Type | Parameters | Example of Value |
Single Line of Text | "fieldname", "value" | Default Text |
Option Set | "fieldname", value | 100000002 |
Two Options | "fieldname", value | true/false -OR- 1/0 |
Whole Number | "fieldname", value | 1000 |
Floating Point Number | "fieldname", value | 1000 |
Decimal Number | "fieldname", value | 1000 |
Currency | "fieldname", value | 1000 |
Multiple Lines of Text | "fieldname", "value" | Default \nText |
Date and Time | These are prefilled differently – which is covered in another post. | |
Lookup | These are prefilled differently – which is covered in another post. |
You can configure as many different event handlers on the same entity to prefill different fields, all you need to do is set the fieldname and value for each one. You can preview the form to see if it works. The values will only be prefilled when a new record is created. In my next blog posts I will show to prefill the Date and Lookup fields.