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

@Ravi Gaurav I tried this version of the script and I'm getting this: did not create or update sc_request using current

@BrianProvencher 

that's fine.

But did it create new RITM and REQ?

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

@Ankur Bawiskar No it did not

@BrianProvencher 

are you sure the inbound action got processed?

if yes then did you debug the script by adding extra gs.info()?

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