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

Need help with an email inbound action script

BrianProvencher
Giga Guru

I am trying to create an inbound action. It should take the username from the subject line and submit a Catalog Item in the name of that user. It should check the u_employee_type field of the user record. If the value is Employee, it should generate Catalog item b150ee90879bb410a2efbaa6cebb3573. if not, generate catalog item 0d4d616f1b1bf0502cc987b3604bcbef. This is the script I have so far. I've run it through chatgpt and NowAssist several times, but I can't get it to work. I would appreciate any help.

 

(function runEmailAction(email, email_action, event) {
    // Extract username from subject line
    var subject = email.subject;
    var usernameMatch = subject.match(/Pending Termination:\s*(\w+)/);
    if (!usernameMatch || usernameMatch.length < 2) {
        gs.log("Inbound Email Action: Username not found in subject line.");
        return;
    }

    var username = usernameMatch[1];
    gs.log("Inbound Email Action: Found username - " + username);

    // Look up user record
    var userGR = new GlideRecord('sys_user');
    userGR.addQuery('user_name', username);
    userGR.query();

    if (!userGR.next()) {
        gs.log("Inbound Email Action: User not found - " + username);
        return;
    }

    // Determine catalog item based on employee type
    var catalogItemId;
    if (userGR.u_employee_type == 'Employee') {
        catalogItemId = 'b150ee90879bb410a2efbaa6cebb3573';
    } else {
        catalogItemId = '0d4d616f1b1bf0502cc987b3604bcbef';
    }

    // Create a new catalog request
    var cart = new Cart();
    cart.setUser(userGR.sys_id);
    cart.addItem(catalogItemId);
    cart.placeOrder();

    gs.log("Inbound Email Action: Catalog item " + catalogItemId + " requested for user " + username);
})(email, email_action, event);
18 REPLIES 18

@BrianProvencher 

did the inbound action got processed?

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

@Ankur Bawiskar It did. What should the target table be for the action?

@BrianProvencher 

you can use inbound action of type New on Request table

check this link

Submitting a catalog item from an Inbound Action 

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

nityabans27
Giga Guru

 

Hi @BrianProvencher 
🔎 Issues in your script

  1. Regex for username extraction

    • Your regex (\w+) only captures alphanumeric and underscore. If usernames contain dots (nitya.bansal), hyphens, etc., it won’t match.

  2. GlideRecord query

    • You’re querying user_name, which is correct — but confirm your subject actually contains the login name and not display name/email.

  3. Cart API usage

    • The ServiceNow Cart API isn’t always fully available in inbound email actions.

    • cart.setUser(userGR.sys_id) isn’t valid — setUser() expects a user object or GlideUser, not just sys_id.

    • cart.addItem(catalogItemId) usually requires at least one variable map, even if empty.


Corrected Script

(function runEmailAction(email, email_action, event) {
    try {
        // Extract username from subject line
        var subject = email.subject || '';
        var usernameMatch = subject.match(/Pending Termination:\s*([\w\.\-]+)/); // allow dot & dash
        if (!usernameMatch || usernameMatch.length < 2) {
            gs.log("Inbound Email Action: Username not found in subject line. Subject = " + subject);
            return;
        }

        var username = usernameMatch[1];
        gs.log("Inbound Email Action: Found username - " + username);

        // Look up user record
        var userGR = new GlideRecord('sys_user');
        userGR.addQuery('user_name', username);
        userGR.query();

        if (!userGR.next()) {
            gs.log("Inbound Email Action: User not found - " + username);
            return;
        }

        // Determine catalog item
        var catalogItemId = (userGR.u_employee_type == 'Employee')
            ? 'b150ee90879bb410a2efbaa6cebb3573'
            : '0d4d616f1b1bf0502cc987b3604bcbef';

        // Create a cart order
        var cart = new Cart();
        cart.setCartUser(userGR.sys_id); // correct method for setting user
        var item = cart.addItem(catalogItemId, {}); // add empty variables
        var rc = cart.placeOrder();

        gs.log("Inbound Email Action: Catalog item " + catalogItemId + " requested for user " + username + ". Request: " + rc.sys_id);
    } catch (e) {
        gs.error("Inbound Email Action: Error - " + e);
    }
})(email, email_action, event);

🔧 Key Fixes

  • Regex → Now allows dots and hyphens in usernames.

  • Cart API → Use setCartUser(userGR.sys_id) (not setUser).

  • addItem → Pass an empty object {} for variables, otherwise some instances reject the call.

  • Logging → Added more detail to troubleshoot subject parsing & request creation.

Ravi Gaurav
Giga Sage
Giga Sage

Hi @BrianProvencher 

(function runEmailAction(email, email_action, event) {

// Extract username from subject line
var subject = email.subject;
var usernameMatch = subject.match(/Pending Termination:\s*([\w\.\-]+)/); // allow dot & hyphen

if (!usernameMatch || usernameMatch.length < 2) {
gs.info("Inbound Email Action: Username not found in subject line. Subject: " + subject);
return;
}

var username = usernameMatch[1];
gs.info("Inbound Email Action: Found username - " + username);

// Look up user record
var userGR = new GlideRecord('sys_user');
userGR.addQuery('user_name', username);
userGR.query();

if (!userGR.next()) {
gs.info("Inbound Email Action: User not found - " + username);
return;
}

// Determine catalog item based on employee type
var catalogItemId;
if (userGR.u_employee_type + '' == 'Employee') { // string comparison
catalogItemId = 'b150ee90879bb410a2efbaa6cebb3573';
} else {
catalogItemId = '0d4d616f1b1bf0502cc987b3604bcbef';
}

// Create a new catalog request
var cart = new Cart();
cart.setCartUser(userGR.sys_id.toString()); // correct method
cart.addItem(catalogItemId);
var rc = cart.placeOrder();

gs.info("Inbound Email Action: Catalog item " + catalogItemId +
" requested for user " + username + " | Request " + rc);

})(email, email_action, event);

--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/