Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

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);
If you found this response helpful, please mark it as Helpful. If it fully answered your question, consider marking it as Correct. Doing so helps other users find accurate and useful information more easily.

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

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

Any ideas?

 

Hi @BrianProvencher 

 

I’ve found this suport article about this  issue 

 

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0759187

If you found this response helpful, please mark it as Helpful. If it fully answered your question, consider marking it as Correct. Doing so helps other users find accurate and useful information more easily.

@Rafael Batistot I've tried using the email table as a target table, I've tried using RITM and REQ as target table, I've tried leaving the target table blank. They all result in the same problem.