Below script in nor working correctly

keval3
Tera Contributor

Hi All,

I have written below inbound email action script on sc_req_item table

by using that 2 request are created 1 with proper catalog item and another one in without catalog item please let me know what is issue in my script.

keval3_0-1730913502603.png

var cart = new Cart();
    // // add in cart, substitute your catalog item sys_id
    var item = cart.addItem('49dd78a3db427c50eeb8026dd3961902');
     cart.setVariable(item, 'description', email.body);
     cart.setVariable(item, 'contact_type', 'email');
     cart.setVariable(item, 'requested_for', '3336eb92dba9dc10a73ddf0bd39619bd');
    cart.setVariable(item, 'subject', email.subject);
    var rc = cart.placeOrder();

  Thanks & Regards

KP

 

7 REPLIES 7

Brad Bowman
Kilo Patron
Kilo Patron

Try instantiating the cart with a unique ID

var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);

 

Hi Brad,

After using that only one ticket has been created but without item. kindly find below screen snap.

keval3_0-1730958833323.png

Regards,

KP

Runjay Patel
Giga Sage

Hi @keval3 ,

 

Use below code.

var id = GlideGuid.generate(null); // Generate a unique GUID for the cart
var cart = new Cart(id); // Instantiate a new cart with the GUID
var item = cart.addItem('Put sys_id of your catalog ite.'); 
cart.setVariable(item,'put your variable name ','your variable value');
cart.setVariable(item,'put your variable name2 ','your variable value2');
var request = cart.placeOrder(); 
//Example...
// Add the catalog item using its sys_id
var RITM = cart.addItem('060f3afa3731300054b6a3549dbe5d3e'); // sys_id of the catlog item
// Set variables for the catalog item
cart.setVariable(RITM, 'ram', '16');       // RAM size
cart.setVariable(RITM, 'os', 'win10');     // Operating system
cart.setVariable(RITM, 'storage', '1tb');  // Storage capacity
// Place the order
var request = cart.placeOrder(); 

 

You can visit https://servicenowwithrunjay.com/create-ritm-using-cart-api-in-servicenow/ for more detailed info.

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

 

 

Hi Runjay,

I apply your code in my inbound as you can see in below screen snap 3 tickets has been create at same time.

keval3_0-1730961500638.png

(function runAction( /*GlideRecord*/ current, /*GlideRecord*/ event, /*EmailWrapper*/ email, /*ScopedEmailLogger*/ logger, /*EmailClassifier*/ classifier) {

    // Implement email action here

    var id = GlideGuid.generate(null); // Generate a unique GUID for the cart
    var cart = new Cart(id); // Instantiate a new cart with the GUID
    var item = cart.addItem('49dd78a3db427c50eeb8026dd3961902');
    cart.setVariable(item, 'contact_type', 'email');
    cart.setVariable(item, 'sub_heads', 'nurture_business');
    var request = cart.placeOrder();
    //Example...
    // Add the catalog item using its sys_id
    var RITM = cart.addItem('49dd78a3db427c50eeb8026dd3961902'); // sys_id of the catlog item
    // Set variables for the catalog item
    cart.setVariable(RITM, 'contact_type', 'email'); // RAM size
    cart.setVariable(RITM, 'sub_heads', 'nurture_business'); // Operating system
    //cart.setVariable(RITM, 'storage', '1tb');  // Storage capacity
    // Place the order
    var request = cart.placeOrder();

})(current, event, email, logger, classifier);