Using username from inbound email action to fill in Caller field.

gnunez
Kilo Guru

I currently use an inbound email action that captures the email in a web form outside of ServiceNow, but if the email isn't in a certain format it won't recognize who the caller is and will leave as blank. To avoid this I am trying to find a way to add to the script so it can capture the information using the username field. Is there any way to do this?

Thanks again!

1 ACCEPTED SOLUTION

Caller field is captured now. Had to add value pairs and then add that to the inbound action for Create Incident.


View solution in original post

44 REPLIES 44

Yes, you can test for the "alternate" emails and try to match the user. Are the alternate emails already in the user profile? if so, you can try matching them in turn in your script. Please let me know if you need help with that.


Yes the alternate emails are in the profiles. Yes, if you could help with the code for matching them it would be greatly appreciated!


Please insert the below script between lines 3 and 4 in your script above - you'll need to replace 'alt_eamil_1' and 'alt_email_2' with your field names from sys_user:



if (gs.getUserDisplayName() == 'Guest') {   // sender couldn't be resolved to a user


  var email_only = /[\w.-]+@[\w.-]+\.[a-z]+/i.exec(email.origemail);   // extract only email (not name) from email.origemail


  if (email_only) {


      var grUser = new GlideRecord('sys_user');


      grUser.addQuery('alt_email_1=' + email_only + '^ORalt_email_2=' + email_only);   // find matching alt emails


      grUser.query();


      if (grUser.next()) current.caller_id = grUser.sys_id.toString();


  }


}



This assumes you have 2 other alt emails. If all fails, the user will remain as "Guest".



UPDATED to extract only the email address from email.origemail and simplified using Encoded Query.


Do you think your code will still work if there is only one email field or would you recommend creating two additional ones that are unique to the additional email formats?



This is in place:


find_real_file.png


How do you store multiple emails in a single field?