Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

Need help with an email inbound action script

BrianProvencher
Tera 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
Tera Guru

Bump

Ankur Bawiskar
Tera Patron

@BrianProvencher 

are you still looking for answer to this?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 10x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

@Ankur Bawiskar  Yes I am

@BrianProvencher 

this should work provided you are extracting the correct name and provided the inbound action is getting processed

var subject = email.subject;
var usernameMatch = subject.match(/Pending Termination:\s*(\w+)/);
if (!usernameMatch || usernameMatch.length < 2) {
    gs.info("Inbound Email Action: Username not found in subject.");
    return;
}
var username = usernameMatch[1];
gs.info("Inbound Email Action: Found username - " + username);

// Look up the 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;
}

// Choose the catalog item based on employee type
var catalogItemId = (userGR.u_employee_type == 'Employee') ?
    'b150ee90879bb410a2efbaa6cebb3573' :
    '0d4d616f1b1bf0502cc987b3604bcbef';

var cart = new sn_sc.CartJS();
cart.setRequestedFor(userGR.getUniqueValue());
var item = {
    'sysparm_id': catalogItemId,
    'sysparm_quantity': '1',
    'variables': {
        'user': '7282abf03710200044e0bfc8bcbe5d03',
    }
};
var cartDetails = cart.addToCart(item);
var checkoutInfo = cart.checkoutCart();

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

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 10x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

@Ankur Bawiskar  still got: Skipping script 'Auto Offboarding', a suitable GlideRecord not found