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

Ankur Bawiskar
Tera Patron
Tera Patron

@pvv1045330 

you should set milliseconds, no need to query as BR is before insert on CHG table itself

Also use setDateNumericValue function to set duration field

update script as this

 

if(current.type == 'normal'){
	var approvalDuration = 4*24*60*60*1000;// 4 days converted to milliseconds
	current.u_minimum_lead_time.setDateNumericValue(approvalDuration);
}
else if(current.type == 'expedite'){
	var Duration = 1*24*60*60*1000; // 1 day i.e. 24 hour converted to milliseconds 
	current.u_minimum_lead_time.setDateNumericValue(Duration);
}
else if(current.type == 'standard'){
	var approval = 0.5*24*60*60*1000; // 12 hours means 0.5 day and this is converted to milliseconds
	current.u_minimum_lead_time.setDateNumericValue(approval);
}

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar, Thanks for the response, I'll check and confirm you. 

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

Hi @Vishal Birajdar, it's working for only normal change

and other 2 type it's populated wrong place date & time, please find the below secern short 

Type: Standard

pvv1045330_0-1697006792654.png

Type: Expedite

pvv1045330_1-1697006868750.png

Type: Normal

pvv1045330_2-1697006960777.png

use update before br

condition:  type is one of  normal,expedite,standard

Script:

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;
    }

 

 

 

 

Hi @pvv1045330 

 

You should put space between Days & Hr:mm:ss - format "Days Hr:mm:ss" e.g., "00 48:00:00"

 

 

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";   // Space between 00 & 48:00:00
        current.u_minimum_lead_time = Duration;
    } else if (current.type == 'standard') {
        var approval = "00 24:00:00";  // Space between 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