Andrew Barnes

3 minute read

Share Spotlight

Welcome to the second share spotlight! Every Friday we will be spotlighting a project from the Share site on the Developer Portal.

Fill Mandatory Fields

For the next installment, we are featuring a project called Fill Mandatory Fields authored by Vijaya Sadasivuni. This project adds one Global UI Action to add some quality of life adjustments for ServiceNow Developers specifically. It solves a very handy need for developers while they are testing - filling in all the mandatory fields for a given record. It does just that one thing, but oh, it does it so well :D. I can’t tell you how much of my life was spent just selecting templates and building templates for change request form processes to fill all the required fields.

Mandatory UI Action

Since this share item only does just the one thing, I figured I would dive into the guts of how it operates and share it with you. The UI Action starts by collecting all the fields and table that need filling out on the form.

var fields = g_form.getMissingFields();
var tableName = g_form.getTableName();

Next, we send these fields to a GlideAjax script to get the appropriate values for each field’s type. You need different filler data for a reference field as you do for a date field. This is ended with a call back function.

var ga = new GlideAjax('MandatoryScript');

As you can see from above, we need to send the script all the information it needs to get us the response we need. We will return to the UI Action script once we have some values to return. This Ajax call is important as it allows us to invoke server-side functions asynchronously so that the user is not stuck. Let’s go take a look at the Script Include that is being called.

var tableName = this.getParameter('sysparm_tableName');
var fieldNames = this.getParameter('sysparm_fieldNames');
fieldNames = fieldNames.split(",");
var dataVal = [];
for(var i=0;i<fieldNames.length;i++){

This section is breaking up each mandatory field and performing a loop over them. One interesting note is for the reference field solution. You can see the script is querying the target reference table and getting an active result so that the data is valid on the form. You will note the use of setLimit(1) to get just one record.

var getRef = new GlideRecord(refTbl);

Another interesting thing of note is the use of properties which are configurable, and thus future proof.

var mand_datetime= gs.getProperty('mandatory.datetime.value');

Now that we have the values for filling in all the mandatory fields, we will return to the UI Action to populate them on the form.

return JSON.stringify(dataVal);

We are sending back a JSON object as you can see above, and thus must process that to use the response in the code below.

var answer = response.responseXML.documentElement.getAttribute("answer");
answer = JSON.parse(answer);

Then we need to loop through our field value pairs and populate the form with setValue. Once all the fields are populated, we exit the callback function and are done and all mandatory fields are now filled in, and the form is ready for submission.

Thank you for this share item Vijaya. I will be adding it to my kit of items to install on new instances for myself. Please let me know if you enjoyed the walk-through of the share items code via this community thread.

Keep an eye on this space as every Friday we will provide a spotlight to another project on the Share site we think is worthy of attention. Have fun exploring!