Days calculations for Knowledge article using mail script

vamshi2
Tera Contributor

I need to send expiry notification based on valid to in knowledge base 

 

i have created flow design it is triggering exactly using lookup activity

vamshi2_0-1718299843467.png

 

but in the notification, am using mail script to calculate days based on valid to field in knowledge article

as below 

 

    (function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
        /* Optional EmailOutbound */
        email, /* Optional GlideRecord */ email_action,
        /* Optional GlideRecord */
        event) {

        // Add your code here
        //get the days of knowledge article which will be expired
        var validToDate = current.valid_to;
        var kaNumber = current.number;
        var finalDays;
        var todayDate = new GlideDate(); //get the todays date
        gs.info("todayDate "+ todayDate);
        var validDateFormatted = " ";
        var validDate = new GlideDateTime(validToDate);
        validDateFormatted = validDate.getDate(); //get the formatted validTo date
        gs.info("validDateFormatted "+ validDateFormatted);
        var dur = new GlideDuration();
        dur = GlideDateTime.subtract(todayDate, validDateFormatted);
        gs.info("dur "+ todayDate);
        var days = dur.getDayPart(); //get the expiring days
        gs.info("days "+days )
        //Calculating the days
        if (days == '0' || days == 0) {
            finalDays = days + 1;
        } else {
            finalDays = days
        }
        gs.info("finalDays "+finalDays );
        var subjectKA = "ACTION REQUIRED: Knowledge Article " + kaNumber + " Expires in " + finalDays + " Days";
        email.setSubject(subjectKA);
        template.print(finalDays);

    })(current, template, email, email_action, event);
 

please find logs

 

12th jun 15:30 aest(same day output)

 

validDateFormatted 2024-08-11

todayDate 2024-06-12

 days 59

finalDays 59

dur 2024-06-12

 

12th jun 9:15 aest(same day output)

 

dur 2024-06-11

validDateFormatted 2024-08-11

todayDate 2024-06-11

days 15

dur 2024-06-11

finalDays 60

 

Please advice how to write mail script which should consider correct days  i.e., 60 days irrespective of time on same day 

1 REPLY 1

Mark Manders
Mega Patron

https://www.servicenow.com/community/developer-forum/how-to-calculate-days-using-mailscript-which-is...


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark