Inbound Email Actions- How to set target

Kelvin16
Tera Contributor

I have a Inbound email actions to create catalog request item. While the request is created I want to add the inbound email to the request activity like the email sent below.

find_real_file.png

I found the "Target" is empty in sys_email.

find_real_file.png

Is there anyway to set the target from the inbound email action script so the email can be found in activity?

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

    // look for existing Email to Case
    var gr = new GlideRecord("sc_req_item");
    var str = email.subject;
    var text = str.substr(str.indexof("FW:"), 3);
    text = str.replace("FW:", "").trim();
    gs.info("[Email to Case]text = " + text);
    gr.addQuery("short_description", "CONTAINS", text);
    gr.query();
    if (gr.next()) {
        gs.info("[Email to Case] updating worknotes");
        gr.work_notes = email.body_text;
        gr.update();
    }
    //did not find it so we need to create a new request
    else {
        var cartId = GlideGuid.generate(null);
        var cart = new Cart(cartId);
        var item = cart.addItem('ce6ee3e3db70101010af14613996321a');
        cart.setVariable(item, 'issue_category', '5');
        cart.setVariable(item, 'issue_details', email.body_text);
        sys_email.target_table = current.getTableName();
        sys_email.instance = current.sys_id;
        sys_email.update();
        var rc = cart.placeOrder();
        updateRITM(rc.sys_id);
    }

    function updateRITM(req) {
        var ritm = new GlideRecord('sc_req_item');
        ritm.addQuery('request', req); //req is what we passed from the previous function. the sys_id of the request.
        ritm.query();
        while (ritm.next()) {
            ritm.due_date = email.body.start_date;
            ritm.comments = "Request by: " + email.origemail + "\n\n" + email.body_text;
            ritm.short_description = email.subject;
            ritm.u_is_email_create = 'true';
            ritm.contact_type = "Email";
            ritm.update();
        }
    }

    //now update the email record so it will show up in the record's activity formatter

    logger.logInfo("Processing 'Email Record'");
    //sys_email.target_table = "sc_req_item";
    sys_email.target_table = current.getTableName();
    sys_email.instance = current.sys_id;
    sys_email.update();

    event.state = "stop_processing";

})(current, event, email, logger, classifier);
1 ACCEPTED SOLUTION

asifnoor
Kilo Patron

Hi,

The email activity shows only when the inbound action has updated the current record. Only then it can link the email with the associated record. In your code, you are fetching the record using gliderecord query and updating the gr, but not the current.

Hence the email activity is not showing in that specific record as SN considers that as a regular update and not through inbound action.

Mark the comment as a correct answer and also helpful if this has answered your question.

View solution in original post

6 REPLIES 6

Kelvin16
Tera Contributor

Thanks for your help. I figure out the issue.

Any chance that you could detail what you did, to get it to work?