- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2023 12:00 PM
Hello All! We have a requirement to set a custom date/time field on hardware models (category is computer), u_eol, to 4 years from the created date...
I've tried a number of business scripts that I thought would work but haven't figured it out yet.
Can anyone share, and/or help me figure this out.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2023 12:05 PM - edited 12-21-2023 12:05 PM
Hi @Nathan Okh ,
Can u try below code in a BR u can create a after insert BR.
(function executeRule(current, previous /*null when async*/) {
// Check if the category is "computer"
if (current.category == 'computer') {
// Get the created date
var createdDate = new GlideDateTime(current.sys_created_on);
// Add 4 years to the created date
var expirationDate = createdDate.addYearsUTC(4);
// Set the u_eol field to the calculated expiration date
current.u_eol = expirationDate;
}
})(current, previous);
Thanks,
Danish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2023 12:05 PM - edited 12-21-2023 12:05 PM
Hi @Nathan Okh ,
Can u try below code in a BR u can create a after insert BR.
(function executeRule(current, previous /*null when async*/) {
// Check if the category is "computer"
if (current.category == 'computer') {
// Get the created date
var createdDate = new GlideDateTime(current.sys_created_on);
// Add 4 years to the created date
var expirationDate = createdDate.addYearsUTC(4);
// Set the u_eol field to the calculated expiration date
current.u_eol = expirationDate;
}
})(current, previous);
Thanks,
Danish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2023 02:13 PM
So this got me to the correct answer however the IF statement did not work for me. It would not update.
I decided to do this on the alm_hardware asset record instead of on the model side.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2023 02:27 PM
@Danish Bhairag2 @Aniket Chavan I just posted another question regarding a fix script that is related. If you are able to help that would be great!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2023 12:56 PM
Hello @Nathan Okh ,
Please give a try to the script below and let me know how it works for you.
(function executeRule(current, previous /*null when async*/) {
// Add your code here
if (current.category == 'computer' && current.u_eol.nil()) {
// Check if the category is computer and retirement day is not set
var createdDate = new GlideDateTime(current.sys_created_on);
createdDate.addYearsUTC(4);
// Set retirement day to 4 years after the created date
current.u_eol = createdDate;
}
})(current, previous);
Let me know your views on this and Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks,
Aniket