How to check for approvals on an RITM from the Service Catalog Request workflow?

Laurie Marlowe1
Kilo Sage

Hello,

We have a requirement for some catalog items to obtain the supervisor's approval on the RITM before the Service Catalog Request workflow goes through the Technology approval.  I am having an issue with the second loop if (ap.next()) not finding the record.  For my log statement it says "ap is no".  I tried various things, such as running a background script and hard coding the RITM number, which did not work.  I tried going to the sysapproval_approver table and creating a query with state = requested .and. Approved for.number = <RITM number> and this returns the record.

 

// Set the variable 'answer' to true or false to indicate if the condition has been met or not.

var found = 'no';
var num = '';
var itm = new GlideRecord('sc_req_item');
itm.addQuery('request', current.sys_id);
itm.addQuery('request_item.cat_item.u_no_it_review_approvals', false);
itm.addQuery('request_item.cat_item.u_no_ps_approval', false);
itm.addQuery('state', '!=', '3');
itm.addQuery('state', '!=', '4');
itm.query();
while (itm.next()) {

    num = itm.number;
    gs.log('number is ' + num);
}
    var ap = new GlideRecord('sysapproval_approver');
    ap.addQuery('state', 'requested');
    ap.addQuery('sysapproval.number', num);
    ap.addQuery();
    
    if (ap.next()) {
        found = 'yes';
		gs.log('in second loop' + num);
    }

gs.log('ap is ' + found);

if (found == 'yes') {
    answer = false;
} else {
    answer = true;
}

The workflow just passes through the Wait for condition for Additional RITM Approvals.

find_real_file.png

Any ideas?

Thank you,

Laurie

 

 

 

 

1 ACCEPTED SOLUTION

sachin_namjoshi
Kilo Patron
Kilo Patron

Update your code like below

 

// Set the variable 'answer' to true or false to indicate if the condition has been met or not.

var found = 'no';
var num = '';
var itm = new GlideRecord('sc_req_item');
itm.addQuery('request', current.sys_id);
itm.addQuery('request_item.cat_item.u_no_it_review_approvals', false);
itm.addQuery('request_item.cat_item.u_no_ps_approval', false);
itm.addQuery('state', '!=', '3');
itm.addQuery('state', '!=', '4');
itm.query();
while (itm.next()) {

    num = itm.number;
    gs.log('number is ' + num);
}
    var ap = new GlideRecord('sysapproval_approver');
    ap.addQuery('state', 'requested');
    ap.addQuery('document_id', current.sys_id);
    ap.addQuery();
    
    while(ap.next()) {
        found = 'yes';
		gs.log('in second loop' + num);
    }

gs.log('ap is ' + found);

if (found == 'yes') {
    answer = false;
} else {
    answer = true;
}

 

Regards,

Sachin

View solution in original post

5 REPLIES 5

sachin_namjoshi
Kilo Patron
Kilo Patron

Update your code like below

 

// Set the variable 'answer' to true or false to indicate if the condition has been met or not.

var found = 'no';
var num = '';
var itm = new GlideRecord('sc_req_item');
itm.addQuery('request', current.sys_id);
itm.addQuery('request_item.cat_item.u_no_it_review_approvals', false);
itm.addQuery('request_item.cat_item.u_no_ps_approval', false);
itm.addQuery('state', '!=', '3');
itm.addQuery('state', '!=', '4');
itm.query();
while (itm.next()) {

    num = itm.number;
    gs.log('number is ' + num);
}
    var ap = new GlideRecord('sysapproval_approver');
    ap.addQuery('state', 'requested');
    ap.addQuery('document_id', current.sys_id);
    ap.addQuery();
    
    while(ap.next()) {
        found = 'yes';
		gs.log('in second loop' + num);
    }

gs.log('ap is ' + found);

if (found == 'yes') {
    answer = false;
} else {
    answer = true;
}

 

Regards,

Sachin

Hi Sachin,

Now the problem seems to be that the "found = 'yes' is never set.  For some reason it does not go into the while loop. 

I tried this in a background script and it still won't work.  "ap is no" is what prints.  I verified there are two records that meet the condition for the query.

var found = 'no'; 
var ap = new GlideRecord('sysapproval_approver');
    ap.addQuery('state', 'requested');
    ap.addQuery('document_id', 'ec5245791b8ce4103efe8480cd4bcb71');
    ap.addQuery();
    
    if(ap.next()) {
        found = 'yes';
		gs.print('in second loop' + num);
    }

gs.print('ap is ' + found);

Any ideas?

Thank you,

Laurie

I figured it out!

    ap.addQuery();
    
    if(ap.next()) {

 

I should be ap.query() not ap.addQuery().

Thank you very much for your help!

Laurie

Yes, i was about to point it out.

Code will work for you.

 

Regards,

Sachin