The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Add an hour to time field in incident form

sagarpundir
Tera Contributor

i am trying to add an extra hour in time in time field but i face some problem .

this is my code running on before update 

 

(function executeRule(current, previous /*null when async*/) {

    var gt = new GlideTime(current.u_customtime);  
    var newTime = gt.addSeconds(300);  
    current.setValue('u_customtime', newTime);

})(current, previous);
6 REPLIES 6

AbhishekLawaniy
Tera Contributor
(function executeRule(current, previous /*null when async*/) {

    // Ensure the field has a value before proceeding
    if (current.u_customtime) {

        // Create a GlideDateTime object from the field's value
        var gdt = new GlideDateTime(current.u_customtime.toString());
        
        // Add one hour (3600 seconds)
        gdt.addSeconds(3600);
        
        // Set the value of the field directly from the modified GlideDateTime object
        current.u_customtime = gdt;
    }

})(current, previous);

Try this.

Regards,
Abhishek Lawaniyan
=========================================================
If my response proves useful, please indicate its helpfulness by selecting "Accept as Solution" and " Helpful." This action will support the community.

GlideFather
Tera Patron

Hi @sagarpundir,

 

try something like this:

(function executeRule(current, previous /*null when async*/) {
    var customTime = new GlideDateTime(current.u_customtime);
    var newTime = customTime.addSeconds(300);
    current.setValue('u_customtime', newTime);
})(current, previous);

 

let me know if that helped

———
/* If my response wasn’t a total disaster ↙️ drop a Kudos or Accept as Solution ↘️ Cheers! */


Musab Rasheed
Tera Sage
Tera Sage

Try below in before business rule

(function executeRule(current, previous /*null when async*/) {
    // Convert time field to a GlideDateTime object
    var dt = new GlideDateTime(current.u_customtime);
    // Add 1 hour = 3600 seconds
    dt.addSeconds(3600);
    // Retrieve only the time portion
    var newTime = dt.getLocalTime().toString();
    current.setValue('u_customtime', newTime);
})(current, previous);
Please hit like and mark my response as correct if that helps
Regards,
Musab

Rafael Batistot
Kilo Patron

Hi @sagarpundir 

 

Here’s how you can correctly add 1 hour to your time field:

 

(function executeRule(current, previous /*null when async*/) {

// Get the current time from the field
var gt = new GlideTime();
gt.setValue(current.u_customtime);

// Add 1 hour (3600 seconds)
gt.addSeconds(3600);

// Set back the updated value
current.setValue('u_customtime', gt.getValue());

})(current, previous);