Populate the value of the field depending on the created date of a configuration item

matthew_hughes
Kilo Sage

I've got a situation where I'm trying to populate a field called 'Legacy or New' depending on the value of the created date:

matthew_hughes_0-1705410689357.png

 

I'm wanting to do a comparison of the value of the Test create date field against the value of a system property:

matthew_hughes_1-1705410776145.png

 

The outcome that I'm wating to achieve is that if the created date of the selected business application is older than 01-09-2024, then the 'Legacy or New' field should be populated to 'Legacy'. Otherwise, the 'Legacy or New' field should be populated to 'New'.

 

If somebody could let me know what I need to do, that would be great.

 

 

10 REPLIES 10

Ahana 01
Tera Expert

Sure, you can achieve this by creating a business rule in ServiceNow. Here's a step-by-step guide:

1. Navigate to System Definition > Business Rules in your ServiceNow instance.
2. Click on "New" to create a new business rule.
3. Give your business rule a name, for example, "Populate Field Based on CI Creation Date".
4. In the "When to run" section, select "before" and "insert" if you want the rule to run before a new record is inserted.
5. In the "Filter conditions" section, select the table where your configuration item (CI) resides, for example, "cmdb_ci".
6. In the "Script" section, write a script to populate the value of the field based on the created date of the CI. Here's a sample script:

javascript
(function executeRule(current, previous /*null when async*/) {
var ci = new GlideRecord('cmdb_ci');
ci.get(current.sys_id);
var createdOn = ci.sys_created_on.getDisplayValue();
var fieldToUpdate = ""; // replace with the name of the field you want to update

if (createdOn < gs.daysAgoStart(7)) { // if the CI was created more than a week ago
current[fieldToUpdate] = "Old CI";
} else {
current[fieldToUpdate] = "New CI";
}
})(current, previous);


7. Click on "Submit" to save the business rule.

Please replace the fieldToUpdate variable with the actual name of the field you want to update. This script checks if the CI was created more than a week ago and sets the value of the field accordingly. You can adjust the condition and the values as per your requirements.


nowKB.com