Create incidents from external users by mail

Hichem
Mega Expert

Hello,

I'm trying to allow external users to create incidents through emails (whitout having accounts).

I've found in this forum below code snippet.
The inbound email action successfully create an incident, but nothing in user and caller fields.
What I want is to populate these fields with Guest user if email sender is not found in the sys_user table and add in a specific field "u_guest_email" sender email.

Any help would be greatly appreciated

Thanks

-----------------------------------
current.comments = "received from: " + email.origemail + "\n\n" + email.body_text;
current.short_description = email.subject;

var eto = email.to.toLowerCase().toString();
// var sid = gs.createUser(email.from);
var sid;
var rec = new GlideRecord('sys_user');
rec.addQuery('email',email.from);
rec.query();
rec.next();

if(rec.sys_id != "") {
sid = rec.sys_id;
} else {
gs.getUser();
sid = gs.getUserById('guest').sys_id;
}

current.caller_id = sid;
current.opened_by = sid;
//current.watch_list = email.from;
current.state = 1;
//current.notify = 2;
current.contact_type = "email";

//if(eto.indexOf("accounts") > -1){
//current.assignment_group.setDisplayValue('Account Support');
//}
//else{
//current.assignment_group.setDisplayValue('Consulting');
//}

current.insert();
-----------------------------------

17 REPLIES 17

That's a nice workaround! Glad you got it working.

Take care.


While this will work, you might also consider using the 'Watch list' field provided out of box to populate the email address in.


Hi Hichem,



I am trying to do something similar for our HR Team but I do not understand code too well. Are you able to send the entire script you used and I can probable work out where the HR piece fits in.



Thanks


Hi Clinton,



Hereunder what I have done:



1- create an inbound email action


name: what you want


active: checked


type: new


target table: incident


conditions:   in my example to identify what email can generate an incident I have the following condition that must be true, the email subject must start with [moogly.demo]


email.subject.indexOf("[moogly.demo]") == 0


script:


var body_text = email.body_text;


var short_description = "Email Auto-Open incident";


current.incident_state = '1';


current.opened_at = gs.nowDateTime();


current.impact = 2;


current.urgency = 2;


current.location = "XXXXX";


current.cmdb_ci = "XXXXX";


current.u_application = "XXXXX";


current.short_description = email.subject;


current.description = "Subject: Email Auto-Open incident \n\n" + email.body_text;


current.category = "Request";


current.subcategory = "Question";


current.assignment_group = "put here your group sysID";


current.u_source = "Email";


//current.opened_by = "Google API";


//current.caller_id = "Google API";


//current.u_user_id = "Google API";


current.watch_list = email.origemail;


current.comments = "received from: " + email.origemail + "\n\n - " + email.from + "\n\n - " + email.copied + "\n\n - " + email.direct + "\n\n - " + email.to + email.body_text;


current.insert();




I voluntarily put here in the comments different variables available to have all email addresses received in the original email in order to let you adapt it to your needs



You can see that I have added in the script the email sender in the watchlist of the incident.


Doing that the original sender will be notified when his incident is updated !!



2 - Create an Email notification



name: what you want


table: incident


type: email


active: checked


*when to send*


send when: event is fired


event name: incident.inserted


weight: 20


condition: if you have any specific condition that must be met, for example I have added the assignment group


*who will receive*


user/groups in fields: caller (if needed) + watch list


send event to creator: checked


*what it will contain*


content type: html and plain text


from: the name that will be displayed in the mail when notifications will be send to requesters for example : "Helpdesk Team"


reply to: in our case, senders don't send emails directly to our servicenow instance but through another email dedicated to these kind of requests, you can put any email address if needed here.


subject:


Incident ${number} -- Notification of creation



message
:


Your incident number ${URI_REF} about "${short_description}", has been created and assigned to ${assignment_group.name}.



Please for any update, you can answer to this email.



Thanks



If you want to manage incident updates in both directions, when senders send an email to update existing incident and when IT update an incident, you will need to create other inbound email actions and email notifications to cover these cases.



I've tried this process sending emails from an external account (without having servicenow account) and it's working fine.


It can certainly be improved but it works


Hi,



Thanks for your reply.



I managed to get this working. Thanks for your help.