The CreatorCon Call for Content is officially open! Get started here.

Inbound Email Action to set Requested for on an item

matt_a
Kilo Guru

Hi, I created an inbound action that uses the cart to parse details into variables on a form and create an item for a team to deal with.

To help the inbound action, I created a user with associated email address so that whenever an email comes in from it, the inbound action is triggered.

I now have a new requirement to include on this and I am struggling so asking for help.

In the text on the email is a users samaccount name. This active directory name is on every user record.

I need to update my action so that the requested_for field on the RITM is changed from the generic user to the user contained within the email.

I tried with the following code. however, the result is that it still just uses the generic user. Is my code right? IF so, could it be that I am using a user for triggering the inbound action and the user is taking priority? Or is my code wrong?

var cartmsg = email.body_text;
var cart = new Cart();
var item = cart.addItem('b2ac7a2bdb5bfb0055ad10284b9619f0'); //we have to hardcode this sys_id

//Look up the user table for someone with the same name.
var user = new GlideRecord("sys_user");
if(user.get("u_samaccountname", email.body.created_by))
	{
	//Found the users with a matching name,
	cart.setVariable(item, 'request.requested_for', user.getDisplayValue());
}
else
	{
	//No matching user
	cart.setVariable(item, 'request.requested_for', '6a1b3ae7db5bfb0055ad10284b96192c');
}

cart.setVariable(item, 'order_details', email.body_html);
cart.setVariable(item, 'job_number', (email.body.job));
cart.setVariable(item, 'contract_number', (email.body.contract));
cart.setVariable(item, 'order_comments', (email.body.comments));
cart.setVariable(item, 'value', (email.body.total_value));
cart.setVariable(item, 'comments', cartmsg);
var rc = cart.placeOrder();

//add the email body into the comments
var grRequest = new GlideRecord('sc_req_item');
grRequest.addQuery('request', rc.sys_id);
grRequest.query();

if (grRequest.next()) {
	gs.log("Found it!!", "Matt");
	grRequest.comments = cartmsg;
	grRequest.update();
}

Thanks

6 REPLIES 6

Mike Patel
Tera Sage

you can also update requested for after it's created

add below after grRequest.comments = cartmsg;

grRequest.requested_for = user.sys_id;

 

 

kalindaskalov
Giga Contributor

I dont know if you found an answer to this but check this out:
https://community.servicenow.com/community?id=community_question&sys_id=7511e2d1dbd7d41066f1d9d9689619a5

 

Basically you need to add this code after var rc = cart.placeOrder();

var reqRecord = new GlideRecord('sc_request');

reqRecord.get(rc.sys_id);

reqRecord.requested_for = 'user sys_id you want here';

reqRecord.update();