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 Michael,

No the email will always come from Workday.  The requested for will be the direct_supervisor which is located in the email; this is the user that needs to be requested for on REQ. below is an example of email coming from Workday, where I would need the requested for to be Rachel Green:

Human Resources is setting up an employee. This is a new employee. (NH)

Legal Name: Jennifer Lopez
Nickname: JLo 
E Number: e2222
Work Status: Regular
Position: Provider Operations Specialist I
Department: Provider Network Operations
Office Location: OR: Eugene
Direct Supervisor: Rachel Green (e9999)
Start Date: 06/01/2020

 

 

OK so the format is always First Last (employee id number)?  I ask because code would expect it a certain way.  Also is the employee number attribute on the User table populated with e9999 for Rachel Green in this example?

 

Have you considered using a web services integration from WD to ServiceNow instead?

It is always first last. And yes the direct supervisor is set up as a user. 

 

We didn't get the WD integration. I wish we did, would make this a lot easier. 🙂 

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.

Hi Michael, 

I do appreciate your help, but this didn't work on my requests. Any other suggestions? 

thank you!