doing script for creating catlaog request by email inbound action script

sainath reddy
Tera Contributor

I have a requirement for scripting by using email inbound action I have to raise a catalog request through an emai.I'm writing this script but this is not working.Are there any complications in this code.can anyone let me know.Even though it is reflecting in system logs a created.Yet it creating under Incident table, even though I Gave table as sc_request in inbound email action.

 

createRequest();
function createRequest() {
             sys_email.target_table = 'sc_request';
             var short_description = email.subject.toString();
             var body = email.body_text.toString();
             var message = "received from: " + email.origemail + "\n\n" + body;
             var cat_item = 'hgi434nmrXXXXXXXXXXX'; 
             //var name = gs.getUser().getUserByID(email.from_sys_id).getDisplayName();
              
             var cart = new sn_sc.CartJS();
             var item = {
                 'sysparm_id': cat_item,
                 'sysparm_quantity': '1',
                 'variables': {
                     'notes': message
                 }
             };
             cart.addToCart(item);

             var request = {
                 'special_instructions': '',
                 'requested_for': requested_for,
                 'delivery_address': '',
             };
             var requestDetails = cart.submitOrder(request);

             var reqSysID = requestDetails.request_id;

             var rec = new GlideRecord("sc_req_item");
             rec.addQuery("request", reqSysID);
             rec.query();
             if (rec.next()) {
         
                 rec.short_description = short_description;
                 rec.update();
             }
         }
7 REPLIES 7

Sruthi17
Kilo Sage

Hi,

You need to Cart API as you are implementing this in inbound email (server-side script). You can use Cart JS API for any client-side

 

Please refer the code below:

 

var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
 //add your requested item to the cart by sys_id of the catalog item
var item = cart.addItem('sys_id', 1);
//Fill the variables
cart.setVariable(item,"u_requested_for", "req_sys_id");
var rc = cart.placeOrder();

 

Please mark my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Hemanth M1
Giga Sage
Giga Sage

Hi @sainath reddy ,

 

Could see cat_item value as some string it should be sys_id of the catalog item and don't see requested_for value defined.

try executing this logic in the background script as below (in dev) using static values and check if any issues.

HemanthM1_0-1692004758875.png

Output:

HemanthM1_1-1692004797145.png

 

 

 

we have another way of submitting catalog item using cartAPI

Option 2:

var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
var item = cart.addItem('sys_id of the catalog item');
cart.setVariable(item,'variable backendname',value);// example cart.setVariable(item,'requested_for','sys_id of the user');
cart.setVariable(item,'variable backend name',value); // example cart.setVariable(item,'notes','1234');
var rc = cart.placeOrder(); //rc is the sys_id of the request

 

 

 

 

Accept and hit Helpful if it helps.

Thank you,
Hemanth
Certified Technical Architect (CTA), ServiceNow MVP 2024, 2025

it is creating under incident table.even if the changes had made

Hi @sainath reddy ,

 

What do you mean its creating under incident table?? is it creating a incident record?

did you check in the request table for any new requests

 

we would like to know more about inbound action condition (may be another inbound is triggering )

check inbound logs , it gives which inbound actions are executed/skipped

 

 

Accept and hit Helpful if it helps.

Thank you,
Hemanth
Certified Technical Architect (CTA), ServiceNow MVP 2024, 2025