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

Hi Christina,

Can you log to see what are the values of directSupervisor, userID? Just want to make sure you are able to retrieve the user name and sys_id of the user properly.

If you are sure you are able to retrieve user sys_id, use below code to update requested_for field.

var req = new GlideRecord('sc_request');
req.addQuery('sys_id', <your_request_sys_id>); 
req.query();
if(req.next()){
   req.requested_for = user.sys_id;
   req.update();
}

Christina, OK I created a new inbound email action mimicking yours and everything is working as expected.  Just to validate, here is everything I did.

Inbound email action is similar to above but I didn't setup every variable, just subject and also a reference to populate requested_for to view.

I set Abraham Lincoln's employee number to match what you pasted above:

find_real_file.png

I then sent the following email:

find_real_file.png

The inbound action processed it but didn't insert a target because the inbound action didn't say to.  But when I navigate to the Service Catalog, I see the request:

find_real_file.png

And the requested item populated too:

find_real_file.png

I got it. 

thank you so much for your help Michael!

You are very welcome!

Ashok Katam
Mega Guru

Christina,

You have "ritm.user = email.body.direct_supervisor;" in updateRITM function, but we dont have a field user in RITM table. requested_for on RITM is REQ table field. 

You can query User table to get sys id of user email (email can be retrieved using email.from) 

Then set this sys_id to requested_for field in sc_request table. This will show up in RITM