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

Here you go. This is a code snippet to get the sys_id of the catalog item


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');


}


Thank you Abhinay, unfortunately a RITM was not created. Perhaps there was something wrong with my code?



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('sys_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();


}




}


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();


}




}


Thank you Abhinay! You are a life saver!


Abhinay Erra
Giga Sage

You are welcome. Glad you got your question answered.