Script to select specific approvers from a table

jesusnava
Giga Expert

Hello community,

I need your help with the following, I created an approval table with 5 different approvers per user

find_real_file.png

, and I have an approver field in a cat item,

find_real_file.png

what I need is that every time any user logs a ticket, when selecting an approver from the approval field, the user will see just his approvers. I tried creating an script include and a client script fot this but seems not be working fine

could you help me out with this?

Thank you

4 REPLIES 4

Matthew Glenn
Kilo Sage

Given that you didn't share your exact set up, you'll have to change a few things here:

 

To start, you mentioned that the user will see just their approvers. So we can drop whatever Client Script you have and use an Advanced Reference Qualifier on your variable. Something like this, where getApprovers refers to the name of your Script Include, passing along the users sys_id.

javascript:getApprovers(gs.getUserID())

Your variable should look like this:

find_real_file.png

 

Next, for the script include, you'll have to make some adjustments to the table/column names, but I believe this should work for you. 

function getApprovers(user){
	var arr = [];
	var gr = new GlideRecord('u_approval_lookup'); //update table name
	if(gr.get('u_user', user)){ //update column where the user is identified
		arr.push(gr.u_approver_l1); //update column names for the approvers
		arr.push(gr.u_approver_l2);
		arr.push(gr.u_approver_l3);
		arr.push(gr.u_approver_l4);
		arr.push(gr.u_approver_l5);
	}
	return "sys_idIN" + arr;
}

 

Your Script Includes should look something like this:

find_real_file.png

You'll like want to add in some error handling in the event the user doesn't have any associated approvers or their approvers are disabled/missing. Up to you.

 

Hopefully this works for you. If not, you may need to provide a few more details regarding your setup.

Hello Matthew,

 

Greetings, I tried what you sent me but I always get the same approvers, the idea is that by changing the user, the approvers would change as well.

find_real_file.png

 

I tried using a different script include and created a client script which are the following ones, please take a look

This is the script include:

----------------------------------------------

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

Approvers:function(){

var user = this.getParameter('sysparm_usuario');

var User_item = this.newItem("User_item");
var valor='';

var gr = GlideRecord('u_user_approvers');
gr.addQuery('u_user',user);
gr.query();

if(gr.next()){

User_item.setAttribute('First',gr.u_approver_l1.name);
User_item.setAttribute('Second',gr.u_approver_l2.name);
User_item.setAttribute('Third',gr.u_approver_l3.name);

}

gs.info('SCRIPT INCLUDE Approver name xxxx '+User_item.getAttribute('First'));
return '';

},

type: 'Get_approvers'

});

-------------------------------------------

And this is the client script I use:

function onLoad() {
//Type appropriate comment here, and begin script below

var usrID = g_form.getValue('requested_for');

var ga = new GlideAjax('Get_approvers');
ga.addParam('sysparm_name','Approvers');
ga.addParam('sysparm_usuario',usrID);
ga.getXML(ajaxResponse);

}

function ajaxResponse(response){


var answer = response.responseXML.getElementsByTagName("User_item");

var first = answer[0].getAttribute('First');
var second = answer [0].getAttribute('Second');
var third = answer [0].getAttribute('Third');


g_form.addOption('approval','u_approver_l1.name', first);
g_form.addOption('approval','u_approver_l2.name', second);
g_form.addOption('approval','u_approver_l3.name', third);


}

--------------------------------------

My question is, in the scrpt include, how to get the approvers into an array to validate in case there are blanks as for approvers.

Thank you Matthew!

OK, so I misunderstood your question. You stated "the user will see just his approvers", leading me to believe that it was based on the current user.

To clarify, this is actually based on another field on the form, is that correct? Hence the reason for the Client Script you had?

 

OK, so I misunderstood your question. You stated "the user will see just his approvers", leading me to believe that it was based on the current user.

Actually the answer to the script include you sent me the idea is that the current user just sees his approvers as you mentioned but I tried it and I see approvers but when I change user, I still see the same approvers,

This is how the code looks after the changes made based on the info and table:

find_real_file.png

but when I change approvers I get the same 3 approvers, they don´t change based on another user´s approvers.

 

To clarify, this is actually based on another field on the form, is that correct? Hence the reason for the Client Script you had?

The reason for the client script is to get the script include through an Ajax Response,

hope I did not mix you up,

 

Thanks and regards