Need inbound action to only create new user if TO: is a specific address

JPlace
Giga Contributor

We have a couple aliases pointed at ServiceNow, but only want users automatically created if someone emails a specific one. Is this possible to do with an inbound action without enabling the mail property "Automatically create users for incoming emails from trusted domains"?

Thank you in advance,

-John

1 ACCEPTED SOLUTION

Brian Dailey1
Kilo Sage

Hi John,



You could definitely use your Inbound Email Action (IEA) script to create a user based on this condition instead of relying on the automated system property.   In an IEA script you have access to the object called 'email', and the variable 'email.direct' contains a comma-separated list of all addresses in the To: field of the message.



Check for your condition (specific address) somewhere in the beginning of your IEA script, and generate a new user accordingly:



  if(email.direct.indexOf('mySpecificAddress@domain.com') != -1){


            //Create a new user


            var newUser = new GlideRecord('sys_user');


            newUser.initialize();


            newUser.email = email.origemail;


            ... (go through and set the new user's properties as you see fit for your organization)



            // Capture the new user's sys_id on record insert and use it for creating the task (INC/REQ) record


            var newUserID = newUser.insert();


            current.opened_by = newUserID;


            current.caller_id = newUserID; //Note: caller_id is used for Incidents.   Requests would use requested_for or some other field.


    }


    ... (proceed with the rest of your Inbound ticket creation action)




EDIT:Instead of doing this in your Inbound Email Action script, I would suggest doing it as a before Business Rule on the Email table ([sys_email]) to catch a message when it gets entered rather than adding code to all of your IEA scripts.   For incoming emails when a sender is unrecognized, the sender's email address is stored in current.user and current.user_id is empty, so that can be in your criteria.



The *email* object is not available to you in a BR, but you can check either current.to or current.recipients for your specific address just as above.   And just remember that you're dealing with the email record itself and not the task generated from it when you update values on *current*.



    if(current.recipients.indexOf('mySpecificAddress@domain.com') != -1){


    ...


            current.user_id = newUserID;


    }





Good luck,


-Brian


View solution in original post

3 REPLIES 3

Brian Dailey1
Kilo Sage

Hi John,



You could definitely use your Inbound Email Action (IEA) script to create a user based on this condition instead of relying on the automated system property.   In an IEA script you have access to the object called 'email', and the variable 'email.direct' contains a comma-separated list of all addresses in the To: field of the message.



Check for your condition (specific address) somewhere in the beginning of your IEA script, and generate a new user accordingly:



  if(email.direct.indexOf('mySpecificAddress@domain.com') != -1){


            //Create a new user


            var newUser = new GlideRecord('sys_user');


            newUser.initialize();


            newUser.email = email.origemail;


            ... (go through and set the new user's properties as you see fit for your organization)



            // Capture the new user's sys_id on record insert and use it for creating the task (INC/REQ) record


            var newUserID = newUser.insert();


            current.opened_by = newUserID;


            current.caller_id = newUserID; //Note: caller_id is used for Incidents.   Requests would use requested_for or some other field.


    }


    ... (proceed with the rest of your Inbound ticket creation action)




EDIT:Instead of doing this in your Inbound Email Action script, I would suggest doing it as a before Business Rule on the Email table ([sys_email]) to catch a message when it gets entered rather than adding code to all of your IEA scripts.   For incoming emails when a sender is unrecognized, the sender's email address is stored in current.user and current.user_id is empty, so that can be in your criteria.



The *email* object is not available to you in a BR, but you can check either current.to or current.recipients for your specific address just as above.   And just remember that you're dealing with the email record itself and not the task generated from it when you update values on *current*.



    if(current.recipients.indexOf('mySpecificAddress@domain.com') != -1){


    ...


            current.user_id = newUserID;


    }





Good luck,


-Brian


Thank you Brian.   We decided we didn't want external users in our system, so we will be leveraging the guest account. "ServiceNow runs inbound actions from users who do not match an existing user by impersonating the Guest user."


Yep, that's how I have it setup too.   Don't want random accounts being created, or multiple accounts for the same person with two addresses



-Brian