Problem record , GlideFunction field to stop calculating on State == Closed condition
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2024 05:12 AM
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!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2024 05:42 AM
Assuming you are calculating age in business rule,
var age;
if(current.state != 'Closed' ) {
age = datediff(now(),sys_created_on)
}
Please try it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2024 08:16 AM
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.