marcguy
ServiceNow Employee
ServiceNow Employee

Today, I have a very interesting requirement from a customer, when logging a change in their current tool, in the 'Affected Environments' field, instead of selecting every environment like, PROD, UAT, DR they had an option to SELECT ALL.

We don't really have that option in a glide_list field in SNC but they wanted to keep that easy, select all functionality and they wanted to be able to report on environments affected, so just using ALL in the glide_list was not an option.

so here is what we done.

On the table that the glide_list referenced (which in this case was a table called ENVIRONMENTS we created a dummy record called ALL.
Then we created an OnChange client script that if 'ALL' was added to the glide_list field, we would grab every record from that table and populate the glide_list field with all environments like UAT, PROD, etc, thus saving them the time of typing every affected environment into the lookup field.

Here's the client script, it uses the callback feature so as not to tie up the client screen too:
We could have used ('name', '!=', 'ALL') in the query but I thought the sys_id of the dummy would be safer and it's easier to grab in the client.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
return;
}
if (newValue.indexOf('28fcbadb1c4894005000f6c7e888e2d7') >= 0)
//see if they have added the all record to the list the long string is the sys_id for the dummy record named ALL
{
var gr = new GlideRecord('u_environments'); //go look on the environments table
gr.addQuery('sys_id', '!=','28fcbadb1c4894005000f6c7e888e2d7'); //query for every record except the dummy ALL record
gr.query(myCallbackFunctionforenvironments); //Execute the query with a callback function so as not to lock the client
}
}
function myCallbackFunctionforenvironments(gr){
var list = new Array();
while (gr.next()) {
list.push(gr.sys_id);
}
g_form.setValue('u_affected_environments',list); //add all records except ALL into the glide_list field
gel('change_request.u_affected_environments_lock').onclick(); //lock the field so it reduces in size on the form
}