The CreatorCon Call for Content is officially open! Get started here.

How can I utilize a condition in sn_sc.CartJS variables object?

Shane J
Tera Guru

I need to use the sn_sc.CartJS to generate a request via script.  One of the values to pass in the variables object only needs to be used conditionally.  I haven't been able to find a straight-up Javascript solution.  I attempted similar solutions to this:

 

const a = {
   ...(someCondition && {b: 5})
}

 

but the ... isn't allowed.

 

1 ACCEPTED SOLUTION

scwillson
Mega Guru

You can apply your logic after creating the cart object, but before you input the object into the checkout cartapi call.

cartJS = new sn_sc.CartJS();
var cart = {
   'sysparm_quantity': '1',
   'variables': {
      'var_1' : 'my value',
      'var_2' : 'other value',
      'var_test': 'etc...'
   }
}

//Determine if additoinal variables need to be added
if (current.date != '') {
   //IF so, then add them like this:
   cart['variables']['var_3'] = current.date.toString();
}

cartJS.submitOrder(cart);

View solution in original post

8 REPLIES 8

Craig Gruwell
Mega Sage

Hi Shane,

 

You might have to perform your conditional logic prior to creating the JSON object.  Would something like this work?

 

var item_id = '<item_sys_id>';
var item_quantity = 1;
var var_value1 = "";
var var_value2 = "";

if (someCondition)
  var_value1 = "XXX";
else
  var_value1 = "YYY";

if (someCondition && var_value1 = "YYY") 
  var_value2 = "123";
else
  var_value2 = "456";

 

JSON object:

 

{
'sysparm_id': item_id,
'sysparm_quantity': item_quantity,
'variables':{
     'var_name1': var_value1,
     'var_name2': var_value2,
     'var_name3': 'var_value3'
    }
}

I'm already doing that for some other variables, but I think the underlying issue is that that variable being passed is null unless the condition is met.

 

Causes this error:

java.lang.NullPointerException: org.mozilla.javascript.JavaScriptException: java.lang.NullPointerException:

along with a bunch of other nonsense

 

Here's the parts of the script that apply directly to this issue:

var edate;
if (current.u_employee_type == 'Temporary' || current.u_employee_type == 'Contingent' || current.u_employee_type == 'Contractor') {
edate = new GlideDateTime(current.u_change_effective_date_date);
edate.addDays(365);
}

var cartidINITNEW = "cart_" + gs.generateGUID(); // along with gs.generateGUID() we should append "cart_". This can be used as unique id for the Cart.
        var cartINITNEW = new sn_sc.CartJS(cartidINITNEW);
        var itemINITNEW = { // Note that values using CartJS are instantiated via a JSON object
            'sysparm_id': 'ITEM_SYS_ID', //Onboarding Item
            'sysparm_quantity': '1',
            'variables': {
	'assignment_end_date': edate //should be 1 year later if not Employee
            },

ETC...

 

scwillson
Mega Guru

You can apply your logic after creating the cart object, but before you input the object into the checkout cartapi call.

cartJS = new sn_sc.CartJS();
var cart = {
   'sysparm_quantity': '1',
   'variables': {
      'var_1' : 'my value',
      'var_2' : 'other value',
      'var_test': 'etc...'
   }
}

//Determine if additoinal variables need to be added
if (current.date != '') {
   //IF so, then add them like this:
   cart['variables']['var_3'] = current.date.toString();
}

cartJS.submitOrder(cart);

2022_ServiceNow
Tera Expert

Hi @Shane J ,

 

I have a similar requirement. 

This creates the 2 RITMs with the first 2 catalog items under the same REQ. But it's not generating the RITM for the 3rd item with the 'if' condition.

Can you please help me with this? Thanks in advance!

 

var items = [];

var adJSON1 = {
    'sysparm_id': '8dfdbd11e', // catalog sys_id
    'variables': {
        'name': details.sys_id.toString()
    }
};

var adJSON2 = {
    'sysparm_id': '9bde6h3', // catalog sys_id
    'variables': {
        'name': details.sys_id.toString()
    }
};

var adJSON3 = {};

if (details.location.toString() == 'GLOBAL') {
    adJSON3 = {
        'sysparm_id': 'b749ed9fa', // catalog sys_id
        'variables': {
            'name': details.sys_id.toString()
        }
    };
}

items.push(adJSON1);
items.push(adJSON2);
items.push(adJSON3);

var cartname = "cart" + gs.generateGUID();
for (var i = 0; i < items.length; i++) {
    var cart = new sn_sc.CartJS(cartname);
    var item = items[i];
    cart.addToCart(item);
}

var requestResponse = cart.checkoutCart(true);
var request_id = requestResponse.request_id;