Fix Script to add Retired Date to all alm_hardware computer

Nathan Okh
Mega Sage

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?

 

1 ACCEPTED SOLUTION

Sainath N
Mega Sage
Mega Sage

@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.

View solution in original post

8 REPLIES 8

Sainath N
Mega Sage
Mega Sage

@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.

@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.

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!

Hello @Nathan Okh ,

I'm really glad to hear that it worked well for you!😁 kudos to you too!