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

G Ponsekar
Mega Guru

Hi @Naman Jain2412 ,

 

Configure scheduled job with Run marked as "Daily"

In script, add query like below which will fetch renewal date that is expired

 

gr.addQuery('renewal_dateRELATIVELT@dayofweek@ago@1');

 

Please mark correct if it is helpful

 

Thanks,

GP

Ankur Bawiskar
Tera Patron
Tera Patron

@Naman Jain2412 

you can use scheduled flow for this. why scheduled job is required?

I shared solution on this link earlier using flow, enhance your as per your requirement (table, field etc)

I cannot seem to get an Email Notification when a record's due date is reached 

sharing again

1) Flow Trigger Daily 23:00:00

2) Use Lookup Records action on your table to check if due date is today

3) Then use For Each to iterate over those records

4) then use Send Email action

AnkurBawiskar_0-1752129574314.png

 

AnkurBawiskar_1-1752129574322.png

 

AnkurBawiskar_2-1752129574307.png

 

 

AnkurBawiskar_3-1752129574294.png

 

AnkurBawiskar_4-1752129574346.png

 

AnkurBawiskar_5-1752129574322.png

 

AnkurBawiskar_6-1752129574315.png

 

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Naman Jain2412 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Naman Jain2412 

Thank you for marking my response as helpful.

As per new community feature you can mark multiple responses as correct.

If my response helped please mark it correct as well so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Naman Jain2412 

Thank you for marking my response as helpful.

As per new community feature you can mark multiple responses as correct.

If my response helped please mark it correct as well so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader