How to call a callback from a script include that's inside a while loop?

tezzk
Kilo Expert

The example script does not work, I am only testing it at the moment to try to get something to come back (anything)!
The client script is needed to populate the reference field for the lvl3 approver on a CMS phone order.

Trying to get a version that will work on the service portal plugin: is difficult. as we were using a while loop in a CMS form. 
but our experimental version being built without previous experience with callbacks, is obviously wrong..  Any help or pointers would be appreciated. 

Client script: 
=================================================

function onLoad() {

var appValue = g_form.getValue('rbio_approver');
//Ajax client script
var ga = new GlideAjax('global.LookupVPAJAX2');
ga.addParam('sysparm_name','getVIP');
ga.addParam('sysparm_line_approver',appValue);
ga.getXML(myCallBack);

function myCallBack(response) {
var val = response.responseXML.documentElement.getAttribute('answer');
if (val != 'true') {
alert('Alert =' +val);
g_form.setValue("to_lvl_3_approver",val);
}
}
}

================================================

Script Include:
=================================================

var LookupVPAJAX2 = Class.create();
LookupVPAJAX2.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

getVIP: function() {
/* the intention of the scrip include is simply to pass back the value of the manager at level 3
In order to do this I have a while loop that tests for the user level and returns when it trips over a level three
*/
var current_ec = ''; var chk = ''; var currentname = ''; var mydata = '';
var employeeId = this.getParameter('sysparm_line_approver');
var linemanager_m_ec_chk = ''; var linemanagername = '';
//load current user if no line manager then reset
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id',employeeId);
gr.query(myCallBack);

function myCallBack(gr) {
while (gr.next()) {
current_ec = gr.u_lvl_grade;
if (user.manager == '' || user.manager == null || user.manager == undefined || user.manager.isNil()){
return 1; // indicates to the calling client script that the approver does not have a manager.
}
}
}
// loop around until you find someone who is a level three
while (current_ec != "EC" && current_ec != '' ) {
gs.log(' loop ' + current_ec);
var ga = new GlideRecord('sys_user');
ga.addQuery('sys_id',employeeId);
ga.query(myCallBackb);
}

//define the callback function
function myCallBackb(ga) {
while (ga.next()) {
current_ec = ga.u_lvl_grade;
employeeId = ga.manager;
if (current_ec == 'EA' || current_ec == "EB" || current_ec == "EC" ){
return user.sys_id;
}
}
}
},
});
===============================================

 

1 ACCEPTED SOLUTION

tezzk
Kilo Expert

The solution was to stop attempting to use a callback inside a loop using the process i have shown in previous log
what I should have done was call the AJAX script include using this:

client side script: Onchange: line_approver

    var ga = new GlideAjax('LookupVPAJAX');
    ga.addParam('sysparm_name','getVIP');
    ga.addParam('sysparm_line_approver',newValue);
    ga.getXML(myCallBack);
    
function myCallBack(response)
        var vip = response.responseXML.documentElement.getAttribute('answer');

use the returned value to populate the form field for third level approver

 

In the Script include:

var LookupVPAJAX = Class.create();
LookupVPAJAX.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    
    getVIP: function() {
        var employeeId = this.getParameter('sysparm_line_approver');
        var xi = 0;
        gs.log(' employeeId =' +employeeId);
        var users = new GlideRecord('sys_user');
        users.get(employeeId);
        if (users.u_lvl_grade == 'EC') {  // cant have the approver being the lvl3 approver as well
            users = users.manager.getRefRecord();
        }
        gs.log(' users.u_lvl_grade 1 ' + users.u_lvl_grade);  // u_lvl_grade is a custom variable in the user record
        user = users.manager.getRefRecord();
        gs.log(' users.u_lvl_grade 2 ' + users.u_lvl_grade);
        while (xi < 10) {
            xi++;
            gs.log('users = ' + users.name + ' users.u_lvl_grade.' + users.u_lvl_grade +'.  sys_id = ' + users.sys_id + ' xi count =' + xi);
            if (users.u_lvl_grade.indexOf('EA')>=0) {gs.log(' exit ramp 1 ' + xi);return  'EA';}
                if (users.u_lvl_grade.indexOf('EC') >=0 || users.u_lvl_grade.indexOf('EB')>=0) { gs.log(' exit ramp 2 ' + xi); return users.sys_id; }
                    if (!users.manager){gs.log(' exit ramp 3 ' +xi); return 'null';}
                        users = users.manager.getRefRecord();
                    }
                },
                type: 'LookupVPAJAX'
            });

This combination gets the line approver on the RITM and searches the hierarchy of users until it finds the user who is a level 3 "EC" employee (held by u_lvl_grade), which we then use to populate the workflow as an extra approver.  no need to do complex gliderecord lookups. etc  

This worked :

 

 

 

 

 

 

 

View solution in original post

4 REPLIES 4

Omkar Mone
Mega Sage

Hi 

Maybe you can try this once - 

gr.query(function() {
while (gr.next()) {
current_ec = gr.u_lvl_grade;
if (user.manager == '' || user.manager == null || user.manager == undefined || user.manager.isNil()){
return 1; // indicates to the calling client script that the approver does not have a manager.
}
}
}
// loop around until you find someone who is a level three
while (current_ec != "EC" && current_ec != '' ) {
gs.log(' loop ' + current_ec);
var ga = new GlideRecord('sys_user');
ga.addQuery('sys_id',employeeId);
ga.query(myCallBackb);
}

//define the callback function
function myCallBackb(ga) {
while (ga.next()) {
current_ec = ga.u_lvl_grade;
employeeId = ga.manager;
if (current_ec == 'EA' || current_ec == "EB" || current_ec == "EC" ){
return user.sys_id;
}
}
}
},
});

 

Hope this helped.

 

Regards,

Omkar Mone


tezzk
Kilo Expert

Thanks Omkar, unfortunately this change does not seem to affect the result: still showing a Nul responce at the client scriupt:

var LookupVPAJAX2 = Class.create();
LookupVPAJAX2.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    
    getVIP: function() {
        /* the intention of the scrip include is simply to pass back the value of the manager at level 3
        In order to do this I have a while loop that tests for the user level and returns when it trips over a level three
         */
        var current_ec = ''; var chk = ''; var currentname = ''; var mydata = '';
        var employeeId = this.getParameter('sysparm_line_approver');
        var linemanager_m_ec_chk = '';    var linemanagername = '';
        //load current user if no line manager then reset
        var gr = new GlideRecord('sys_user');
        gr.addQuery('sys_id',employeeId);
        gr.query(function() {
            while (gr.next()) {
                current_ec = gr.u_lvl_grade;
                if (user.manager == '' || user.manager == null || user.manager == undefined || user.manager.isNil()){
                    return 1;   // indicates to the calling client script that the approver does not have a manager.  
                }
            }
        });
        
        // loop around until you find someone who is a level three
        var ga = new GlideRecord('sys_user');
        while (current_ec != "EC" && current_ec != ''  && current_ec != null  && current_ec != 'undefined'  ) {
        gs.log(' loop     ' + current_ec);    
        ga.addQuery('sys_id',employeeId);
        ga.query(myCallBackb);
        }

        //define the callback function
function myCallBackb(ga) {
    while (ga.next()) {
        current_ec = ga.u_lvl_grade;
        employeeId = ga.manager;
        if (current_ec == 'EA' || current_ec == "EB" || current_ec == "EC" ){
            return user.sys_id;
        }
    }
  }
},
    
});

 

I think the callback from inside the loop might be the cause of the problem?

 

tezzk
Kilo Expert

The solution was to stop attempting to use a callback inside a loop using the process i have shown in previous log
what I should have done was call the AJAX script include using this:

client side script: Onchange: line_approver

    var ga = new GlideAjax('LookupVPAJAX');
    ga.addParam('sysparm_name','getVIP');
    ga.addParam('sysparm_line_approver',newValue);
    ga.getXML(myCallBack);
    
function myCallBack(response)
        var vip = response.responseXML.documentElement.getAttribute('answer');

use the returned value to populate the form field for third level approver

 

In the Script include:

var LookupVPAJAX = Class.create();
LookupVPAJAX.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    
    getVIP: function() {
        var employeeId = this.getParameter('sysparm_line_approver');
        var xi = 0;
        gs.log(' employeeId =' +employeeId);
        var users = new GlideRecord('sys_user');
        users.get(employeeId);
        if (users.u_lvl_grade == 'EC') {  // cant have the approver being the lvl3 approver as well
            users = users.manager.getRefRecord();
        }
        gs.log(' users.u_lvl_grade 1 ' + users.u_lvl_grade);  // u_lvl_grade is a custom variable in the user record
        user = users.manager.getRefRecord();
        gs.log(' users.u_lvl_grade 2 ' + users.u_lvl_grade);
        while (xi < 10) {
            xi++;
            gs.log('users = ' + users.name + ' users.u_lvl_grade.' + users.u_lvl_grade +'.  sys_id = ' + users.sys_id + ' xi count =' + xi);
            if (users.u_lvl_grade.indexOf('EA')>=0) {gs.log(' exit ramp 1 ' + xi);return  'EA';}
                if (users.u_lvl_grade.indexOf('EC') >=0 || users.u_lvl_grade.indexOf('EB')>=0) { gs.log(' exit ramp 2 ' + xi); return users.sys_id; }
                    if (!users.manager){gs.log(' exit ramp 3 ' +xi); return 'null';}
                        users = users.manager.getRefRecord();
                    }
                },
                type: 'LookupVPAJAX'
            });

This combination gets the line approver on the RITM and searches the hierarchy of users until it finds the user who is a level 3 "EC" employee (held by u_lvl_grade), which we then use to populate the workflow as an extra approver.  no need to do complex gliderecord lookups. etc  

This worked :

 

 

 

 

 

 

 

tezzk
Kilo Expert

NB// this works in CMS and also in the Service portal Plugin environment!