Inbound Email Action to set Requested for on an item

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-05-2019 05:51 AM
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
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-05-2019 06:44 AM
you need to change below
cart.setVariable(item, 'request.requested_for', user.getDisplayValue());
to
cart.setVariable(item, 'request.requested_for', user.sys_id);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-05-2019 06:58 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-05-2019 07:30 AM
if that's the field on sys_user than it's fine.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-05-2019 07:25 AM
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