I have created the inbound action to create an HR Case for forwarded email.

Praju_123
Tera Contributor

I have created the inbound action to create an HR Case for forwarded email base on the email subject it will first check if we have any active case available in the hr case table have same short description 
suppose my mail subject is "FW: employee onboarding" then it will check the records of hr case table if it contain short description as same as email subject if not match then it will remove the "FW: " from the email subject and then again check the short description of all the hr cases if its not match then only i will allow to create new hr case.
below script I written for the inbound email action.

(function runAction(current, event, email, logger, classifier) {
    var subject = email.subject || '';
    var origEmail = email.origemail || '';
    var bodyText = email.body_text || email.body_html || '';
    var cleanedSubject = subject.replace(/^(FW:|RE:|FWD:)\s*/i, '').trim();

    if (subject) {
        var gr = new GlideRecord('sn_hr_core_case');
        gr.addQuery('short_description', subject);
        gr.query();
        if (gr.hasNext()) {
            // Duplicate found, do not create a new incident
            gs.log("Prajwal"+ subject);
            current.setAbortAction(true); // Prevents record creation
            return;
        }
    }
    if (cleanedSubject) {
        var gr1 = new GlideRecord('sn_hr_core_case');
        gr1.addQuery('short_description', cleanedSubject);
        gr1.query();
        if (gr1.hasNext()) {
            // Duplicate found, do not create a new incident
            gs.log("Prajwal" + cleanedSubject);
            current.setAbortAction(true); // Prevents record creation
            return;
        }
    }


    gs.log("Prajwal Outside the if loop");
    current.short_description = subject;
    current.description = bodyText;
    current.insert();

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

Ankur Bawiskar
Tera Patron
Tera Patron

@Praju_123 

if it's not working then what debugging did you perform?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

I used for debugging but still not able to fix the issue.

@Praju_123 

so what debugging you did and what's your findings?

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader