Getting Related Record Data on ServiceNow Forms
Sometimes a case comes up where you need to lookup further information on the active record, via is referenced records. ServiceNow makes it pretty painless with the g_form.getReference method, which uses a callback to keep from blocking the rest of your script.
The example I have below, and on our GitHub repo, uses getReference in a Client Script as a way to notify uses if they are setting the owner of a CI as an inactive user. It's not looking to do anything flashy, just help reduce the number of CI's that are assigned to inactive users which might not even work with the company anymore.
Setting up the Client Script
The script calls g_form.getReference on the assigned_to field, and the callback checks to see if the user is active. If the user is inactive, we add a field message to notify the UI
function onChange(control, oldValue, newValue, isLoading, isTemplate) {if (isLoading || newValue === '') {return;}var assignee = g_form.getReference('assigned_to',checkUserStatus);// call back to notify on the case of inactive usersfunction checkUserStatus(assignee){if(assignee.active === "false"){var msg = assignee.first_name + ' ' + assignee.last_name + '[' + assignee.email +'] is not an active user';g_form.showFieldMsg('assigned_to',msg,'error');}}}
The resulting UI
As you can see in the screen shots below, ServiceNow lets the end-user know that they have selected an inactive assignee for the Configuration Item.