Set Assignment Group for multiple email accounts forwarded in to SN

jlaue
Kilo Sage

Hi All -

We are looking to forward several more mailboxes into ServiceNow.   I want to use our current Inbound Email Action and just modify it to set the Assignment group based on which email box the email was sent to.   I have found the way to do this for one mailbox (from the wiki), however, I am not quite sure how I should write the code to get it working for multiple email accounts, but still have a catch-all at the end so that any emails that didn't match the other mailboxes would end up going to the HelpDesk.  

Here is what I have, but it is only working for the 2nd if statement.   If I swap the 2nd and the 1st if statements then it will still only work on the 2nd statement when testing emails to both mailboxes.   Note - the 2 mailboxes will still end up going in to the same Assignment Group.   I would like to get this working so when we add additional mailboxes we can just add to this one mail rule.

{

if (email.direct.indexOf('XXXXHelp@XXXXXX') >= 0){

current.assignment_group.setDisplayValue('XXXX Support');

}

if (email.direct.indexOf('YYYYSupport@YYYYY') >= 0){

current.assignment_group.setDisplayValue('XXXX Support');

}

else{

current.assignment_group.setDisplayValue('HelpDesk');

}

}

Thanks in advance!!

1 ACCEPTED SOLUTION

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Jiaue,



Update the code as below



if (email.direct.indexOf('XXXXHelp@XXXXXX') >= 0){


current.assignment_group.setDisplayValue('XXXX Support');


}




else if (email.direct.indexOf('YYYYSupport@YYYYY') >= 0){


current.assignment_group.setDisplayValue('XXXX Support');


}




else{


current.assignment_group.setDisplayValue('HelpDesk');


}


View solution in original post

3 REPLIES 3

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Jiaue,



Update the code as below



if (email.direct.indexOf('XXXXHelp@XXXXXX') >= 0){


current.assignment_group.setDisplayValue('XXXX Support');


}




else if (email.direct.indexOf('YYYYSupport@YYYYY') >= 0){


current.assignment_group.setDisplayValue('XXXX Support');


}




else{


current.assignment_group.setDisplayValue('HelpDesk');


}


Community Alums
Not applicable

The other way you can do this, that may prove more scalable in the future is to create a table to store the email address and assignment groups. Then on the inbound action, do a lookup on the table using the 'email.direct' object, and get/set the assignment group that way. This will save you the hassle of having to add more 'if/else' statements to the inbound action as more email addressed get used.



Something like this:



(function(){


        var aEmails = email.direct.toLowerCase().split(",");


        // Set Values based on email object parameters


        current.short_description = email.subject.toString();


        current.contact_type = "email";


        /** Loop through aEmails array and determine assignment group **/


        aEmails.forEach(function(sEmail) {


                  //gs.log("TM===> Address is " + sEmail);


                  if (email.body.assignment_group !== undefined) {


                            current.assignment_group.setDisplayValue(email.body.assignment_group);


                            /** Insert Record **/


                            current.insert();


                            event.state="stop_processing";


                  } else {


                            /** Query DB **/


                            var oGr = new GlideRecord("u_incoming_addresses");


                            oGr.addQuery("u_email_address", sEmail);


                            oGr.addQuery("u_table", "incident");


                            oGr.query();


                            if(oGr.next()){


                                      /** If we match, set the assignment group **/


                                      current.assignment_group.setDisplayValue(oGr.u_group);


                                      /** Insert Record **/


                                      current.insert();


                                      event.state="stop_processing";


                            }


                  }


        });


})();



Hope that helps!



Cheers,



Tim


Thanks for the super-fast response!     Appreciate your help!