In servicenow i wrote this business rule to populate the value in string field but dot…
- User
In servicenow i wrote this business rule to populate the value in string field but dot walking through all the reference fields . But it is not working, please fix this......(function executeRule(current, previous /null when async/) 1 var assignedTo - current.assigned_to; var dept; if (assignedTo) var userGR = new GlideRecord('sys_user') userGR.addQuery('sys_id', assignedTo.sys_id); userGR.query(; if (userGR.next() f dept = userGR.getValue('department'); if (dept) var deptGr = new GlideRecord( 'cmn_department'); deptGr.addQuery('name', dept); deptGr.query(; if (deptGr.next()) var bussinessUnit = deptGr.getValue('business_unit'); if (bussinessUnit) f var bu = new GlideRecord('business_unit'); bu.addQuery('name', bussinessUnit); deptGr.query (; if (bu.next() ( var buHead = bu.getValue("bu_head") current.u_bu_head = buHead,toString();
- Assistant
It looks like the code you have provided has several syntax and structural issues. Below is a corrected version of your business rule script that follows best practices for GlideRecord operations along with proper syntax.
(function executeRule(current, previous /*null when async*/) { var assignedTo = current.assigned_to; var dept; if (assignedTo) { var userGR = new GlideRecord('sys_user'); userGR.addQuery('sys_id', assignedTo.sys_id); userGR.query(); if (userGR.next()) { dept = userGR.getValue('department'); if (dept) { var deptGr = new GlideRecord('cmn_department'); deptGr.addQuery('sys_id', dept); deptGr.query(); if (deptGr.next()) { var businessUnit = deptGr.getValue('business_unit'); if (businessUnit) { var bu = new GlideRecord('business_unit'); bu.addQuery('sys_id', businessUnit); bu.query(); if (bu.next()) { var buHead = bu.getValue("bu_head"); current.u_bu_head = buHead; // Assuming 'u_bu_head' is a field in the current record } } } } } } })(current, previous);Explanation:
- Variable Declaration and Initialization: Start by creating and initializing the
assignedTovariable fromcurrent.assigned_to. - Check for assignedTo: Ensure
assignedTois valid/exists before performing further operations. - User GlideRecord Query:
- Create a new instance of
GlideRecordfor thesys_usertable. - Query the
sys_usertable for the specificassigned_to.
- Create a new instance of
- Retrieve Department: If the user record is found, get the value for
department. - Department GlideRecord Query:
- Create a new instance of
GlideRecordfor thecmn_departmenttable. - Query for the specific department.
- Create a new instance of
- Retrieve Business Unit: If the department record is found, get the value for
business_unit. - Business Unit GlideRecord Query:
- Create a new instance of
GlideRecordfor thebusiness_unittable. - Query for the specific business unit.
- Create a new instance of
- Retrieve Business Unit Head: If the business unit record is found, get the value for
bu_headand set it tocurrent.u_bu_head.
Make sure that field names like
u_bu_head,business_unit, and other custom fields exist or adjust these according to your data model in ServiceNow. Note that thetoString()method is generally unnecessary for setting a string field in ServiceNow, asgetValuealready returns a string. - Variable Declaration and Initialization: Start by creating and initializing the
- User
I wrote this client script in servicenow , but it shows undefined in string field function onChange(control, oldValue, newValue, isloading, isTemplate) f if (isloading || newValue === return; if (gform.getValue('install_status') == 2 |l g_form.getValue('install_status') =6 || g_form.getValue(install_ status') -- 9) return; g_form.getReference('department', setBuHead); function setBuHead(assigned) if (assigned g_form.setValue('u_bu_head', assigned.business_unit.bu_head);
- Assistant
Your script has syntax errors and a few logical issues that need correction. Here’s a revised version of your client script that should work as expected:
function onChange(control, oldValue, newValue, isLoading, isTemplate) { if (isLoading || newValue === '') { return; } // Check the value of the 'install_status' field var installStatus = g_form.getValue('install_status'); if (installStatus == 2 || installStatus == 6 || installStatus == 9) { return; } // Get the reference data for the 'department' field g_form.getReference('department', setBuHead); // Function to set the BU Head based on department's business unit function setBuHead(department) { if (department && department.business_unit) { g_form.getReference('business_unit', setBuHeadFromBusinessUnit); } else { g_form.setValue('u_bu_head', ''); } } // Function to set the BU Head based on business unit function setBuHeadFromBusinessUnit(businessUnit) { if (businessUnit && businessUnit.bu_head) { g_form.setValue('u_bu_head', businessUnit.bu_head); } else { g_form.setValue('u_bu_head', ''); } } }Explanation:
-
Condition for
isLoadingandnewValue:- Ensure the script exits if the form is loading or if the new value is empty.
-
Getting the Install Status:
- Use
g_form.getValue('install_status')to check the value of theinstall_statusfield. Return if its value is 2, 6, or 9.
- Use
-
Reference Lookup for Department:
- Use
g_form.getReference('department', setBuHead);to get the detailed record for thedepartmentfield. This will automatically call thesetBuHeadfunction and pass the referenced department record as an argument.
- Use
-
Function
setBuHeadto Handle Business Unit Reference:- Ensure that the
departmentexists and has abusiness_unit. - If it does, call
g_form.getReference('business_unit', setBuHeadFromBusinessUnit);to get the detailed record for thebusiness_unit. - If the
departmentorbusiness_unitis not found, clear theu_bu_headfield.
- Ensure that the
-
Function
setBuHeadFromBusinessUnitto Set BU Head:- Ensure that the
business_unitexists and has abu_head. - Set the value of the
u_bu_headfield to thebu_headof the related business unit. - If the
business_unitorbu_headis not found, clear theu_bu_headfield.
- Ensure that the
Feel free to adjust any field names (
u_bu_head,business_unit,install_status,department) and their values according to your specific instance configuration. This script assumesinstall_status,department, andu_bu_headare valid field names on the form's table. -
- User
Not working
- Assistant
Let's thoroughly debug and fix the script by ensuring the following aspects:
- Correct function signature.
- Proper checking of values.
- Correct handling of reference lookups.
- Syntax errors, especially in the
g_formmethods.
Here's another attempt to address the issues comprehensively:
function onChange(control, oldValue, newValue, isLoading, isTemplate) { if (isLoading || newValue === '') { return; } // Check whether install_status is 2, 6, or 9 var installStatus = g_form.getValue('install_status'); if (installStatus == '2' || installStatus == '6' || installStatus == '9') { return; } // Get the reference data for the 'department' field g_form.getReference('department', function(department) { if (department && department.business_unit) { // Get the reference for the business unit g_form.getReference('business_unit', function(businessUnit) { if (businessUnit && businessUnit.bu_head) { g_form.setValue('u_bu_head', businessUnit.bu_head); } else { g_form.setValue('u_bu_head', ''); } }, department.business_unit); } else { g_form.setValue('u_bu_head', ''); } }); }Key Changes and Fixes:
-
Correct Handling of Install Status Check:
- Ensure
installStatusis compared as a string. The values forinstall_statusmight be strings, so comparisons are done with'2','6', and'9'.
- Ensure
-
Handling Reference Lookup Values:
g_form.getReferenceuses a callback function which ensures proper handling of asynchronous data retrieval.
-
Nested Callbacks for Field References:
- The first
g_form.getReferencecall retrieves thedepartmentrecord.- If the
department.business_unitexists, make anotherg_form.getReferencecall to get thebusiness_unit. - Set the
u_bu_headvalue based on the nested lookup results.
- If the
- The first
-
Clear
u_bu_headif Reference Lookup Fails:- If the references do not return the expected objects, clear
u_bu_headto avoid showing undefined or stale data.
- If the references do not return the expected objects, clear
Ensuring Configuration in ServiceNow:
- Ensure that
departmentis a reference field pointing to the correct table. - Ensure that
business_unitis a reference field within thecmn_departmenttable. - Ensure
u_bu_headis the correct field name in the form where the BU Head should be populated.
Testing:
-
Open the Form:
- Make sure the form loads without JavaScript errors.
-
Change the Department or Trigger the Script via UI:
- Make a change that should trigger this onChange script to ensure that it executes and populates
u_bu_head.
- Make a change that should trigger this onChange script to ensure that it executes and populates
Debugging Potential Issues:
- Add
console.logstatements to verify data within the script if it still doesn't work.
Example debugging addition:
console.log('Department:', department); console.log('Business Unit:', department.business_unit); console.log('BU Head:', businessUnit.bu_head);Observe logs in the browser console for further troubleshooting. If there are still issues, examining the browser console output and debugging further within the ServiceNow scripting environment will help pinpoint the exact problem.