The Zurich release has arrived! Interested in new features and functionalities? Click here for more

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);
2 REPLIES 2

Rafael Batistot
Kilo Patron

Hi @BrianProvencher 

i've found some issues in your code:

 

  1. Cart.setUser() doesn’t exist.
  2. Need to cast u_employee_type to string for comparison.
  3. Must update requested_for after placeOrder().

Here’s a cleaned-up script you can drop into your inbound action:

(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();
    var cartItem = cart.addItem(catalogItemId);

    // Place the order
    var rc = cart.placeOrder();

    // Update the request to set "Requested For" as the found user
    if (rc) {
        var req = new GlideRecord('sc_request');
        if (req.get(rc.sys_id)) {
            req.requested_for = userGR.sys_id;
            req.update();
        }
    }

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

@Rafael Batistot thank you. I tried your fixes and am getting this error: 

Skipping script 'Auto Offboarding', a suitable GlideRecord not found

Any ideas?