Global script include method always return undefined value

dhanu3
Kilo Sage

We are trying to execute script include from business rule and for testing tried from background script also.

Script include is not client callable as we are calling it from server side only and Accessible from is 'All application scope'.

We are calling it from global scope only.

When we print the value of variable before return statement it gives correct value. But when it returns and we are printing the output of script include method it return undefined. We tried by defining static value and returning it gives same result.

return "some sys id"; this statement also gives undefined answer.

 

Regards,

Dhanashree

 

1 ACCEPTED SOLUTION

@dhanu3 

try this and see

var getOwner = Class.create();
getOwner.prototype = {
    initialize: function() {},

    getOwner: function(approver) {
        var ul, lastMgr, path;
        var t = this.getManager(approver);
        var exe = t.split(' ');
        path = exe[1];
        lastMgr = exe[0];

        if (path == '') {
            ul = approver;
        } else if (path == '1000M') {
            ul = approver;
        } else {
            ul = this.getOwner(lastMgr); // Ensure the recursive call returns a value
        }

        if (ul != '' && ul != undefined) {
            gs.log("UL " + ul, 'UL');
            return "UL " + ul;
        }
    },

    getManager: function(user) {
        var man, path;
        var getM = new GlideRecord('sys_user');
        if (getM.get(user)) {
            if (getM.active == true) {
                man = getM.manager;
                path = getM.manager.u_org_path.u_path;
                return man.toString() + ' ' + path.toString();
            }
        }
    },

    type: 'getOwner'
};

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

13 REPLIES 13

dhanu3
Kilo Sage

I was updating the script as client data was included in it, that's why not added it directly. I was not ignoring it, I have mentioned it already.

 

@Ankur Bawiskar 

Below is the script, on PDI also getting same result.

 

var getOwner = Class.create();
getOwner.prototype = {
    initialize: function() {},
 
getOwner: function(approver) {
        var ul, lastMgr, path;
        var t = this.getManager(approver);
        var exe = [];
        exe = t.split(' ');
        path = exe[1];
        lastMgr = exe[0];

        if (path == '') {
            ul = approver;
            //  return approver;
        } else if (path == '1000M') {
            ul = approver;
            //  return approver;
        } else {
            this.getOwner(lastMgr);
        }
        if (ul != '' && ul != undefined) {
            gs.log("UL " + ul, 'UL'); //getting value
            return "UL " + ul; //getting undefined not even getting 'UL'
        }
    },
    getManager: function(user) {
        var man, path;
        var getM = new GlideRecord('sys_user');
        if (getM.get(user)) {
            if (getM.active == true) {
                man = getM.manager;
                path = getM.manager.u_org_path.u_path;
                return man.toString() + ' ' + path.toString();
            }
        }
    },
  type: 'getOwner'
};

@dhanu3 

try this and see

var getOwner = Class.create();
getOwner.prototype = {
    initialize: function() {},

    getOwner: function(approver) {
        var ul, lastMgr, path;
        var t = this.getManager(approver);
        var exe = t.split(' ');
        path = exe[1];
        lastMgr = exe[0];

        if (path == '') {
            ul = approver;
        } else if (path == '1000M') {
            ul = approver;
        } else {
            ul = this.getOwner(lastMgr); // Ensure the recursive call returns a value
        }

        if (ul != '' && ul != undefined) {
            gs.log("UL " + ul, 'UL');
            return "UL " + ul;
        }
    },

    getManager: function(user) {
        var man, path;
        var getM = new GlideRecord('sys_user');
        if (getM.get(user)) {
            if (getM.active == true) {
                man = getM.manager;
                path = getM.manager.u_org_path.u_path;
                return man.toString() + ' ' + path.toString();
            }
        }
    },

    type: 'getOwner'
};

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar  Thank You!

BillMartin
Mega Sage

Hi @dhanu3 ,

 

Sharing with you how Script Includes work in ServiceNow. This lesson will help you build a deeper understanding of how to use Script Includes effectively with object-oriented programming principles. You'll gain full control over the parameters you pass in and the data you return—giving you the confidence and maturity to write clean, reusable, and scalable code.