Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

how to set dynamic date and time on duration filed?

pvv1045330
Tera Contributor

Hi All,

 

I have a requirement like, when we create a change request based on the type we need to set date and time on duration filed?

normal change = 4 days

expiated change = 24 hr

standard change = 12 hr

 

how can we set the date and time dynamically based on the change type.

I'm using before business rule

type: before

condition: state is of normal,expedite,standard

var chnge = new GlideRecord('change_request');
    chnge.addQuery('sys_id', 'current.sys_id');
    chnge.query();
    if(current.type=='normal'){
        var approvalDuration = 14*60*60*60;//date/hr/min/seconds
        current.u_minimum_lead_time=approvalDuration;
 }
 else if(current.type=='expedite'){
        var Duration = 48*60*60;
        current.u_minimum_lead_time=Duration;
 }
    else if(current.type=='standard'){
        var approval = 24*60*60;
        current.u_minimum_lead_time=24*60*60;
    }
1 ACCEPTED SOLUTION

Vishal Birajdar
Giga Sage

Hi @pvv1045330 

 

Can you simply try to set like below :

 

 

  if(current.type=='normal'){
        /*Days Hr:mm:ss  - As we know 14 days for normal*/
        var approvalDuration = "14 00:00:00";
        current.u_minimum_lead_time=approvalDuration;
 }
 else if(current.type=='expedite'){
        /* Days Hr:mm:ss  - As we know 48 hrs  will be converted into days automatically*/
        var Duration = "00 48:00:00";
        current.u_minimum_lead_time=Duration;
 }
    else if(current.type=='standard'){
        var approval = "00 24:00:00";
        current.u_minimum_lead_time=approval;
    }

 

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

View solution in original post

14 REPLIES 14

Harish KM
Kilo Patron
Kilo Patron

Hi @pvv1045330 here is the BR on change request table runs before insert

Script:

var todayDate = new GlideDateTime(); // get today date
var addDays;
var addHours;
if(current.type == 'normal')
{

todayDate.addDaysLocalTime(4); // add 4 days
addDays = todayDate.getDisplayValue();
current.description  = addDays; // pass date field to description field on change request

}
else if(current.type == 'emergency')
{
 todayDate.addSeconds(43200); // add 12 hours
 addHours = todayDate.getDisplayValue();
current.description  = addHours; // pass date field to description field on change request
}
 
Regards
Harish

Hi @Harish KM , Thanks for the replay.

Have tried with your script but no luck, it's haven't populated the value on field,

the value will be populated when state leaves to new 

pvv1045330_0-1696996353934.png

 

var todayDate = new GlideDateTime(); // get today date
var addDays;
var addHours;
if(current.type == 'normal')
{

todayDate.addDaysLocalTime(4); // add 4 days
addDays = todayDate.getDisplayValue();
current.u_minimum_lead_time  = addDays; // pass date field to description field on change request

}
else if(current.type == 'expedite')
{
 todayDate.addSeconds(43200); // add 12 hours
 addHours = todayDate.getDisplayValue();
current.u_minimum_lead_time  = addHours; // pass date field to description field on change request
}
   

Hi @pvv1045330 Sry i tot it is a date field. You can follow Ankur suggestion for duration field

Regards
Harish

Hi @Vishal Birajdar 
I have the similar required but i have set the delivery date for a catalog item.
Can you please guide me over that.

Thanks,
Shivani