- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2023 01:14 PM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2023 08:53 AM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-30-2023 08:33 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-30-2023 08:45 AM
@Shane J ,
I tried that approach, it's it's not generating the 3rd RITM. Added logs after if and json, it's executing.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-30-2023 11:28 AM
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;*/
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2023 06:45 AM
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.