- 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 06:41 PM
Hi @Nathan Okh ,
Everything is right in ur fix script except one thing. I would suggest in ur fix script instead of adding addQuery for ur u_eol field u can go to the list view of hardware table manually add the condition from filter above like u_eol is empty n run it. It will give u all those records which are empty.
Verify if all records returned good. If required add one more condition of model category is computer. Just to make sure we don't touch any other records
Now right click from that filter generated n use it in ur fix script as addEncodedQuery('your_query_copied_from_list_view');
Thanks,
Danish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2023 09:28 PM
Hello @Nathan Okhm,
Your Fix Script looks close, but there are a few adjustments needed e.g. we cannot use current inside the background or fix script.
Also I have added the logs inside the script so you can verify with them first with the count like you can apply the query on the table and see the total count and then copy this code in background script and comment the line " alm.update();" so that no record will update and you can see the total count and verify that count with the table one.
and then you can go ahead and run the same script as I given you below in the fix script.
Here's a modified version:
var alm = new GlideRecord('alm_hardware');
alm.addQuery('u_eol', ''); // Check if retirement date is not set
alm.query();
var recordCount = 0;
while (alm.next()) {
var createdDate = new GlideDateTime(alm.sys_created_on); // Use alm.sys_created_on
createdDate.addYearsUTC(4);
// Set retirement day to 4 years after the created date
alm.u_eol = createdDate;
alm.update();
// Print the updated record
gs.print('Updated Record: ' + alm.getDisplayValue());
recordCount++;
}
// Print the total count
gs.print('Total Records Updated: ' + recordCount);
Let me know your views on this and still need any help from my end.
Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks,
Aniket
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-26-2023 08:10 AM
Thank you @Aniket Chavan the logs are vital to development. Cheers.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-28-2023 07:33 AM
Hello @Nathan Okh ,
you're welcome! I'm glad the logs were helpful for development.😁
