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-17-2024 01:18 AM
Hello @matthew_hughes ,
There must be something wrong with your code, can you share the code that you are using?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2024 01:24 AM
Hi @Siddhesh Gawade I've updated the code, but now it seems that the response is always being set to 'Legacy' even though the create date of an application is newer than the system property:
Client Script:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2024 01:51 AM
it should be returning false if the create date is newer than the property.
Can you please check the format of date you are using in your system property. it should similar to "var DateTime" in your script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2024 02:10 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 ServiceNow.
2. Click on New to create a new business rule.
3. Give your business rule a name, for example, "Update Field Based on CI Creation Date".
4. In the Table field, select the table where your configuration item (CI) resides, typically this would be the cmdb_ci table.
5. In the When to run section, select "before" and "insert" if you want the rule to run before a new record is inserted into the database.
6. In the Advanced tab, you can write a script to update the field value based on the created date of the CI. Here's a sample script:
javascript
(function executeRule(current, previous /*null when async*/) {
var createdDate = current.sys_created_on;
if (createdDate != '') {
var year = createdDate.getFullYear();
if (year < 2020) {
current.field_name = 'Value for CIs created before 2020';
} else {
current.field_name = 'Value for CIs created in 2020 and later';
}
}
})(current, previous);
Replace "field_name" with the actual name of the field you want to update.
7. Click on Submit to save the business rule.
Please note that this is a basic example and you might need to adjust the script based on your specific requirements.
nowKB.com
For a good and optimistic result, and solving ServiceNow-related issues please visit this website.https://nowkb.com/home
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2024 02:34 AM - edited 01-17-2024 02:35 AM
Hello @matthew_hughes ,
I checked your client script and script include it is working on my side. So I am confident that the date format of your property must be incorrect. Please check with format: 2024-01-09
Kindly mark the answer ✔️Correct or Helpful ✔️If it addresses your concern.
Regards,
Siddhesh