Set Retirement day 4 years after Created date

Nathan Okh
Mega Sage

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. 

1 ACCEPTED SOLUTION

Danish Bhairag2
Tera Sage
Tera Sage

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

 

View solution in original post

4 REPLIES 4

Danish Bhairag2
Tera Sage
Tera Sage

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

 

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.

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

Aniket Chavan
Tera Sage
Tera Sage

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