- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-21-2020 10:23 AM
Hi Community,
Need to update the state field of a custom table based on the state value (on approval table). i.e..,
Once the request is created on custom table an approval notification will be sent to manager of that user, in 6 business days this should be approved if not need to change the record state to cancelled on custom table. I am trying this using scheduled jobs but didn't find a solution. Hope someone would help me.
Thank you in Advance!!
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-21-2020 12:09 PM
Hi Madhuri,
Please follow the below steps:
1. Create a Schedule(here you can exclude holidays as well) like below:
2. Create a Scheduled Job that will execute daily on your time choosing.
3. Paste the script below in the run script section.
var grAprvl = new GlideRecord('sysapproval_approver');
grAprvl.addEncodedQuery('state!=approved^sys_created_on'); // check for all records with the state not 'Approved'
grAprvl.query();
while (grApp.next()) {
var grCustom = new GlideRecord('custom_table'); // find the appropriate record
grCustom.addQuery('approval_field', grAprvl.sysapproval); //the field 'Approval for'
grCustom.query();
if (grCustom.next()) {
var sched = new GlideSchedule('090eecae0a0a0b260077e1dfa71da828');
var created = grCustom.sys_created_on;
var gdt = new GlideDateTime();
var gtime1 = new GlideDateTime(created); //passing created on date to the GlideDateTime object
gtime1.addDays(6); //adding 6 days to the created on date
var dur = GlideDateTime.subtract(gdt, gtime1); //the difference between todays date and date after 6 days record was created
var result = dur.getNumericValue();
if (sched.isInSchedule(gdt) /*checking if todays date is in schedule*/ && result < 0 /*checking if 6 days has passed*/ ) {
grCustom.u_state = 'cancelled'; // set state to 'cancelled' on custom table
grCustom.update();
}
}
}
Please mark correct if find it helpful.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-22-2020 02:06 AM
Hi Pavlo Rudenko,
Sorry it didn't worked for me, I have checked in background scripts
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-22-2020 05:09 AM
Can you please clarify, what table do you use for approval and what is your custom table name? Also in the 'sysapproval_approver' table (if you use it), do you have 'Approval for' field referring to your custom table record sys_id?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-22-2020 09:46 AM
Thanks for the quick response. I am using 'sysapproval_approver' table for approval and don't use 'Approval for' field, so I have changed script using 'Approving' field comparing with sys_id of the record, even though I have changed the query I'm unable to update the expected records.
Samama Butt's reply helped me, I have followed those steps and made slight changes to script as per my requirement
If possible could you let me know how can we achieve this, in the way you have tried
Here is the script I have tried:
var grApp = new GlideRecord('sysapproval_approver');
grApp.addEncodedQuery('state!=approved^sys_created_on<javascript:gs.beginningOfLast8Days()'); //tried with state=requested
grApp.query();
while(grApp.next()) {
var grCr = new GlideRecord('custom_table');
grCr.addQuery('sys_id', grApp.document_id);
grCr.addQuery('u_state', 'new'); //'Approving'
grCr.query();
if(grCr.next()) {
grCr.u_state = 'cancelled';
grCr.update();
}
}