- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2023 01:14 PM
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.
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2023 05:07 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2023 01:39 PM
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');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2023 07:46 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2023 05:07 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2023 02:33 PM
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