The CreatorCon Call for Content is officially open! Get started here.

Script for approval requests

Taaha M
Tera Contributor

I am looking for a script where I want to send a approval request to a user and if user is not active it should go to the User's Manager.

The below script is not working

answer = [];
appr();
function appr() {
var ownerManager = [];
ownerManager = current.variables.v_role.u_owner.split(',');
var gr = new GlideRecord('sys_user');
gr.addQuery("sys_id",'IN',ownerManager);
gr.addActiveQuery();
gr.query();
while(gr.next()){
if (gr.active) {
answer.push(gr.user_name);
} else {
answer.push(gr.manager);
}
}
}

2 ACCEPTED SOLUTIONS

Peter Bodelier
Giga Sage

Hi @Taaha M,

 

Assuming the ownerManager variable is filled correctly:

answer = [];

var ownerManager = [];
ownerManager = current.variables.v_role.u_owner.split(',');
var gr = new GlideRecord('sys_user');
gr.addQuery("sys_id","IN",ownerManager);
gr.query();
while(gr.next()){
	if (gr.getValue('active')) {
		answer.push(gr.getUniqueValue());
		} else {
		answer.push(gr.getValue('manager'));
	}
}

Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

View solution in original post

Brad Bowman
Kilo Patron
Kilo Patron

If your u_owner field on the v_role referenced table is a list field, don't split it into an array before using it in the addQuery line as the value will be a comma-separated list of sys_ids.  And you don't want to only return active records since you're checking for that in the returned records.  Also, you need to push sys_ids to the answer array, not user_name, and when you push a sys_id to an array you need to either getValue or force it to a string or you will get an array of all the same sys_id (it pushes the memory location or something like that when it's not a string):

 

answer = [];
appr();
function appr() {
    var gr = new GlideRecord('sys_user');
    gr.addQuery("sys_id", 'IN', current.variables.v_role.u_owner);
    gr.query();
    while (gr.next()) {
        if (gr.active == 'true') {
            answer.push(gr.sys_id.toString());
        } else {
            answer.push(gr.manager.toString());
        }
    }
}

 

 

View solution in original post

2 REPLIES 2

Peter Bodelier
Giga Sage

Hi @Taaha M,

 

Assuming the ownerManager variable is filled correctly:

answer = [];

var ownerManager = [];
ownerManager = current.variables.v_role.u_owner.split(',');
var gr = new GlideRecord('sys_user');
gr.addQuery("sys_id","IN",ownerManager);
gr.query();
while(gr.next()){
	if (gr.getValue('active')) {
		answer.push(gr.getUniqueValue());
		} else {
		answer.push(gr.getValue('manager'));
	}
}

Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

Brad Bowman
Kilo Patron
Kilo Patron

If your u_owner field on the v_role referenced table is a list field, don't split it into an array before using it in the addQuery line as the value will be a comma-separated list of sys_ids.  And you don't want to only return active records since you're checking for that in the returned records.  Also, you need to push sys_ids to the answer array, not user_name, and when you push a sys_id to an array you need to either getValue or force it to a string or you will get an array of all the same sys_id (it pushes the memory location or something like that when it's not a string):

 

answer = [];
appr();
function appr() {
    var gr = new GlideRecord('sys_user');
    gr.addQuery("sys_id", 'IN', current.variables.v_role.u_owner);
    gr.query();
    while (gr.next()) {
        if (gr.active == 'true') {
            answer.push(gr.sys_id.toString());
        } else {
            answer.push(gr.manager.toString());
        }
    }
}