Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

how to calculate days using mailscript which is usefd in notifications

Ramu8
Tera Expert

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 

2 REPLIES 2

johnfeist
Mega Sage

Hi Ramu8,

 

The simplest thing is to grab the day, month and year for each of your datetime variables and then just create a string in the form of year-month-day 00:00:00.  You can then use a setDateTime() passing that string and your various datetime variables will all be for the start of the day.  Depending on how you need to do you revaluations, the 00:00:00 can be set to any relevant value (e.g. grab the time piece from todayDate and apply it to all relevant variables).

Hope that helps.

:{)

Helpful and Correct tags are appreciated and help others to find information faster

Mark Manders
Mega Patron

https://www.servicenow.com/community/developer-forum/days-calculations-for-knowledge-article-using-m...


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