The CreatorCon Call for Content is officially open! Get started here.

Trouble sending e-mail from scheduled job

Mike Hashemi
Kilo Sage

I have been asked to write a script, which will be schedule to run periodically. The script will search for all active contacts associated with a specific customer account, where the "location" field is blank. Ultimately, that list needs to be e-mailed to an external e-mail address.

 

I have created a script, job, event registration, and notification. When I execute the job, I can see that it runs because I have it sending gs.info() messages but I do not see any e-mail in the outbox (I am running this in Dev right now).

 

What am I missing? Here is what I have so far (please note that the message in "What will it contain" is not final):

 

Script:

// Select a random number and use it to track the sequence of log messages in this script.
var seq = Math.floor(Math.random() * 999999) + 1;

gs.info('[{0}]GetContacts: Beginning "User list - no location" scheduled job.', seq++);

// #region Initialize variables
var account = 'bb8083d14755f910291fe0e8036d43ea'
var userList = [];
// #endregion Initialize variables

// #region Get contacts
gs.info('[{0}]GetContacts: Query for contacts contacts at {1}.', seq++, account);
var grContact = new GlideRecord('customer_contact');
grContact.addEncodedQuery('account=' + account + '^user_nameISNOTEMPTY^locationISEMPTY^active=true');
grContact.query();

while (grContact.next()) {
    //gs.info('[{0}]GetContacts: Adding {1} to the list.', seq++, grContact.getValue('user_name'));

    userList.push(grContact.getValue('user_name'));
}

gs.info('[{0}]GetContacts: There are {1} contacts.', seq++, userList.length);
// #endregion Get contacts

// #region Trigger event
gs.eventQueue('send.genericemail', null, 'testuser@domain.com', userList.length);
// #endregion Trigger event

 

Event registration:

MikeHashemi_0-1716917370365.png

 

Notification:

MikeHashemi_1-1716917507304.png

MikeHashemi_2-1716917559369.png

MikeHashemi_3-1716917620611.png

 

 

1 ACCEPTED SOLUTION

Mike Hashemi
Kilo Sage

Turns out that the notification was in a non-global domain. I am not sure why that would cause this issue, but I:

  1. Exported the notification's record to XML
  2. Updated the domain and domain path fields
  3. Imported the XML
  4. Ran the scheduled job

 

This time, the e-mail was sent. This is the script I ended-up using:

// Select a random number and use it to track the sequence of log messages in this script.
var seq = Math.floor(Math.random() * 999999) + 1;

gs.info('[{0}]GetContacts: Beginning "Get change notification contacts" action in the "Change notification message" flow.', seq++);

// #region Initialize variables
var account = 'bb8083d14755f910291fe0e8036d43ea';
var recpients = 'user1@domain.com,user2@domain.com';
var userList = [];
// #endregion Initialize variables

// #region Get contacts
gs.info('[{0}]GetContacts: Query for active contacts contacts with no location, at {1}.', seq++, account);
var grContact = new GlideRecord('customer_contact');
grContact.addEncodedQuery('account=' + account + '^user_nameISNOTEMPTY^locationISEMPTY^active=true');
grContact.query();

while (grContact.next()) {
    //gs.info('[{0}]GetContacts: Adding {1} to the list.', seq++, grContact.getValue('user_name'));

    userList.push(grContact.getValue('user_name'));
}

gs.info('[{0}]GetContacts: There are {1} matching contacts.', seq++, userList.length);
// #endregion Get contacts

// #region Trigger event
if (userList.length > 0) {
    var userListStr = userList.join('\n');
    gs.info("Triggering event to send e-mail.");
    gs.eventQueue('send.genericemail', null, recpients, userListStr);
}
// #endregion Trigger event

View solution in original post

19 REPLIES 19

I assume that your account's email address is set to testuser@domain.com

 

Try adding another recipient in the notification via the Users field under the Who will receive tab and trigger the event again.

If it works, something's wrong with the parm1.

 

If that still doesn't work, remove the ${event.parm2} from the body and try again.

 

My guess is that something's wrong with the parm2 as the body appears to be empty.

Try changing the parm2 to some random integer and see if that works.

 

Thanks, I will make those modifications tomorrow.

After making the changes SanjivMeher suggested, I previewed the notification again and see that it looks different:

MikeHashemi_0-1717005870715.png

I added Paul M to the recipient list as you suggested. Still nothing in the Outbox.

That's because you are passing the length

userList.length

. Instead pass userList.toString();
Once you do that, check the email logs, if the events are generated with the right parameter.

If event is generated, email should trigger. 


Please mark this response as correct or helpful if it assisted you with your question.

I should not matter though, because userList.toString() and userList.length both end up being strings. That's why we see "969" in the notification preview (see screenshot in my response to James Chun). Regardless, I modified the script as you suggested, ran it, and checked the outbox. Still no expected e-mail.