Email Script Processes in Workflow, but No Email Shows in Logs

appstorm
Tera Contributor

I am using the below script in Workflow Editor to generate an email response.  The script is supposed to capture the displayValue from student_name, which is a Reference that points to sys_user.  Based-on the u_display_name - which is the display field on the sys_user table, it should return the email record for that user.

// Ensure student_name is set and populated with a reference to sys_user
var studentSysUser = current.student_name;  // student_name references sys_user

// Check if student_name is populated
if (studentSysUser) {
    // Log to confirm we have a valid student_name reference
    gs.info('Student Name reference found: ' + studentSysUser);

    // Query the sys_user table using student_name.sys_id to retrieve the user record
    var userRecord = new GlideRecord('sys_user');
    if (userRecord.get(studentSysUser)) {  // Use the sys_id from student_name reference
        // Log to confirm we have found the sys_user record
        gs.info('Found sys_user record for: ' + userRecord.u_display_name);

        // Retrieve the email field from the sys_user record
        var recipientEmail = userRecord.email;  // Assuming the sys_user record has the 'email' field
        gs.info('Recipient Email: ' + recipientEmail);

        // Check if email is valid (in case it's missing)
        if (recipientEmail) {
            // Compose the email body
            var emailBody = 'Dear Student,\n\n';
            emailBody += 'There is a hold on your account, and you are advised to contact your academic advisor.\n\n';
            emailBody += 'The following holds have been placed on your account:\n' + current.student_information.u_hold_codes.getDisplayValue() + '\n\n';
            emailBody += 'Please reach out to your academic advisor for further assistance.\n\n';
            emailBody += 'Best regards,\nYour IvyOnline Team';

            // Create a new GlideEmailOutbound to send the email
            var mail = new GlideEmailOutbound();
            mail.setSubject('Action Required: Hold on Your Account');
            mail.setTo(recipientEmail);  // Send to the recipient's email (from sys_user)
            mail.setBody(emailBody);  // Set the email body

            // Send the email
            mail.send();
            gs.info('Email sent to: ' + recipientEmail);  // Log email sent
        } else {
            gs.error('No email found for the student: ' + studentSysUser);
        }
    } else {
        gs.error('No matching sys_user record found for student_name: ' + studentSysUser);
    }
} else {
    gs.error('student_name is not populated or invalid.');

 While the script is processing in the workflow, it is not generating an email in the logs.  From what I can see, there is no email match from student_name, even though the displayValue returns a valid response for u_display_name on both the form and the RITM.

 

 

4 REPLIES 4

Shivalika
Mega Sage

Hello @appstorm 

 

There could be issue with Glideemailoutbound due to mail settings in the system. 

 

Why don't you create an event and fire it from here ? Then use event triggered to send email notifications. Pass the recipients and records as parameters. 

 

Another option could be to directly create a record in sys_email table with all the details you have and state as "Send-Ready". 

 

This is provided all the information and variables that you are using is correct and logs are also generating correctly. 

 

Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket. 

 

Regards,

 

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY

Pascal Verdieu
Mega Sage

One of my recent "findings" (from scripting classes) is that you should make use of gr.getDisplayValue('fieldName') instead of gr.fieldName as the latter returns an object and not a string.

Therefore you should update this line as such:

var recipientEmail = userRecord.getDisplayValue('email');  // Assuming the sys_user record has the 'email' field

I've noted several SN native scripts using '' + gr.fieldName (or something similar) to force the result to become a string.

PS: Other comment from Shivalika may be relevant as well.

HTH

Stephencroc
Kilo Guru

It looks like in this section of code.  You are not completing the query.  

   // Query the sys_user table using student_name.sys_id to retrieve the user record
    var userRecord = new GlideRecord('sys_user');
    if (userRecord.get(studentSysUser)) {  // Use the sys_id from student_name reference
        // Log to confirm we have found the sys_user record
        gs.info('Found sys_user record for: ' + userRecord.u_display_name);

Initial look at this is there is no userRecord.query(); to actually run the GlideRecord Query;
Also you probably should filter the results a bit so you are not pulling the entire table in.

So this might work

   // Query the sys_user table using student_name.sys_id to retrieve the user record
    var userRecord = new GlideRecord('sys_user');
    userRecord.addQuery('sys_id', 'studentSysUser'); //filter the table to just what you need not the entire table
    userRecord.query();  //This runs the query
    if (userRecord.get(studentSysUser)) {  // Use the sys_id from student_name reference
        // Log to confirm we have found the sys_user record
        gs.info('Found sys_user record for: ' + userRecord.u_display_name);

I think the problem is your query is not running.

No, the userRecord.get(studentSysUser), should get him the user; besides, he would see that in the logs.  I'm pretty sure it is the object returned instead of a sting that messes up the mail.setTo(recipientEmail) line as the mail.setTo doesn't now what to do with the object...

He could as well convert that line as:

mail.setTo('' + recipientEmail)

 might work...