This code is for Scheduled Job, but it is not working. i m fetching data from 'sc_item_option'

Singhdeep70
Tera Contributor
var reqGR = new GlideRecord('sc_req_item');

var past24 = new GlideDateTime();
past24.addHoursUTC(-24);
var catSysID = '3D72bd38fc8310c75069fbc5a6feaad3d9';
reqGR.addQuery('cat_item', catSysID);
reqGR.addQuery('approval', 'requested');
reqGR.addQuery('sys_created_on', '<=', past24);
reqGR.query();

while (reqGR.next()) {
    var sysID = '3D6569c5b88390c75069fbc5a6feaad341';
    var variableGR = new GlideRecord('sc_item_option');
    variableGR.addQuery('item_option_new', sysID);
    variableGR.query();

    if (variableGR.next()) {
        var user = variableGR.value;
        var userGR = new GlideRecord('sys_user');
        userGR.addQuery('name', user);
        userGR.query();

        if (userGR.next()) {

            gs.print('working');
            gs.eventQueue(
                'approval.required.email',
                reqGR,
                userGR.email,
                reqGR.getValue('number')
            );
        }
    }
}
1 ACCEPTED SOLUTION

var reqGR = new GlideRecord('sc_req_item');





reqGR.addQuery('approval', 'requested');
reqGR.addEncodedQuery('sys_created_onRELATIVELE@hour@ago@24');
reqGR.addQuery('cat_item.name', 'Onboarding Employee');
reqGR.query();

 

while (reqGR.next()) {

 

    var approverGR = new GlideRecord('sysapproval_approver');
    approverGR.addQuery('state', 'requested');
    approverGR.addQuery('sysapproval', reqGR.sys_id);
    approverGR.query();

 

    while (approverGR.next()) {

 

        var userGR = new GlideRecord('sys_user');
        if (userGR.get(approverGR.approver)) {
            gs.eventQueue('approval.required.email',reqGR,userGR.email,reqGR.getValue('number'));
        }

 

    }
}
 
I've got solution, but Thank you
 

View solution in original post

6 REPLIES 6

lpruit2
Mega Sage

@Singhdeep70 is there a reason why you are querying  specific hard-coded sys_id? Also, it looks like your sys_ids are incorrect. I noticed they both start with "3D" which makes me think they were copied and pasted from your URL which captured the URL-encoded equivalent of the = sign. You can also tell that you sys_id is 34 characters (3D + actual sys_id) instead of the correct 32 character count. 

 

You can verify this outside of the Scheduled Job by going to the individual target table and use the standard condition builder to query for that specific sys_id. If your query returns 0 results you know it's an incorrect sys_id. You can then rerun the query this time removing the "3D" and see if it works. 

 

While this doesn't address the best-practice use-case of hard-coding sys_ids, I hope this solves your problem of why your query isn't working. 

Ankur Bawiskar
Tera Patron

@Singhdeep70 

what's your business requirement?

are you using the correct sysId?

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 10x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

This script is for finding pending approvals older than 24 hours for a specific catalog item and then triggering event for notification.

@Singhdeep70 

did you check if you are using correct sysids?

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 10x ServiceNow MVP  ||  ✨ ServiceNow Community Leader