In an email inbound action using CartJS() to order from a specific user's cart.

Ilo Gassoway
Mega Guru

I am building an inbound action to take an email from one of our automation tools and order a catalog item. in the email is the manager who I want to order the item from. However even if i add the setRequestedFor it still shows the email address ordered the item.  Suggestions?

 

//get Employee Object
var employeeCorpID = email.body.corpid;
var employee = new GlideRecord('sys_user');
employee.get('user_name', employeeCorpID);

//get Manager Object
var managerCorpID = email.body.manager_corpid;
var manager = new GlideRecord('sys_user');
manager.get('user_name', managerCorpID);


//order Item
var cart = new sn_sc.CartJS();
var item = {
    'sysparm_id': 'fd79407b1b95da10d40e2f05604bcb87',
    'sysparm_quantity': '1',
    'variables': {
        'submitted_by': manager.sys_id.toString(),
        'requested_for': employee.sys_id.toString(),
        'title': employee.title.toString(),
        'email': employee.email.toString(),
        'location': employee.location.toString(),
        'department': employee.department.getDisplayValue().toString(),
        'phone': employee.phone.toString(),
        'actions_needed': email.body_text
    }
};
var cartDetails = cart.addToCart(item);
cart.setRequestedFor(manager.sys_id.toString());
var checkoutInfo = cart.checkoutCart();
var req_id = checkoutInfo.request_id;
8 REPLIES 8

DrewW
Mega Sage
Mega Sage

When you do things like

employee.get('user_name', employeeCorpID);

You really should do something like

if(employee.get('user_name', employeeCorpID)) {
   //Do your work here
}

 Just in case a record is not returned.  So the first part you should consider something like

var employee = new GlideRecord('sys_user');
var manager = new GlideRecord('sys_user');
if(email.body.corpid 
      && email.body.manager_corpid
      && employee.get('user_name', email.body.corpid)
      && manager.get('user_name', email.body.manager_corpid)
   ) {
   //Put your code here.
} else {
   //Do something to let someone know that this came in but not all of the info
   //was present to order the item.
}

 

You may also want to consider using getValue() instead of .toString().  The reason is that if manager.sys_id is null then manager.sys_id.toString() may error, but manager.getValue("sys_id") will return a blank string if it is null.

 

Its the last thing I think is your issue.  Because if manager is not a valid record then the system will set the requested for to the user that ran the code.  Since this is an inbound action its using the email address.

 

Last item is have you put in some logging statements to make sure things are what you think they are?

 

I have been doing logging, and it does get the values I expect. It's still setting the cart, there for 'created_by' for the request to be the automation account, not the manager.

Also have you considered just updating the value after you submit the order?