- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2023 02:25 PM
I recently got help with a business rule to add a retirement date of 4 years after the Created On field is set on, u_eol.
Now we want to run a fix script to go through our entire asset inventory and run this "business rule"
(function executeRule(current, previous /*null when async*/) {
// Add your code here
//if (current.model_category == 'Computer') {
// 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);
I started working on a Fix Script, but I'm not sure if its correct and worried about testing it...
var alm = new GlideRecord('alm_hardware');
alm.addQuery('u_eol', '');
alm.query();
while(alm.next()){
if (u_eol == nil()){
var createdDate = new GlideDateTime(current.sys_created_on);
createdDate.addYearsUTC(4);
// Set retirement day to 4 years after the created date
alm.u_eol = createdDate;
alm.update();
}
}
Can anyone share code, and/or help to get the correct answer?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2023 04:00 PM
@Nathan Okh : Please use the below code and try it out. It's always advisable to execute data updates on a small set of data and in sub-production instances.
var alm = new GlideRecord('alm_hardware');
alm.addEncodedQuery('model_category=81feb9c137101000deeabfc8bcbe5dc4'); // Filter for Model Category = Computer
alm.setLimit(5); // This helps you to execute only for 5 records. (Good to have it for unit testing)
alm.query();
while (alm.next()) {
if (gs.nil(alm.u_eol)) {
var createdDate = new GlideDateTime(alm.sys_created_on);
createdDate.addYearsUTC(4);
alm.setWorkflow(false); // To avoid any additional rules to trigger during update
alm.u_eol = createdDate; // Set retirement day to 4 years after the created date
alm.update();
}
}
Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2023 04:00 PM
@Nathan Okh : Please use the below code and try it out. It's always advisable to execute data updates on a small set of data and in sub-production instances.
var alm = new GlideRecord('alm_hardware');
alm.addEncodedQuery('model_category=81feb9c137101000deeabfc8bcbe5dc4'); // Filter for Model Category = Computer
alm.setLimit(5); // This helps you to execute only for 5 records. (Good to have it for unit testing)
alm.query();
while (alm.next()) {
if (gs.nil(alm.u_eol)) {
var createdDate = new GlideDateTime(alm.sys_created_on);
createdDate.addYearsUTC(4);
alm.setWorkflow(false); // To avoid any additional rules to trigger during update
alm.u_eol = createdDate; // Set retirement day to 4 years after the created date
alm.update();
}
}
Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-25-2023 09:14 PM
@Nathan Okh : Did my response address your question, or is there anything else I can help you with?
Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-26-2023 08:09 AM
Thank you so much for the quick response. I just tried this and it worked great, much appreciated. I did add @Aniket Chavan 's greater and very important requirement of printing logs as well. So kudos to Aniket!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-28-2023 07:36 AM
Hello @Nathan Okh ,
I'm really glad to hear that it worked well for you!😁 kudos to you too!