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 need to change below 

cart.setVariable(item, 'request.requested_for', user.getDisplayValue());

to

cart.setVariable(item, 'request.requested_for', user.sys_id);

Hi Mike, I have updated as suggested but its still being set as the user that sent the email.

find_real_file.png

The underlined field - thats the field on the sys_user table. Is this correct?

Many thanks 

if that's the field on sys_user than it's fine.

I have created a reference variable on the form and told the script to populate that instead:

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, 'name', user.sys_id);
}
else
	{
	//No matching user
	cart.setVariable(item, 'name', '6a1b3ae7db5bfb0055ad10284b96192c');
	//	gs.log("Found it!!", "Matt");
}

It does work and the variable correctly set with the user.

Just need to work out why its not updating the requested for. Maybe I could just use this new field and then get the workflow to set it once created