Microsoft Dynamics CRM 2013: Populating Required Fields with Bookmarklets

Ahmed Anwar, 08 December 2014

I definitely reckon you that you had to fill all required fields every time you test a small piece of code on an entity form, right? For example, you may have added some JavaScript code to the onSave event of a form and when it comes to testing, you need to manually fill in all of the required fields before pressing save.

To avoid this repetitive effort, I have developed two bookmarklets to populate all required fields on a form automatically. I have created a custom entity that contains many different types of fields in order to demonstrate the bookmarklets in action.

Dynamics CRM 2013 Populating Required Fields with Bookmarklets

Clicking on the Populate Min bookmarklet fills out numeric values on the form with their minimum values.

Dynamics CRM 2013 Populating Required Fields with Bookmarklets

Dynamics CRM 2013 Populating Required Fields with Bookmarklets

Alternatively, clicking on the Populate Max bookmarket fills out the numeric fields on the form with their maximum values.

Dynamics CRM 2013 Populating Required Fields with Bookmarklets

Note: string, multi-text and Date/Time fields have neither min nor max values, so I decided to use static values. Lookup fields have not been handled as is it is not simple to specify default values.
For more information about how to create your own bookmarklets, please read this article written by Jared Johnson.

Populate Min:
javascript:var form = $("iframe").filter(function () { return $(this).css("visibility") == "visible" })[0].contentWindow;var attrs = form.Xrm.Page.data.entity.attributes.get();for (var i in attrs) {    var attr = attrs[i];    var contrs = attr.controls.get();    if (attr.getRequiredLevel() == 'required') {        switch (attr.getAttributeType()) {            case 'memo': { attr.setValue('memo'); break; }            case 'string': { attr.setValue('string'); break; }            case 'boolean': { attr.setValue(false); break }            case 'datetime': { var today = new Date(); attr.setValue(today); break; }            case 'decimal': { attr.setValue(attr.getMin()); break; }            case 'double': { attr.setValue(attr.getMin()); break; }            case 'integer': { attr.setValue(attr.getMin()); break; }            case 'lookup': { attr.setValue(0); break; }            case 'money': { attr.setValue(attr.getMin()); break; }                       case 'optionset': {                var options = attr.getOptions();                attr.setValue(options[0].value);            }                  }    }}

Populate Max:
javascript: var form = $("iframe").filter(function () { return $(this).css("visibility") == "visible" })[0].contentWindow;var attrs = form.Xrm.Page.data.entity.attributes.get();for (var i in attrs) {    var attr = attrs[i];    var contrs = attr.controls.get();    if (attr.getRequiredLevel() == 'required') {        switch (attr.getAttributeType()) {            case 'memo': { attr.setValue('memo'); break; }            case 'string': { attr.setValue('string'); break; }            case 'boolean': { attr.setValue(false); break }            case 'datetime': { var today = new Date(); attr.setValue(today); break; }            case 'decimal': { attr.setValue(attr.getMax()); break; }            case 'double': { attr.setValue(attr.getMax()); break; }            case 'integer': { attr.setValue(attr.getMax()); break; }            case 'lookup': { break; }            case 'money': { attr.setValue(attr.getMax()); break; }            case 'optionset': {                var options = attr.getOptions();                attr.setValue(options[0].value);            }        }    }}