Not able to update date time in column

Akshaykhare
Tera Contributor

I want to add one column which will show the date and time at which the status of request is change to Completed. I have created the column and created business rule to update the column with the date and time but somehow the script is not working. Please suggest any improvements

 

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

    // Add your code here
    if(current.u_request_state == '6'){
    current.u_completed = gs.nowDateTime();
    }

})(current, previous);
2 ACCEPTED SOLUTIONS

Ankur Bawiskar
Tera Patron
Tera Patron

@Akshaykhare 

you can use before update BR with correct condition

Script is this

current.setValue('u_completed', new GlideDateTime());

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

View solution in original post

Runjay Patel
Giga Sage

Hi @Akshaykhare ,

 

You dont require to write script for this, just configure the business rule like below.

In when to run select state changes to completed.

RunjayPatel_1-1736856063670.png

 

And in action section select the date same as updated.

RunjayPatel_2-1736856100761.png

 

RunjayPatel_3-1736856118396.png

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

 

 

 

 

View solution in original post

5 REPLIES 5

Anurag Tripathi
Mega Patron
Mega Patron

Is this a Before BR?

-Anurag

Use this in a Before BR

 

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

    // Add your code here
    if(current.u_request_state == '6'){
    current.u_completed =  new GlideDateTime();
    }
-Anurag

Ankur Bawiskar
Tera Patron
Tera Patron

@Akshaykhare 

you can use before update BR with correct condition

Script is this

current.setValue('u_completed', new GlideDateTime());

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

Community Alums
Not applicable

@Akshaykhare 

a Business rule should run on the "After" update to capture changes to the status field after it's saved.

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

// Check if the status has changed to "6"

if (current.status == '6' && previous.status != '6') {

// Set the custom date/time field with the current date and time

current.u_completed = new GlideDateTime();

}

})(current, previous);