Update date field by 2 years
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-30-2023 03:26 AM
Hi All,
I have a requirement to update a date field by 2 years. One custom table has been created to hold data for hardware assets and it is calling the scheduled retirement date from alm_asset table. Now I want to update all the scheduled retirement date by 2 years irrespective of any single date.
For example:
Asset 1 : Scheduled Retirement date : 31-10-2024; new Scheduled Retirement date : 31-10-2026
Asset 2 : Scheduled Retirement date : 25-02-2026; new Scheduled Retirement date : 25-02-2028 and so all other records.
Please help with the code and process to update all the records at once.
Regards
Souvick
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-30-2023 04:00 AM
you need to use scheduled job which will run only once and use script something similar to this
updateRecords();
function updateRecords(){
try{
// give correct field name and table name
var gr = new GlideRecord("tablename");
gr.addEncodedQuery("retirementDateFieldISNOTEMPTY");
gr.query();
while (gr.next()) {
var dt = new GlideDateTime(gr.retirementDateField);
dt.addYearsLocalTime(2);
gr.retirementDateField = dt;
gr.update();
}
}
catch(ex){
gs.info(ex);
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-30-2023 04:35 AM
HI @Souvick A ,
I trust you are doing great.
Please find below reference code for the usecase
function updateRetirementDates() {
try {
var gr = new GlideRecord('your_custom_table_name');
gr.addQuery('scheduled_retirement_date', '!=', '');
gr.query();
while (gr.next()) {
var retirementDate = new GlideDateTime(gr.scheduled_retirement_date);
retirementDate.addYears(2);
gr.scheduled_retirement_date = retirementDate;
gr.update();
}
gs.info('Retirement dates updated successfully.');
} catch (ex) {
gs.error('An error occurred while updating retirement dates: ' + ex.message);
}
}
updateRetirementDates();
Was this answer helpful?
Please consider marking it correct or helpful.
Your feedback helps us improve!
Thank you!
Regards,
Amit Gujrathi