Script to identify if a ticket has been closed for more than 7 days

ceraulo
Mega Guru

Hello!

We have an inbound action (reply) for a custom app. The requirements are:

(1) if ticket is closed for more than 7 days, create new ticket

(2) if ticket is closed for less than 7 days, update ticket

(3) if ticket is still open, update ticket

 

this is my script and working correctly for (1) and (3). it is not working for (2).

if (current.state == 3) {
        var checkDate = new GlideDateTime(current.closed_at);
        checkDate.addDaysLocalTime(-7);
        if (checkDate > gs.nowDateTime()) { //check record is not closed before more than 7 days
            if (current.state == 3) {
                current.state = 2;
            }
            current.work_notes = email.body_text;
            current.update();
        } else {
            <script to create new ticket>
            current.insert();
        }
    } 
	
else {
        
        current.work_notes = email.body_text;
        current.update();
    }

 

Please help!

Thank you.

 

1 ACCEPTED SOLUTION

Nayan  Dhamane
Kilo Sage
Kilo Sage

Hello @ceraulo ,

Please try the below script:

if (current.state == 3) {

        var checkDate = new GlideDateTime(current.closed_at);

var now = new GlideDateTime(gs.nowDateTime());
now.addDays(-7);

        if (checkDate > now) { //check record is not closed before more than 7 days

            current.state = 2;

            current.work_notes = email.body_text;

            current.update();

        } else {

            <script to create new ticket>

            current.insert();

        }

    } 

 

else {

        

        current.work_notes = email.body_text;

        current.update();

    }

Please mark my answer as correct if it helps.
BR,
Nayan.

If my answer solved your issue, please mark my answer as Correct & Helpful based on the Impact

Best Regards,
Nayan Dhamane
ServiceNow Community Rising Star 2023.

View solution in original post

2 REPLIES 2

Nayan  Dhamane
Kilo Sage
Kilo Sage

Hello @ceraulo ,

Please try the below script:

if (current.state == 3) {

        var checkDate = new GlideDateTime(current.closed_at);

var now = new GlideDateTime(gs.nowDateTime());
now.addDays(-7);

        if (checkDate > now) { //check record is not closed before more than 7 days

            current.state = 2;

            current.work_notes = email.body_text;

            current.update();

        } else {

            <script to create new ticket>

            current.insert();

        }

    } 

 

else {

        

        current.work_notes = email.body_text;

        current.update();

    }

Please mark my answer as correct if it helps.
BR,
Nayan.

If my answer solved your issue, please mark my answer as Correct & Helpful based on the Impact

Best Regards,
Nayan Dhamane
ServiceNow Community Rising Star 2023.

ceraulo
Mega Guru

This worked! Thank you.