Calculate business days in scheduled jobs

Madhuri8
Tera Expert

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!!

1 ACCEPTED SOLUTION

Samama Butt
Tera Expert

Hi Madhuri,

Please follow the below steps:

1. Create a Schedule(here you can exclude holidays as well) like below:

find_real_file.png

 

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.

View solution in original post

7 REPLIES 7

Hi Pavlo Rudenko,

Sorry it didn't worked for me, I have checked in background scripts

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?

 

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();

}

}