Inbound email action not always finding the senders details

Russell Abbott
Kilo Sage

Here's the inbound email action script that I'm struggling to find issue with.

Occasionally the record is generated as expected, more often than not the record is generated using the guest account information as though the user details cannot be parsed.

Is the + "" necessary here? The email variable is already a string..

 

(function runAction(/*GlideRecord*/ current, /*GlideRecord*/ event, /*EmailWrapper*/ email, /*ScopedEmailLogger*/ logger, /*EmailClassifier*/ classifier) {

	//To fetch the Requested for in a forwarded email
    var fwd_user = "";
    try {
        fwd_user = email.body_text.split('From:')[1].split('<')[1].split('>')[0]; //parsing the forwarded user email from the email body
    } catch (err) {
        fwd_user = sys_email.user_id.email + "";
    }

    var id = GlideGuid.generate(null); // Generate a new GUID.
    var cart = new Cart(id); // Instantiate a fresh cart
    var RITM = cart.addItem('8ac5fa60db4f68106fb250c7f49619da'); // The value used here is the sys_id of New HR Request catalog item

    cart.setVariable(RITM, 'subject', email.subject);

    var grEmail = new GlideRecord("sys_user");
    grEmail.addQuery("email", fwd_user + "");
    grEmail.query();
    if (grEmail.next()) {
        cart.setVariable(RITM, 'kpe_requested_for', grEmail.sys_id);
        cart.setVariable(RITM, 'kpe_requested_by', sys_email.user_id + "");

    } else {
		cart.setVariable(RITM, 'guest_email', fwd_user + ""); //storing the email of guest user
        cart.setVariable(RITM, 'kpe_requested_for', sys_email.user_id + "");
        cart.setVariable(RITM, 'kpe_requested_by', sys_email.user_id + "");
    }

    cart.setVariable(RITM, 'details', email.body_text);
	cart.setVariable(RITM, 'assignment_group', 'HR-BENEFITS');//setting Assignment group as HR-BENEFITS

    var request = cart.placeOrder(); // The placeOrder() function returns a GlideRecord for the sc_request record it generates
    sys_email.target_table = 'sc_request';
    sys_email.instance = request.sys_id;
    sys_email.update();
    current = request;
    current.short_description = "HR Request";
    current.update();

})(current, event, email, logger, classifier);
3 REPLIES 3

Maik Skoddow
Tera Patron
Tera Patron

Hi

unfortunately it's not really clear what your issue is.

As you are using the sys_email.user_id reference in your script, you should be aware of the following documentation page User impersonations and inbound actions . This probably explains the "guest" user. 

 

And to answer your second question:
If you are accessing a field value via dot-walk (like sys_email.user_id.email) then you ALWAYS access a reference to an object and not the value itself. This is one of the biggest misunderstandings among beginners. The reason why in most of the cases you get the real value at the end is the automatic type conversion in the background of the JavaScript engine. And the fallback of all conversions is invoking the method .toString() which returns the string representation of the field. 

So whenever you concatenate a dot-walked field (reference to an object) like

fwd_user = sys_email.user_id.email + "";

the .toString() method of .email is invoked to match the same data type of the empty string "". At the end it's working, but it is always also an indicator for someone who is not an experienced developer.

@Maik SkoddowThank you for your feedback. May I offer a little more clarification as to the issue? This inbound action was put in place by a team that set up our instance for golive.

What seems to happen, and I'm struggling to figure out is, each time an inbound email comes in and invokes this script it only detects the userID about %25 of the time.

I could have the same user send 10 emails to invoke this script, and it will only correctly identify the userID 3 times. The other 7, it will fail that and fall back to guest user.

So something is either running before this script? after it maybe? or the script itself has something at fault

I hope that makes more sense

 

Thanks!

Chetan Mahajan
Kilo Sage
Kilo Sage

Hello @Russell Abbott ,

In some places, you are using + "" to concatenate strings. For instance, fwd_user + "" and sys_email.user_id + "". If fwd_user and sys_email.user_id are already strings, this concatenation isn't necessary. It's redundant and doesn't add any value to the script.

I would recommend to add Some gs.log(""); in your script where you are trying to fetch user id. So as you said sometime its detect and some time not, so it might help you to find exact roadblock.

Kindly mark correct and helpful if applicable