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 Erra
Giga Sage

Where is the catalog item present in your email? Is it the subject line or is it present in the email body?


Hi Abhinay,


The catalog item request is in the body


Abhinay Erra
Giga Sage

Can you show me the sample email how the email body looks?


Sure thing, this is what is sent to SN and a RITM is created but with no REQ



Item: ServiceNow Permissions


Assignment Group: SN group


Requested For: Abhinay E


Configuration Item: [TSVC] ServiceNow


State:2


Priority: 4


Planned Effort: 0:10


Location:   2621


Company: Sn Community


Requested For Organization: SNC


Due Date: 10-14-2016 14:00