I need to include in my inbound email action a way to pull the user from email and use this user as the requested for

christina20
Kilo Guru

I have a script to pull different information from the email, and it works. Except for being able to pull the Requested for.  It is putting the email address of the sender as the requested for.  I need it to bypass pulling the email address of sender and instead pull from the email.  In the email body the user is Direct Supervisor: Amy Smith.  This user is set up as a user within ServiceNow. Any help would be greatly appreciated.  I am new to this as well. And here is my script:

createRequest();

function createRequest() {

    var cart = new Cart(); //calling the cart API  

    var item = cart.addItem('0e4f075b1b8518507c1c0d4acd4bcb13');
    var item2 = cart.addItem('47aadb171bc518507c1c0d4acd4bcb26');
    var item3 = cart.addItem('3d1b57131bc518507c1c0d4acd4bcbba');
    var item4 = cart.addItem('cb0d1bd71bc518507c1c0d4acd4bcbfe');
    var item5 = cart.addItem('ab8bdfd71bc518507c1c0d4acd4bcb38');
    var item6 = cart.addItem('c19d931b1bc518507c1c0d4acd4bcb84'); //sys_id of the catalog item I want to fire


    cart.setVariable(item, 'subject', email.subject); //sets catalog variable to the email's subject

    cart.setVariable(item, 'emailbody', email.body_html); //sets catalog variable to email's body

    var rc = cart.placeOrder(); //this launches the catalog item, and creates a request object.   rc = the request object

    updateRITM(rc.sys_id); //call a function immediately to update the ritm. This must be a nested function, otherwise inbound actions get weird. also, we're passing the sys_id of  //the request so we know what RITM to grab. 

}

function updateRITM(req) {

    var ritm = new GlideRecord('sc_req_item');

    ritm.addQuery('request', req); //req is what we passed from the previous function. the sys_id of the request.

    ritm.query();

    while (ritm.next()) {

        ritm.due_date = email.body.start_date;

        ritm.comments = "forwarded by: " + email.origemail + "\n\n" + email.body_text;

        ritm.short_description = email.subject;

        ritm.watch_list = email.body.legal_name;
  
        ritm.user = email.body.direct_supervisor;

        ritm.update();

    }

}
event.state = "stop_processing"; //stop evaluating inbound actions. This is why I keep this record's order lower than my incident from email rules

1 ACCEPTED SOLUTION

Hopefully my assumption mentioned above that the employee number is always included with the Direct Supervisor between ( and ) characters.  Here is an update script that should hopefully work for you - I tested it with an incident but not a request.  Basically I am using a Regex expression to find all characters outside of the ( and ) characters.  Then do a lookup on the sys_user table matching the employee_number attribute to that value.  If found it will pass that into the cart API to hopefully set the Requested For appropriately.

createRequest();

function createRequest() {
	var eNumberExp = /.*\(|\).*/g; //RegExp that finds all characters outside of ( and )
	var directSupervisor = email.body.direct_supervisor;
	directSupervisor = directSupervisor.replace(eNumberExp, "");
	
	var userID;
	
	// Attempt to find user record with the supervisor's E Number
	var userRec = new GlideRecord("sys_user");
	userRec.addQuery("employee_number", directSupervisor);
	userRec.query();
	if (userRec.next()) {
		userID = userRec.getValue("sys_id");
	}
	
	var cart = new Cart(null, userID); //calling the cart API
	var item = cart.addItem('0e4f075b1b8518507c1c0d4acd4bcb13');
	var item2 = cart.addItem('47aadb171bc518507c1c0d4acd4bcb26');
	var item3 = cart.addItem('3d1b57131bc518507c1c0d4acd4bcbba');
	var item4 = cart.addItem('cb0d1bd71bc518507c1c0d4acd4bcbfe');
	var item5 = cart.addItem('ab8bdfd71bc518507c1c0d4acd4bcb38');
	var item6 = cart.addItem('c19d931b1bc518507c1c0d4acd4bcb84'); //sys_id of the catalog item I want to fire
	
	cart.setVariable(item, 'subject', email.subject); //sets catalog variable to the email's subject
	cart.setVariable(item, 'emailbody', email.body_html); //sets catalog variable to email's body
	var rc = cart.placeOrder(); //this launches the catalog item, and creates a request object.   rc = the request object
	updateRITM(rc.sys_id); //call a function immediately to update the ritm. This must be a nested function, otherwise inbound actions get weird. also, we're passing the sys_id of  //the request so we know what RITM to grab.
	
}

function updateRITM(req) {
	var ritm = new GlideRecord('sc_req_item');
	ritm.addQuery('request', req); //req is what we passed from the previous function. the sys_id of the request.
	ritm.query();
	while (ritm.next()) {
		ritm.due_date = email.body.start_date;
		ritm.comments = "forwarded by: " + email.origemail + "\n\n" + email.body_text;
		ritm.short_description = email.subject;
		ritm.watch_list = email.body.legal_name;
		ritm.user = email.body.direct_supervisor;
		ritm.update();
	}
}
event.state = "stop_processing"; //stop evaluating inbound actions. This is why I keep this record's order lower than my incident from email rules

Please mark this post or any as helpful or the correct answer to your question if applicable so others viewing may benefit.

View solution in original post

14 REPLIES 14

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

FWIW "new Cart()" actually calls a script include named "Cart".  Looking at the initialize() function, you can pass in parameters into that to set the Cart Name and user for the cart, were user ends up being the Requested For person.  So change your script to:

var cart = new Cart(null, userID); //calling the cart API

userID would need to be the SysID of the requested for person so you may need to look that up prior.

The requested for person will be different each time. How do I set the sysID in this situation?

thank you!

You can use below code

 

    // set the requested for

//     var gr = new GlideRecord("sys_user");

//     gr.addQuery("email", email.from);

//     gr.query();

//     if (gr.next()) {

//         var cartGR = cart.getCart();

//         cartGR.requested_for = gr.sys_id;

//         cart.setVariable(item, 'requested_for', gr.name);

//         cartGR.update();

 

Regards,

Sachin

Christina I assume you mean that the different users will be emailing this in and you want the script to run as that user?  If so gs.getUserID() is a function that gets the SysID of the logged in user which will match the user who sent in the email.  This is because ServiceNow will match the from email and lookup that a user via email address on the sys_user table.  So your script would become:

var cart = new Cart(null, gs.getUserID()); //calling the cart API