If statement to check if Approval action was done

Ken Berger
Giga Guru

Hi all,

 

I am working on a workflow to automate adding users to a particular AD group.  The workflow contains 2 approval activities (manager and then finance approvals).  Since sometimes the managers or finance folks do not do the approval in a timely manner, I added some loops with a notification, timer and IF activity, to wait a period of time and then check if any approval action was done and if not, to loop back to the notification activity.

KenBerger_0-1697227835055.png

I cannot seem to get the code in my IF activity to detect when the approval action has been completed. I checked and the data in my sysapproval_approver table is there so it should work.  Can someone tell me what is wrong with my code?:

function ifScript() {
	var manager=current.variable_pool.who_are_you_requesting_this_software_for.manager.name;
	var gr = new GlideRecord('sysapproval_approver');
	gr.addQuery('document_id',current.sys_id);
	gr.addQuery('approver', manager);
	gr.addQuery('state','approved' || 'state','rejected');
	gr.query();
		if(gr.next()) {
			return 'yes';
		} else {
			return 'no';}
}

Thanks in advance for any help you may give.

 

Kind regards,

Ken

 

1 ACCEPTED SOLUTION

That looks a lot cleaner.  Now you can temporarily add some logs to find out why the GR isn't returning the correct record.  I would also recommend a more standard format like this:

answer = ifScript();

function ifScript() {
    var aprvr = current.variables.who_are_you_requesting_this_software_for.manager;
    var ritm = current.sys_id;
    workflow.info('Approver: ' + aprvr);
    workflow.info('RITM: ' + ritm);
    var strQuery = 'approver=' + aprvr + '^stateINapproved,rejected^sysapproval=' + ritm;
    var gr = new GlideRecord('sysapproval_approver');
    gr.addEncodedQuery(strQuery);
    gr.query();
    if (gr.next()) {
        return 'yes'; 
    }
    return 'no';
}

When viewing the executing Workflow Context from the RITM you can check the Log tab to confirm that the script is running and the value of the two encoded query variables.  The resulting encoded query should match the query that you can copy from the filter breadcrumb when manually filtering the approvals list by the same request and Approver. 

BradBowman_0-1697285016534.png

 

 

 

View solution in original post

4 REPLIES 4

Brad Bowman
Kilo Patron
Kilo Patron

This is not a valid addQuery statement:

	gr.addQuery('state','approved' || 'state','rejected');

OR conditions are a little funky in GlideRecord, so it's best to use addEncodedQuery

	gr.addEncodedQuery('stateINapproved,rejected');

Since you are using document_id, you might also need to specify the source_table for that sys_id

gr.addQuery('source_table', 'sc_req_item');
 
 

 

Hi Brad,

 

Thanks for the tips.  I tried the changes, but I am still not able to get it to work.  I am seeing if I can get away from using document_id and add the current.sys_id into the encoded query.  Not sure what is wrong.

answer = function ifScript(){
	var aprvr = current.variable_pool.who_are_you_requesting_this_software_for.manager;
	var ritm = current.sys_id;
	var strQuery = 'approver=' + aprvr + '^stateINapproved,rejected^sysapproval=' + ritm;
	var gr = new GlideRecord('sysapproval_approver');
	gr.addEncodedQuery(strQuery);
	gr.query();
	if(gr.next()) {
		return 'yes'; 
	} else {
		return 'no';
	}
}
;

 

Best,

Ken

That looks a lot cleaner.  Now you can temporarily add some logs to find out why the GR isn't returning the correct record.  I would also recommend a more standard format like this:

answer = ifScript();

function ifScript() {
    var aprvr = current.variables.who_are_you_requesting_this_software_for.manager;
    var ritm = current.sys_id;
    workflow.info('Approver: ' + aprvr);
    workflow.info('RITM: ' + ritm);
    var strQuery = 'approver=' + aprvr + '^stateINapproved,rejected^sysapproval=' + ritm;
    var gr = new GlideRecord('sysapproval_approver');
    gr.addEncodedQuery(strQuery);
    gr.query();
    if (gr.next()) {
        return 'yes'; 
    }
    return 'no';
}

When viewing the executing Workflow Context from the RITM you can check the Log tab to confirm that the script is running and the value of the two encoded query variables.  The resulting encoded query should match the query that you can copy from the filter breadcrumb when manually filtering the approvals list by the same request and Approver. 

BradBowman_0-1697285016534.png

 

 

 

Brad, 

Thanks!  That worked a treat.  I modified the script slightly. Here is my final script:

answer = ifScript();
function ifScript(){
	var aprvr = current.variable_pool.who_are_you_requesting_this_software_for.manager.sys_id;
	var ritm = current.sys_id;
	var strQuery = 'approver=' + aprvr + '^stateINapproved,rejected^sysapproval=' + ritm;
	var gr = new GlideRecord('sysapproval_approver');
	gr.addEncodedQuery(strQuery);
	gr.query();
	if(gr.next()) {
		return 'yes'; 
	} else {
		return 'no';
	}
}

 Best regards,

Ken