Inbound Action Script - Duplicate Interactions

mfoster817
Tera Contributor

Hello!

We have an Inbound Action that creates an Interaction when an email alert is received. This is alert is set up to repeat every 2 hours if the issue persists. 

 

I have a script that will check the Interaction table for a record with a short description that matches the email subject.

If an active Interaction record is found, add the email body to the interaction record as work notes.

If an interaction record is not found, create a new interaction.

 

The issue I am having is it is still creating an interaction regardless if there is a matching record. It does record the email body to the most recent record's work notes. 

 

Any assistance is much appreciated!

 

Here is the script:

 

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

    var userId = gs.getUserID();
    var emailSubject = email.subject.toString();
    var emailBodyText = email.body_text.toString();
   
    var gr = new GlideRecord("interaction");
    gr.addActiveQuery();                                      // Search only for active interactions
    gr.addQuery("short_description", emailSubject);           // Search for the same short description
    gr.query();
   
    if (gr.next()) {
        // Interaction found, update work notes
        gr.work_notes = emailBodyText;
        gr.update();
        gs.log('Interaction updated with work notes from email: ' + gr.sys_id, 'runAction');
    } else {
        // No matching interaction found, create a new one
        var newInteraction = new GlideRecord('interaction');
        newInteraction.initialize();
        newInteraction.short_description = emailSubject;
        newInteraction.u_description = emailBodyText;
        newInteraction.insert();
        gs.log('New interaction created from email: ' + newInteraction.sys_id, 'runAction');
    }
   
})(current, event, email, logger, classifier);
1 REPLY 1

Zach Koch
Giga Sage
Giga Sage

Can you confirm that it is finding a matching record? I would recommend after your gr.query() to add a log statement such as:

gs.info('record count # ' + gr.getRowCount())

this will confirm you are returning a record. My guess is you aren't finding a record with your query. If that is the case, log your emailSubject variable and ensure you know what value you have. Also, using gr as your variable name for a GlideRecord query is not best practice, so I would recommend changing that to something more fitting.

 

If this information helped resolve your issue, please remember to mark response correct and thumbs up to help future community members on this information, thanks!