Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Grabbing sys_id in cart api script

javis
Giga Expert

Hi community,

To give you some background of what I am trying to do, we send emails (via inbound actions) to SN to create RITMs based on what the email says. For example, "Item: ServiceNow Permissions" is a catalog item we have that a user can request in an email. SN sees "Item: ServiceNow Permissions" and should get its sys_id to create a REQ and RITM in the cart api.

My question and what I need scripting help with is: How can I grab a sys_id of any catalog item using the cart api? I am thinking of writing an array to query the catalog table based on what the "Item:" is in the email but I don't know how the cart api and array will work together in the code.

The api code I want to use is:

createRequest();

function createRequest() {

var cart = new Cart();

// add in cart, substitute your cat item sys_id

var item = cart.addItem('00000000000000000000000000000000');   ---> Don't want only one sys_id (I.e should be able to query and grab sys_id of wanted item based on email)

// set requested for, substitute your requested for

//Set Variables in your Cart Item

cart.setVariable(item, 'requested_for', '00000000000000000000000000000000');

cart.setVariable(item, 'request_short_description', email.subject.toString());

cart.setVariable(item, 'request_description', email.body_html);

cart.setVariable(item, 'request_type', 'others');

var cartmsg = "received from: " + email.origemail + "\n\n" + email.body_text;

cart.setVariable(item,'comments',cartmsg);

var rc = cart.placeOrder();

}

Thanks in advance

1 ACCEPTED SOLUTION

Abhinay Erra
Giga Sage

Change this line "cart.addItem('sys_id');" in your code to "cart.addItem(cat_id);"



createRequest();


function createRequest() {



var cat_id='';


if(email.body.item != undefined && email.body.item!='') {


var item=email.body.item.trim();


var gr= new GlideRecord('sc_cat_item');


gr.get('name',item);


cat_id=gr.getValue('sys_id');



var cart = new Cart();


cart.addItem(cat_id);


// set requested for, substitute your requested for


//Set Variables in your Cart Item


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


cart.setVariable(item, 'request_short_description', email.subject.toString());


cart.setVariable(item, 'request_description', email.body_html);


cart.setVariable(item, 'request_type', 'others');


var cartmsg = "received from: " + email.origemail + "\n\n" + email.body_text;


cart.setVariable(item,'comments',cartmsg);


var rc = cart.placeOrder();


}




}


View solution in original post

10 REPLIES 10

Abhinay,



I'm trying to set variables with my code and now it is not generating RITMs. Could you look at where I could be going wrong?



var cart = new Cart();




var item=createRequest(cart);


function createRequest(cart) {


  var ritm;


  var cat_id='';


  if(email.body.item != undefined && email.body.item!='') {


  var item=email.body.item.trim();


  var gr= new GlideRecord('sc_cat_item');


  gr.set('name',item);


  cat_id=gr.setValue('sys_id');




  ritm=cart.addItem(cat_id);


  var cartmsg = "received from: " + email.origemail + "\n\n" + email.body_text;


  //cart.setVariable(item,'comments',cartmsg);


  var rc = cart.placeOrder();


  }


  return ritm;


}




populateVariables(cart,item);


function populateVariable(cart,item) {


  var user = new GlideRecord('sys_user');


  user.set(cart.setVariable('requested_for'));




  // Set the department.


  if(cart.setVariable('requested_for_department') != user.department) {


  cart.setVariable(item, 'requested_for_department', user.department);


  }



  // Set the username.


  if(cart.sys_id != undefined && cart.setVariable('requested_for_username') != user.user_name) {


  cart.setVariable(item, 'requested_for_username', user.user_name);


  }



  // Set the phone number.


  if(cart.setVariable('requested_for_phone') != user.phone && user.phone != '' && user.phone != undefined) {


  cart.setVariable(item, 'requested_for_phone', user.phone);


  }



  // Set the budset code.



  if(user.sys_id != undefined && cart.setVariable('requested_for_budsetcode') != user.cost_center) {


  cart.setVariable('requested_for_budsetcode', user.cost_center);



  }



  // Set the company.


  if(cart.setVariable('requested_for_company') != user.company) {


  cart.setVariable('requested_for_company', user.company);


  }




  // If the user does not have a company...


  if(user.company == '') {


  // Use core company.


  var core_company = new GlideRecord('core_company');


  core_company.addQuery('primary', true);


  core_company.query();



  if(core_company.next()) {


  if(cart.setVariable('requested_for_company') != core_company.sys_id) {


  cart.setVaraible('requested_for_company', core_company.sys_id);



  }



  }


  }


}