- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-30-2021 09:17 AM
I am trying to fill out a variable within a variable through Cart API, however, this doesn't seem to work.
- Variable Set: vset_fields
- Variable Set Field # (a multi-line field): vset_fields_test
What could be the issue there?
var cart = new Cart();
var item = '6d0f8e1c81df2500772e95d75e286264';
cart.addItem(item);
cart.setVariable(item,'vset_fields_test','test');
var scREQ = cart.placeOrder();
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-31-2021 09:06 AM
I think I figured out what's wrong with the script. I should store the .addItem() in a variable then use that variable to set the catalog item variables.
var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
var item = '6d0f8e1c81df2500772e95d75e286264';
var ritm = cart.addItem(item);
cart.setVariable(ritm,'vset_fields_test','test');
var scREQ = cart.placeOrder();

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-30-2021 09:59 AM
Hi,
Can you try just using the standard approach, such as:
var cart = new Cart();
var item = '6d0f8e1c81df2500772e95d75e286264';
cart.addItem(item);
cart.setVariable(item,'variable_name','test');
var scREQ = cart.placeOrder();
So without "vset" etc, and just use the variable name.
Please mark reply as Helpful/Correct, if applicable. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-30-2021 11:00 AM
This doesn't work either. I should be able to set variables of the item right before placeOrder()?
My second option was this. It is now working but I am not sure if this is the right approach. I should be able to set the variables on submit right through the cart API? Instead of creating the REQ and RITM first then updating the RITM.
var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
var item = '6d0f8e1c81df2500772e95d75e286264';
cart.addItem(item);
var scREQ = cart.placeOrder();
gs.print('req: ' + scREQ.sys_id);
var ritm = new GlideRecord('sc_req_item');
ritm.addQuery('request',scREQ.sys_id.toString());
ritm.query();
gs.print('ritm: ' + ritm.number);
while(ritm.next()){
ritm.variables.vset_fields_test = 'test';
ritm.short_description = 'short_description';
ritm.update();
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-30-2021 12:18 PM
Hi,
Yes, you should be able to setVariable on during ordering process before you actually "cart.placeOrder()". See documentation here: https://docs.servicenow.com/bundle/quebec-application-development/page/script/server-scripting/refer...
I'm unsure if this variable is a string field? a select-box? etc? So you'd just want to ensure you're using appropriate values, and all that that, but yes, in theory is should work. I wouldn't think that variable set causes an issue here, but you could try with a variable that was created right on the catalog item itself and see if that works (just for testing purposes).
Please mark reply as Helpful/Correct, if applicable. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-31-2021 09:06 AM
I think I figured out what's wrong with the script. I should store the .addItem() in a variable then use that variable to set the catalog item variables.
var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
var item = '6d0f8e1c81df2500772e95d75e286264';
var ritm = cart.addItem(item);
cart.setVariable(ritm,'vset_fields_test','test');
var scREQ = cart.placeOrder();