How to return outcome of function on a background script

matthew_hughes
Kilo Sage

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?

3 REPLIES 3

Shruti
Mega Sage
Mega Sage
Hey,
Try this in background script
function checkValidDepartments(businessUnit, encodedquery, message) {

    var checkdepartment = new GlideRecord('cmn_department');
    checkdepartment.addquery('name', businessUnit);
    checkdepartment.addEncodedQuery(encodedquery);
    checkdepartment.query();
    if (checkdepartment.next()) {
        return "Ok";
    } else {
        return message;
    }
}
gs.info(checkValidDepartments('Chief Data & Anals', 'u_active=true^u_department_type=Business Unit^ORu_department_type=Function', 'Not valid'));

Abhijit4
Mega Sage

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.

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

Nayan  Dhamane
Kilo Sage
Kilo Sage

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

If my answer solved your issue, please mark my answer as Correct & Helpful based on the Impact

Best Regards,
Nayan Dhamane
ServiceNow Community Rising Star 2023.