Checking or unchecking Active check box, should update the active or inactive date fields

Ronak3
Tera Contributor

Hi,

I have created Active field as true/false to check/uncheck and created active date and inactive date fields as date/time. Now when the active box is true, active date should be updated with date when the active box was made true. When 'active' is false, inactivate date should be updated with the date when the active box was made false. How to achieve this?

6 REPLIES 6

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello,

 

You can create a before business rule with filter condition (Active | Changes) with the script as 

if(current.u_active == 'true')

{

current.u_active_date = gs.nowDateTime();

}

else

{

current.u_inactive_date = gs.nowDateTime();

}

 

Modify the field column name accordingly.

 

-Pradeep Sharma

This form is under a scoped application. Still this code works? do i have to do script includes otherwise?

 

This will not work for Scoped Applications. Updated code below

 

if(current.u_active == 'true')

{

current.u_active_date = new GlideDateTime().getDisplayValue()

}

else

{

current.u_inactive_date = new GlideDateTime().getDisplayValue()

}

This code is not working. I do not know if this is because of the getDisplayvalue or any other thing is breaking. I have set the When to run as "before" and checked insert or update and filter condition to "Active = Changes" and in the code,

 

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

// Add your code here
//Type appropriate comment here, and begin script below
if(current.active == 'true'){
current.active_date = new GlideDateTime().getDisplayValue();

}
else{
current.inactive_date = new GlideDateTime().getDisplayValue();
}
})(current, previous);

 

And the dates will change accordingly whenever the user opens an existing record and modify the active check box and also when new record inserted.