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

I guess the most obvious issue to me is the order of operations for your conditional adJSON3.

I am assuming your IF condition is being met.

Modify this area of your script and see how that goes.

 

items.push(adJSON1);  //This happens all the time
items.push(adJSON2);  //This happens all the time

if (details.location.toString() == 'GLOBAL') {  //start condition check
var adJSON3 = {};

    adJSON3 = {
        'sysparm_id': 'b749ed9fa', // catalog sys_id
        'variables': {
            'name': details.sys_id.toString()
        }
    };
items.push(adJSON3);
} //end condition check

 

@Shane J ,

 

I tried that approach, it's it's not generating the 3rd RITM. Added logs after if and json, it's executing.

I don't know where 'details' is pulling from so you must only be offering up a piece of your code.  That being said, here is an example Fix Script that I'm able to get a gs.print to work for, so I would think I would work if you updated it as needed.

 

 

ABCD();

function ABCD() {

    var items = [];

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

    var adJSON2 = {
        'sysparm_id': '9bde6h3', // catalog sys_id
        'variables': {
            'name': 'String2'
        }
    };

    items.push(adJSON2);

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

gs.print(JSON.stringify(items));

    /*  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;*/
}

 

 

@Shane J 

Thanks for your support. The variables I was using was causing an issue. Not sure what's the issue with that variable, still have removed that variable and it's working fine.