How to return outcome of function on a background script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-25-2022 04:26 AM
I've created the following function to return some parameters in a background script:
checkValidDepartments: function(businessUnit, encodedquery, message) {
var checkdepartment = new glerecord('cmn_department');
checkdertment.addquery('sys_id',businessUnit);
checkdepartment.addencodedqry(encodedquery);
checkdepartment.query();
if (checkdepartment.next()) {
return "Ok";
else{
return message;
}
return checkValidDepartments('Chief Data & Anals', 'u_active=true^u_department_type=Business Unit^ORu_department_type=Function', 'Not valid');
}
However, it does not return anything. Please can somebody explain why it won't return anything in the background script?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-25-2022 04:43 AM - edited 11-25-2022 06:09 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-25-2022 04:48 AM - edited 11-25-2022 04:53 AM
You haven't utilized message that is being returned by function. Also there are some modification needed it should be like below.
function checkValidDepartments(businessUnit, encodedquery, message) {
var checkdepartment = new glerecord('cmn_department');
checkdertment.addquery('sys_id',businessUnit);
checkdepartment.addencodedqry(encodedquery);
checkdepartment.query();
if (checkdepartment.next())
return "Ok";
else
return message;
}
var returnedMsg=checkValidDepartments('Chief Data & Anals', 'u_active=true^u_department_type=Business Unit^ORu_department_type=Function', 'Not valid');
gs.print(returnedMsg);
Also business unit you are sending as 'Chief Data & Anals' and trying to match with sys_id wihich will not return anything. You should replace line " checkdertment.addquery('sys_id',businessUnit);" with checkdertment.addquery('business_unit',businessUnit); //business_unit is field name from department table.
lease mark answer as Correct or Helpful based on impact.
Regards,
Abhijit
ServiceNow MVP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-25-2022 04:58 AM - edited 11-25-2022 04:58 AM
Hello @matthew_hughes ,
Please try the below code:
checkValidDepartments('Chief Data & Anals', 'u_active=true^u_department_type=Business Unit^ORu_department_type=Function', 'Not valid');
function checkValidDepartments(businessUnit, encodedquery, message) {
var checkdepartment = new glerecord('cmn_department');
checkdertment.addquery('sys_id',businessUnit);
checkdepartment.addencodedqry(encodedquery);
checkdepartment.query();
if (checkdepartment.next()) {
gs.info("Ok");
}
else{
gs.info(message);
}
}
Please mark my response as Correct / Helpful based on Impact.
BR,
Nayan
Best Regards,
Nayan Dhamane
ServiceNow Community Rising Star 2023.