How can I write an inbound email action to populate the watch_list field with all TO/CC

shareef_223
Tera Contributor

 

Requirement:
I need to configure ServiceNow so that whenever an email is received:

  • A case should be created.

  • The case should be linked to the customer account and populated with customer details.

  • Any To and CC email addresses should be added to the customer comments watch list.

Field Mapping:

  • Subject → short_description

  • Body → description

  • TO/CC → watch_list

Current Approach:
I am using the following logic in my inbound action script:

 

 
current.short_description = email.subject || "New email case"; current.description = email.body_text || ""; current.contact_type = "email"; current.account = acctGR.sys_id; // Step 3: Lookup contact by From email + Account var contactGR = new GlideRecord("customer_contact"); contactGR.addQuery("email", (email.from || "").toLowerCase()); contactGR.addQuery("account", acctGR.sys_id); contactGR.query(); if (contactGR.next()) { current.contact = contactGR.sys_id; } // Step 4: Add To/CC recipients to Watch List var allRecipients = []; if (email.recipients_array && email.recipients_array.length > 0) { allRecipients = email.recipients_array; } var usersToAdd = []; for (var i = 0; i < allRecipients.length; i++) { var recEmail = (allRecipients[i] || "").toLowerCase().trim(); if (!recEmail) continue; var userGR = new GlideRecord("sys_user"); userGR.addQuery("email", recEmail); userGR.query(); if (userGR.next()) { usersToAdd.push(userGR.sys_id.toString()); } } if (usersToAdd.length > 0) { var currentWatchlist = current.getValue("watch_list") || ""; var watchlistArray = currentWatchlist.split(",").filter(Boolean); var combined = watchlistArray.concat(usersToAdd); var uniqueList = []; var seen = {}; for (var j = 0; j < combined.length; j++) { if (!seen[combined[j]]) { uniqueList.push(combined[j]); seen[combined[j]] = true; } } current.setValue("watch_list", uniqueList.join(",")); // Also copy to work_notes_list current.setValue("work_notes_list", uniqueList.join(",")); } // Step 5: Insert PA record current.insert();
 

Issue:
The target case record is being created successfully, but the watch list is not being updated.

Request for Help:
Could anyone please suggest how to correctly populate the watch list (and possibly work notes list) with To/CC recipients?

3 REPLIES 3

Bhuvan
Kilo Patron

@shareef_223 

 

You can use Flow Designer flow and it will be easier to create this with low-code/no-code approach

 

There is an out of box Flow that can create a case from inbound email. You can map additional field in the action or if action is read-only you can duplicate the action and add 'watch list' mapping as per your requirements from dot-walking email fields. You can use the action in your flow and it should work fine.

Bhuvan_0-1757565566740.png

If this helped to answer your query, please mark it helpful & accept the solution.

 

Thanks,

Bhuvan

Ankur Bawiskar
Tera Patron
Tera Patron

@shareef_223 

did you debug and see?

try this

-> watch list field requires comma separated sysIds

// Step 1: Field mapping
current.short_description = email.subject || "New email case";
current.description = email.body_text || "";
current.contact_type = "email";

// Step 2: Lookup account (assumes acctGR is set earlier)
current.account = acctGR.sys_id;

// Step 3: Lookup contact by From email + Account
var contactGR = new GlideRecord("customer_contact");
contactGR.addQuery("email", (email.from || "").toLowerCase());
contactGR.addQuery("account", acctGR.sys_id);
contactGR.query();
if (contactGR.next()) {
    current.contact = contactGR.sys_id;
}

// Step 4: Add To/CC recipients to Watch List and Work Notes List
var allRecipients = [];
if (email.recipients_array && email.recipients_array.length > 0) {
    allRecipients = email.recipients_array;
}
var usersToAdd = [];
for (var i = 0; i < allRecipients.length; i++) {
    var recEmail = (allRecipients[i] || "").toLowerCase().trim();
    if (!recEmail) continue;
    var userGR = new GlideRecord("sys_user");
    userGR.addQuery("email", recEmail);
    userGR.query();
    if (userGR.next()) {
        usersToAdd.push(userGR.sys_id.toString());
    }
}
// Deduplicate and merge with any existing watch_list values
if (usersToAdd.length > 0) {
    var currentWatchlist = current.getValue("watch_list") || "";
    var watchlistArray = currentWatchlist.split(",").filter(Boolean);
    var combined = watchlistArray.concat(usersToAdd);
    var uniqueList = [];
    var seen = {};
    for (var j = 0; j < combined.length; j++) {
        if (!seen[combined[j]]) {
            uniqueList.push(combined[j]);
            seen[combined[j]] = true;
        }
    }
    var sysIdString = uniqueList.join(",");
    current.setValue("watch_list", sysIdString); // watch_list expects comma-separated sys_ids[1][2]
    current.setValue("work_notes_list", sysIdString); // work_notes_list (if needed)
}

// Step 5: Insert the record
current.insert();

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

Srikanth_9
Tera Contributor

Hello @shareef_223 ,

 

As you requested to correctly auto-populate the watch list and also work notes list with TO/CC recipients

 

Field Mapping:

  • Subject → short_description

  • Body → description

  • TO/CC → watch_list & work_notes_list

Please use the below provided script for Inbound Action Script:

 

 

 // Populate case basic fields
 current.short_description = email.subject || "New email case";
 current.description = email.body_text || "";
 current.contact_type = "email";

 // Link primary contact (From email)
 var contactGR = new GlideRecord("customer_contact");
 contactGR.addQuery("email", email.from.toLowerCase());
 contactGR.query();
 if (contactGR.next()) {
     current.contact = contactGR.sys_id;
     current.account = contactGR.account;
 }

 // Collect recipients from TO & CC
 var recipients = [];
 var toList = email.headers["To"];
 if (toList) recipients = recipients.concat(toList.split(/[,;]/));

 var ccList = email.headers["Cc"];
 if (ccList) recipients = recipients.concat(ccList.split(/[,;]/));

 var contactsToAdd = [];

 // Resolve each recipient
 for (var i = 0; i < recipients.length; i++) {
     var addr = recipients[i].trim().toLowerCase();
     if (!addr) continue;

     // First check in customer_contact
     var ccContactGR = new GlideRecord("customer_contact");
     ccContactGR.addQuery("email", addr);
     ccContactGR.query();
     if (ccContactGR.next()) {
         contactsToAdd.push(ccContactGR.sys_id.toString());
         continue;
     }

     // If not found, check in sys_user
     var userGR = new GlideRecord("sys_user");
     userGR.addQuery("email", addr);
     userGR.query();
     if (userGR.next()) {
         contactsToAdd.push(userGR.sys_id.toString());
     }
 }

 // Update WATCH LIST
 var existingWatch = current.watch_list ? current.watch_list.split(",") : [];
 var combinedWatch = existingWatch.concat(contactsToAdd);

 var uniqueWatch = [];
 for (var j = 0; j < combinedWatch.length; j++) {
     if (combinedWatch[j] && uniqueWatch.indexOf(combinedWatch[j]) === -1) {
         uniqueWatch.push(combinedWatch[j]);
     }
 }
 current.setValue("watch_list", uniqueWatch.join(","));

 // Update WORK NOTES LIST
 var existingNotes = current.work_notes_list ? current.work_notes_list.split(",") : [];
 var combinedNotes = existingNotes.concat(contactsToAdd);

 var uniqueNotes = [];
 for (var k = 0; k < combinedNotes.length; k++) {
     if (combinedNotes[k] && uniqueNotes.indexOf(combinedNotes[k]) === -1) {
         uniqueNotes.push(combinedNotes[k]);
     }
 }
 current.setValue("work_notes_list", uniqueNotes.join(","));
 
 
If required use the below script for debugging:
 
 // Optional logging for debugging
 gs.log ("Inbound Email Action: Case " + current.number + " | Watch List: " + uniqueWatch.join(",") + " | Work Notes List: " + uniqueNotes.join(","), "InboundEmailAction");

 
If you need any other customizations or facing errors, please reply to me back will check it. If the provided solution is working, please Accept as Solution and hit the Helpful. 
 
Thanks & Regards,
Akula Srikanth.