Scheduled Script / Notifications

MStritt
Tera Guru

I currently have a scheduled script that looks at the last login time (logging into our Support Portal) of Contacts. If they haven't logged in >1 year (365 days), we are locking their account. We would like to send 3 notifications to these Contacts over a period of time leading up to the actually locking of their account.

 

First notification 30 days (335 days not logged in) in advance of action being taken.
Second notification 2 weeks (351 days not logged in) in advance of action being taken.

Third and final notification 1 day (364 days not logged in) in advance of action being taken.

 

What's the best way of creating these notifications? 

1 ACCEPTED SOLUTION

And it also didn't lock the 8 contacts yesterday?

Last thing we could try is to split the query and call the function twice. Again, make sure to replace the javascript colon. 

function sendReminder(days, event) {

    var query = "locked_out=false^account!=58ba9ccb4fddb640873b69d18110c744^last_login_timeRELATIVELT@dayofweek@ago@" + days + "^last_login_timeRELATIVEGT@dayofweek@ago@" + Number(days + 1);

    var contactGR = new GlideRecord("customer_contact");
    contactGR.addEncodedQuery(query);
    contactGR.query();

    while (contactGR.next()) {
        gs.eventQueue(event, contactGR, contactGR.getUniqueValue());
    }

}


function lockAccount(query) {

    var contactGR = new GlideRecord("customer_contact");
    contactGR.addEncodedQuery(query);
    contactGR.query();

    while (contactGR.next()) {
        contactGR.setValue("locked_out", true);
        contactGR.update();
    }
}

// Send reminders
sendReminder(335, "lock_out_reminder.30days");
sendReminder(351, "lock_out_reminder.14days");
sendReminder(364, "lock_out_reminder.1day");

// Lock accounts
lockAccount("locked_out=false^account!=58ba9ccb4fddb640873b69d18110c744^last_login_time<javascript&colon;gs.beginningOfOneYearAgo()");

lockAccount("locked_out=false^last_login_timeISEMPTY^sys_created_on<javascript&colon;gs.beginningOfOneYearAgo()^account!=58ba9ccb4fddb640873b69d18110c744")

 

View solution in original post

20 REPLIES 20

Rene El Hamzh
Kilo Sage

Hi @MStritt

 

create 3 events and 3 notifications and generate/fire the events at the appropriate time through your code. 

 

Best regards, 

Rene

Thanks Rene,

 

How/where would I create the Events? How would I incorporate those into my code?

 

// Build the encoded query for customer_contact
var encodedQuery = "last_login_time<javascript&colon;gs.beginningOfOneYearAgo()^account!=58ba9ccb4fddb640873b69d18110c744^active=true";

// Query the customer_contact table based on the encoded query
var contactGR = new GlideRecord('customer_contact');
contactGR.addEncodedQuery(encodedQuery);
contactGR.query();

// Loop through the contacts and lock them
while (contactGR.next()) {
    contactGR.locked_out = true;
    contactGR.update();
}

 

I'd prefer using Flow Designer for this. However, if you prefer scripting, the following should work.

 

If you haven't already, some steps to complete to make this work:

  • Make sure your scheduled job runs daily
  • Create the 3 events in System Policy > Events > Registry
  • Create the 3 notification records
    • Send when = Event is fired
    • Event name = Select appropriate event
    • Advanced view: Set "Event parm 1 contains recipient" to true
function sendReminder(days, event) {

    var query = "active=true^last_login_timeRELATIVELT@dayofweek@ago@" + days + "^last_login_timeRELATIVEGT@dayofweek@ago@" + Number(days + 1);

    var contactGR = new GlideRecord("customer_contact");
    contactGR.addEncodedQuery(query);
    contactGR.query();

    while (contactGR.next()) {
        gs.eventQueue(event, contactGR, contactGR.getUniqueValue()); 
    }

}

function lockAccount() {
    var query = "last_login_timeRELATIVELT@dayofweek@ago@365^active=true";

    var contactGR = new GlideRecord("customer_contact");
    contactGR.addEncodedQuery(query);
    contactGR.query();

    while (contactGR.next()) {
        contactGR.setValue("locked_out", true);
        contactGR.update();
    }
}

// Send reminders
sendReminder(30, "reminder.30days");
sendReminder(14, "reminder.14days");
sendReminder(1, "reminder.1day");

// Lock accounts
lockAccount();

 

Thanks Rene.

 

I'll give this a try, and report back if I have questions configuring.