ITSM

Harish Bainsla
Tera Sage
Tera Sage

I have a catalog item name Leave tracker

HarishBainsla_0-1698832971822.png

 

I want when user select leave type and leave start and leave end date i want to auto set value on leave balance table after approvel activity in workflow 

HarishBainsla_1-1698833111064.png

it should be set in a custom table like in below table

HarishBainsla_2-1698833164093.png

example: i select start leave date tomorrow and end leave date 2 day after start leave date the it should subtract from the table mean if it is standard leave and we have total 3 so it should update taken leave field 2 and leave left 1 using workflow can anybody help me

1 ACCEPTED SOLUTION

Tai Vu
Kilo Patron
Kilo Patron

Hi @Harish Bainsla 

You can subtract to have the duration between the Leave Start and End date.

Sample below.

var gdtStart = new GlideDateTime(<leave_start_date>); //replace your variable name
var gdtEnd = new GlideDateTime(<leave_end_date>); //replace your variable name
var duration = GlideDateTime.subtract(gdtStart, gdtEnd);
var days = duration.getDayPart();
var gr = new GlideRecord('<your_balance_leave_available>');
gr.query('<caller>', current.variables.requested_for); //replace your field and variable name
gr.query('<leave_type>', current.variables.leave_type); //replace your field and variable name
gr.query();
if(gr.next()){
    gr.taken_leave = days; //replace your field name
    gr.left_leave = parseInt(gr.balance) - days; //replace your field name
    gr.update();
}

 

You can consider to exclude the weekend for the script.

Sample below.

var schedule = new GlideSchedule();
schedule.load('ded21b2047d27910ab9bb6bf016d43a0'); //24x5 Schedule
var duration = schedule.duration(gdtStart, gdtEnd);
var days = duration.getDayPart();

 

Let me know if it works for you.

 

Cheers,

Tai Vu

View solution in original post

4 REPLIES 4

Tai Vu
Kilo Patron
Kilo Patron

Hi @Harish Bainsla 

You can subtract to have the duration between the Leave Start and End date.

Sample below.

var gdtStart = new GlideDateTime(<leave_start_date>); //replace your variable name
var gdtEnd = new GlideDateTime(<leave_end_date>); //replace your variable name
var duration = GlideDateTime.subtract(gdtStart, gdtEnd);
var days = duration.getDayPart();
var gr = new GlideRecord('<your_balance_leave_available>');
gr.query('<caller>', current.variables.requested_for); //replace your field and variable name
gr.query('<leave_type>', current.variables.leave_type); //replace your field and variable name
gr.query();
if(gr.next()){
    gr.taken_leave = days; //replace your field name
    gr.left_leave = parseInt(gr.balance) - days; //replace your field name
    gr.update();
}

 

You can consider to exclude the weekend for the script.

Sample below.

var schedule = new GlideSchedule();
schedule.load('ded21b2047d27910ab9bb6bf016d43a0'); //24x5 Schedule
var duration = schedule.duration(gdtStart, gdtEnd);
var days = duration.getDayPart();

 

Let me know if it works for you.

 

Cheers,

Tai Vu

ok i will try should i write this code in run script activity in workflow

Hi @Harish Bainsla 

Yes the script can be used inside the workflow. Just make sure to replace the field's name and variable's name.

 

Cheers,

Tai Vu

 

Thanks its working fine