Need help to limit the duration of delegation

rishabh31
Mega Sage

Dear Team,

 

In Employee Center I am able to add a delegate, start date, end date and duties, currently start date and end date are whatever we selected through the calendar as the duration of delegation but I want to Limit the Duration of Delegation to no longer/more than 60days, if the user tries to select an end date to me more than 60days then it throws an error message 'Delegation duration should not be more than 60days' and clears the End Date field value (date).

 

Requesting help to get this acheived.

 

Thank You

1 ACCEPTED SOLUTION

BharathChintala
Mega Sage

@rishabh31 Yes you can achieve this 2 ways.

 

Client vs Server.

 

via onchange client script you can check difference between 2 dates abort action if it is more than 60days( drawback is on list view won't work)

 

Via before business rule will be best solution as it stop from list form or any other source.

 

writr]e a before business rule in delegation table

 

on insert and update

start date or end date changes

 

in script:

var start = new GlideDateTime(current.starts);

var end = new GlideDateTime(current.ends);

var diff = GlideDateTime.subtract(start, end);

var days = diff.getRoundedDayPart();

 

if(days>60){

gs.addErrorMessage("Not allowed to delegate user for more than 60 days");

current.setAbortAction(true);

}

 

If my inputs have helped with your question, please mark my answer as accepted solution, and give a thumb up.
Bharath Chintala

View solution in original post

2 REPLIES 2

BharathChintala
Mega Sage

@rishabh31 Yes you can achieve this 2 ways.

 

Client vs Server.

 

via onchange client script you can check difference between 2 dates abort action if it is more than 60days( drawback is on list view won't work)

 

Via before business rule will be best solution as it stop from list form or any other source.

 

writr]e a before business rule in delegation table

 

on insert and update

start date or end date changes

 

in script:

var start = new GlideDateTime(current.starts);

var end = new GlideDateTime(current.ends);

var diff = GlideDateTime.subtract(start, end);

var days = diff.getRoundedDayPart();

 

if(days>60){

gs.addErrorMessage("Not allowed to delegate user for more than 60 days");

current.setAbortAction(true);

}

 

If my inputs have helped with your question, please mark my answer as accepted solution, and give a thumb up.
Bharath Chintala

Thank You @BharathChintala Sir,

 

This is working fine. Per your help tested this by applying logs and checking table 'syslog' after each save or trying to save a new 'sys_granular_delegate' record.

 

(function executeRule(current, previous /*null when async*/ ) {
    var start = new GlideDateTime(current.starts);
    var end = new GlideDateTime(current.ends);
    var diff = GlideDateTime.subtract(start, end);
    var days = diff.getRoundedDayPart();
    gs.info("Rishabh Starts " + start);
    gs.info("Rishabh Ends " + end);
    gs.info("Rishabh diffsec " + diff);
    gs.info("Rishabh diffday " + days);
    if (days > 60) {
        gs.addErrorMessage(gs.getMessage("Not allowed to delegate user for more than 60 days"));
        current.setAbortAction(true);
    }

})(current, previous);

It gives logs data as (correctly subtracting the No. of days i.e., 155)

rishabh31_0-1690888298363.png

Once again thank you so much, marked your response correct and helpful.