How to send emails from list

Pranavi07
Tera Expert

Hello,

we have a requirement where agent wants to send emails to the consumers from the list view. What we need is a UI action (list choice) and when we navigate to the table and select certain records from list and then click on this UI action, it should show a pop-up and in the pop up we need to have certain fields like "Subject", "Reply to", "body"(html), and once these details are filled, the emails should be send to all the consumers in the selected records. How can we do this?

 

I have seen a few similar posts where it is suggested that we create an event and configure a notification and fire this event from the UI action that is created. The problem with this is that, we can only have predefined subject and matter with this notification/event approach. Instead , our users want something to send dynamic subject and matter to the consumers present in the filtered tickets on the list.

3 REPLIES 3

Ratnakar7
Mega Sage
Mega Sage

Hi @Pranavi07 ,

 

To achieve this requirement, you can create a UI action that opens a pop-up with fields to enter the email subject, body, reply-to address, and other relevant fields. Once the user fills in the required details and clicks on the send button, you can use a server-side script to fetch the selected records' email addresses from the list view and send emails to them using the SMTP email functionality provided by ServiceNow.

Here's a sample code snippet that you can use in your UI action's server-side script:

 

 

function sendEmail() {
    var grList = new GlideRecord(current.listTableName);
    grList.addQuery('sys_id', 'IN', current.sysIds.join());
    grList.query();
    var emailList = '';
    while (grList.next()) {
        var email = grList.getValue('email_field_name'); // replace with the actual field name for email address
        if (email) {
            emailList += email + ',';
        }
    }
    var email = new GlideEmailOutbound();
    email.setSubject(g_form.getValue('subject_field_name')); // replace with the actual field name for email subject
    email.setFrom(g_form.getValue('reply_to_field_name')); // replace with the actual field name for reply-to address
    email.addRecipient(emailList);
    email.setBody(g_form.getValue('body_field_name')); // replace with the actual field name for email body
    email.send();
}

 

 

 

In the above code, replace the current.listTableName with the actual table name from where you want to fetch the email addresses. Also, replace the email_field_name, subject_field_name, reply_to_field_name, and body_field_name with the actual field names in your table.

You can call the sendEmail() function from the UI action's "OnClick" script. This will fetch the email addresses of the selected records and send emails to them using the entered subject, body, and reply-to address.

 

 

If my response was helpful in resolving the issue, please consider accepting it as a solution by clicking on the Accept solution button and giving it a thumbs up 👍. This will benefit others who may have a similar question in the future.

 

Thank you!

Ratnakar

Hello @Ratnakar7 ,

Thanks for the reply, but here we need to send emails with some random subject and body which is not taken from any of the fields. Let's say there are 10 tickets on a whole and I have selected some 4 among them, I will need to send subject as "Testsubject1" for these 4 tickets and lets say some other time I chose few other tickets (that might include these 4 or might not), I will need to send with another subject (test subject2) and body. Is this achievable?

Hi @Pranavi07 ,

 

Yes, it is achievable. Instead of using the g_form.getValue() method to get the subject and body values from fields, you can prompt the user to enter the subject and body text in the pop-up and store them in variables. 

You can write UI page for prompt to get subject/body and call UI Page with UI action like below:

-->> UI Action:

Name: Send Email
Table: <your_table_name>
Client: true
Onclick: openEmailPage()

-->> UI Page:

<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
  <g:evaluate>
    function sendEmail() {
      var grList = new GlideRecord(current.listTableName);
      grList.addQuery('sys_id', 'IN', current.sysIds.join());
      grList.query();
      var emailList = '';
      while (grList.next()) {
        var email = grList.getValue('email_field_name'); // replace with the actual field name for email address
        if (email) {
          emailList += email + ',';
        }
      }
      var email = new GlideEmailOutbound();
      email.setSubject(g_form.getValue('subject_field_name')); // replace with the actual field name for email subject
      email.setFrom(g_form.getValue('reply_to_field_name')); // replace with the actual field name for reply-to address
      email.addRecipient(emailList);
      email.setBody(g_form.getValue('body_field_name')); // replace with the actual field name for email body
      email.send();
    }
  </g:evaluate>
  <j:jelly if="${!gs.hasRole('admin')}">
    <p>You do not have permission to perform this action.</p>
  </j:jelly>
  <j:jelly if="${gs.hasRole('admin')}">
    <div class="form-group">
      <label for="subject">Email Subject:</label>
      <input type="text" name="subject" id="subject" class="form-control">
    </div>
    <div class="form-group">
      <label for="reply-to">Reply-To Address:</label>
      <input type="email" name="reply-to" id="reply-to" class="form-control">
    </div>
    <div class="form-group">
      <label for="body">Email Body:</label>
      <textarea name="body" id="body" class="form-control"></textarea>
    </div>
    <div class="form-group">
      <button type="button" class="btn btn-primary" onclick="sendEmail()">Send Email</button>
    </div>
  </j:jelly>
</j:jelly>