Problem record , GlideFunction field to stop calculating on State == Closed condition

Rohith Sureshk1
Tera Contributor

Hi,

 

I have to requirement to create a Gldiefunction field to calculate live age of problem record. I achieved it using the below code 

glidefunction:datediff(now(),sys_created_on);

But the problem is , this function is running even when the form is inactive(state == closed). I have to stop the calculation when State == close. Need help in this requirement. 

 

Thanks in advance!! 

2 REPLIES 2

udaysingh16
Tera Contributor

Assuming you are calculating age in  business rule,

 

var age;
if(current.state  != 'Closed' ) {
age =  datediff(now(),sys_created_on)
}

 

Please try it.

Iraj Shaikh
Mega Sage
Mega Sage

Hi @Rohith Sureshk1 

Here's the modified code that you can use to achieve your requirement:

 

if (current.state != 'closed') {
  current.glidefunction = gs.dateDiff(gs.nowDateTime(), current.sys_created_on.getGlideObject());
} else {
  current.glidefunction = ''; // Set the GlideFunction field to a blank value
}

 


In this code, `current.state` refers to the State field of the current record. You can replace `'closed'` with the actual value that represents the closed state in your ServiceNow instance.

The `glidefunction` field is set based on the condition. If the state is not equal to 'closed', the `glidefunction` field will be calculated using the `gs.dateDiff()` function, which calculates the difference between the current date and the `sys_created_on` field.

If the state is 'closed', the `glidefunction` field is set to a blank value using `current.glidefunction = ' ';`. You can modify this line to set a default value if needed.

Make sure to place this code in the appropriate business rule or script where you want the calculation to occur.

Please mark this response as correct or helpful if it assisted you with your question.