Trigger Daily Notification if renewal date is expired

Naman Jain2412
Tera Expert

Hi, 

Please provide the relevant code so that schedule job can be run daily if renewal date is expired.

Do let me know in case of any query.

 

Thanks

1 ACCEPTED SOLUTION

Hello @Naman Jain2412 

Please try code below

function checkExpiredPolicies() {
    var gr = new GlideRecord('sn_compliance_policy'); // Target table: Compliance Policy

    var today = new GlideDateTime(); // Get current date and time
   
    var todayFormatted = today.getLocalDate().getValue();

    // Query for policies where the 'u_next_renewal_date' is before today
    // and the policy state is NOT already 'Expired' or 'Inactive' (adjust as per your state values)
    gr.addQuery('u_next_renewal_date', '<', todayFormatted);

    // We only want to process policies that are currently 'Active' (or similar state) and not already marked 'Expired' or 'Inactive'.
    gr.addActiveQuery(); // Only process active records, if 'active' field exists and is used
    gr.addQuery('state', '!=', 7); // Assuming 7 is your 'Expired' or 'Requires Review' state value
    gr.addQuery('state', '!=', 3); // Assuming 3 is your 'Retired' state value (don't process retired policies)
    gr.query();
    var updatedCount = 0;
    while (gr.next()) {
        // 1. Update the state field
        // IMPORTANT: Replace '7' with the correct integer value for your desired 'Expired' or 'Requires Review' state.
        gr.state = 7; // Example: Set state to 'Expired' or 'Requires Review'
        gr.update();
        updatedCount++;

        gs.eventQueue('policy.expired', gr, gr.owner.sys_id, gr.assignment_group.sys_id); // Pass relevant user/group sys_ids
}

checkExpiredPolicies();

Try this and let me know in case of queries

Thank you!

If my response helped you, please accept the solution and mark it as helpful.
Thank You!

View solution in original post

10 REPLIES 10

Naman Jain2412
Tera Expert

I have a doubt that I have created this script but it is showing days like 1414141445 that when I preview notification or trigger notification. could you pls let me know the error in the code actually it's showing the days based on current & next renewal code.

Here is the code for reference (Email Script)

var user;
    var gr = new GlideRecord('sn_compliance_policy');
    gr.addQuery("state", "draft");
    gr.query();
    while (gr.next()) {


        var u = new GlideDateTime(gr.u_next_renewal_date);
        var c = new GlideDateTime().getDate();
        var d = gs.dateDiff(c, u, true);
        var days = d / (60 * 60 * 24);


            if (days == 90 || days == 45 || days == 15 || days == 14 ||days == 12 ||days == 10 ||days == 8 || days == 6 ||days == 4 ||days == 2||days == 0) {
          template.print(days)
       


        // Add your code here

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

Notification code

 

This is to inform you that the ${name} is scheduled for renewal on ${u_next_renewal_date}, which is ${mail_script:sn_dynamic_values_renewal_date} days from today.