Populate the value of the field depending on the created date of a configuration item
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2024 05:14 AM
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:
I'm wanting to do a comparison of the value of the Test create date field against the value of a system property:
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-18-2024 03:43 AM
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