Cart API - Variable Set Field not populated

hanaphouse
Giga Guru

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();
1 ACCEPTED SOLUTION

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();

View solution in original post

8 REPLIES 8

Allen Andreas
Administrator
Administrator

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!

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();
}

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!

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();