In servicenow i have a requirement that in alm_hardware table i put value in assigned to reference field and based on the assigned to reference field, i need to populate the bu head reference field. My condition is that whenever i filled the assigned to field , on change script will run and check the assigned to users department on sys user table and on the basis of department check the business unit on cmn department table and based on business unit check for bu head on business unit table. Assigned to field is reference to user table, department field is also reference to cmn department table and business unit is also reference to business unit table and at last bu head is reference field to sys user table. Bu head is my reference field in alm_hardware table refer to sys user table
Assistant
User
Are we using glide record in client script??
Assistant
To achieve your requirement in ServiceNow, you need to use a client script that triggers whenever the "Assigned to" field changes. This script should perform a series of GlideRecord queries to fetch the related Business Unit Head based on several reference fields' relationships.
Here's a step-by-step approach to achieve this:
Create the Client Script:
Navigate to System Definition > Client Scripts.
Click on New.
Configure the Client Script:
Set the Table to alm_hardware.
Set the Type to onChange.
Specify the Field Name to assigned_to.
Ensure that the Inherit checkbox is unchecked.
Write the Script:
Here’s an illustrative script to guide you:
javascript
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
// Initialize GlideRecord for the sys_user table to get the assigned to user's department
var userGR = new GlideRecord('sys_user');
if (userGR.get(newValue)) {
var departmentSysId = userGR.department;
// If departmentSysId exists, proceed to next step
if (departmentSysId) {
// Initialize GlideRecord for the cmn_department table to get the business unit
var departmentGR = new GlideRecord('cmn_department');
if (departmentGR.get(departmentSysId)) {
var businessUnitSysId = departmentGR.u_business_unit; // assuming 'u_business_unit' is the field that holds the business unit reference
// If businessUnitSysId exists, proceed to next step
if (businessUnitSysId) {
// Initialize GlideRecord for the business_unit table to get the BU head
var businessUnitGR = new GlideRecord('business_unit');
if (businessUnitGR.get(businessUnitSysId)) {
var buHeadSysId = businessUnitGR.u_bu_head; // assuming 'u_bu_head' is the field that holds the BU head reference
// If buHeadSysId exists, set the BU head field on the form
if (buHeadSysId) {
g_form.setValue('bu_head', buHeadSysId);
}
}
}
}
}
}
}
Important points to note:
Replace the assumed field names (u_business_unit, u_bu_head) with actual field names in your instance.
This script uses direct GlideRecord queries in a client script which might not be the best practice in all scenarios due to potential performance issues and Security Access Control concerns. However, for simple and less frequent operations, this can work fine.
Consider using asynchronous calls if the reference tables have large data sets, to avoid UI freezing.
Test the Script:
Test the client script thoroughly to ensure it captures data correctly and updates the bu_head field as expected.
Validate for scenarios where data might be missing or departmental references are updated.
Publish the Client Script:
Once tested successfully, save and activate the client script.
The above steps will automate the process of populating the BU Head field based on the Assigned To user's department and business unit.
You are correct. My previous response used GlideRecord in a client script, which is not a recommended practice, nor is it supported directly in client-side scripts due to security and performance constraints. Instead, you should use GlideAjax in combination with a Script Include to securely perform server-side operations.
Here's how to correctly implement this in a client-side script using GlideAjax:
Create the Script Include:
Navigate to System Definition > Script Includes.
Click on New.
Configure the Script Include:
Name the Script Include, for example, GetBUHead.
Ensure the API Name is set correctly (e.g., GetBUHead).
Set the Accessible from field to All application scopes.
Check the Client callable checkbox to make it accessible from client scripts.
Here is an example of the Script Include:
javascript
var GetBUHead = Class.create();
GetBUHead.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getBUHead: function(userSysId) {
// Initialize GlideRecord for the sys_user table to get the assigned to user's department
var userGR = new GlideRecord('sys_user');
if (userGR.get(userSysId)) {
var departmentSysId = userGR.department;
// If departmentSysId exists, proceed to next step
if (departmentSysId) {
// Initialize GlideRecord for the cmn_department table to get the business unit
var departmentGR = new GlideRecord('cmn_department');
if (departmentGR.get(departmentSysId)) {
var businessUnitSysId = departmentGR.u_business_unit; // replace with actual field name
// If businessUnitSysId exists, proceed to next step
if (businessUnitSysId) {
// Initialize GlideRecord for the business_unit table to get the BU head
var businessUnitGR = new GlideRecord('business_unit');
if (businessUnitGR.get(businessUnitSysId)) {
var buHeadSysId = businessUnitGR.u_bu_head; // replace with actual field name
// If buHeadSysId exists, return it
if (buHeadSysId) {
return buHeadSysId;
}
}
}
}
}
}
return '';
}
});
Create the Client Script:
Navigate to System Definition > Client Scripts.
Click on New.
Configure the Client Script:
Set the Table to alm_hardware.
Set the Type to onChange.
Specify the Field Name to assigned_to.
Ensure that the Inherit checkbox is unchecked.
Here’s how to write the client script using GlideAjax:
javascript
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
// Create a GlideAjax instance and configure the Script Include
var ga = new GlideAjax('GetBUHead');
ga.addParam('sysparm_name', 'getBUHead');
ga.addParam('userSysId', newValue);
// Get the result asynchronously
ga.getXMLAnswer(function(response) {
var buHeadSysId = response;
// If a BU Head was returned, set it on the form
if (buHeadSysId) {
g_form.setValue('bu_head', buHeadSysId);
} else {
g_form.clearValue('bu_head');
}
});
}
Steps to Test and Deploy:
Test the client script thoroughly to ensure it works as expected in different scenarios.
Activate and save both the client script and the Script Include after successful testing.
The usage of GlideAjax ensures that the heavy-lifting happens on the server-side, promoting better performance and security.