We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

Update undelivered and OOO emails to the custom table

Aruna13
Tera Contributor

Hi,

 

I have a custom table where i want to update undelivered and OOO emails to the record in activity log.

Below is my business rule i wrote and the conditions.

BR:

(function executeRule(current, previous /*null when async*/ ) {

    var body = current.body.toString();
    if (body.indexOf("DKIM-Filter:") > -1) {
        var str = body.substring(body.indexOf("DKIM-Filter:"));

        var subject = str.substring(str.indexOf("Subject:") + 8);

        var mime = subject.substring(subject.indexOf("MIME-Version:"));
        var sub = subject.replace(mime, '').trim();

        var gr = new GlideRecord('sys_email');
        gr.addQuery('sys_created_onRELATIVEGT@minute@ago@5');
        gr.addQuery('type', 'sent');
        gr.query();
        while (gr.next()) {
            if (((gr.subject.toString()).localeCompare(sub.toString())) || (gr.subject.toString() == sub.toString())) {
                if (current.instance) {
                    current.type = 'received';
                } else {
                    current.type = 'received';
                    current.target_table = 'u_sr_shared_finance';//custom table
                    current.instance = gr.instance;
                }
            }
        }
    } else {
        var emailStart = body.substring(body.indexOf('The mail system') + 27);
        var emailEnd = body.substring(body.indexOf('>: host'));
        var undeliverEmail = emailStart.replace(emailEnd, '').trim();
        var gre = new GlideRecord('sys_email');
        gre.addQuery('sys_created_onRELATIVEGT@minute@ago@5');
        gre.addQuery('type', 'sent');
        gre.addEncodedQuery('recipientsLIKE' + undeliverEmail);
        gre.query();
        if (gre.next()) {
            current.type = 'received';
            current.target_table = 'u_sr_shared_finance';
            current.instance = gre.instance;
        }
    }

    var ap = new GlideRecord('u_sr_shared_finance');
    ap.addQuery('sys_id', current.instance);
    ap.addQuery('state', '4');
    ap.query();
    if(ap.next()){
        ap.state = '9';
        ap.update();
    }
})(current, previous);
 
Conditions:(new ToCheckRecipients()).checkRecipientAP(current.direct) //this will check the recipients are valid
 
Aruna13_0-1717424031947.png

 

I dont know what is wrong here, but it is working for Repluy/ReplyAll/Forward client templates and not the email template for the custom table.

Can anyone please check and le me know what is the issue here? Please help me since i just started learning scripting.

 

@Ankur Bawiskar @Jitendra diwak1 can you please help

Thanks in Advance!!

0 REPLIES 0