How to pass dynamic values to variables for cart item in cartjs in scripted REST API?

Annamdevula Di1
Kilo Contributor

Hi All,

How to pass dynamic values for variables in scripted REST API for creating cart item by using cartjs API. Can you please suggest  this.

find_real_file.png

12 REPLIES 12

Can you please share what you received in response Body.

Try printing logs when you parse response and share the JSON.

 

Hi Anil,

 

Thanks for reply, this is the response I got.

 

find_real_file.png

It is empty, so obviously you will not get anything in variables.

Can you share the json format you are sending as request to that scripted api.

I have tried with this and its workig, thank you for helping.

 

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var reqbody = request.body.data;
try{
//var parser = new global.JSON();
//var parsedData = parser.decode(reqbody);
//var memoryvar = parsedData.memory;
//var colourvar = parsedData.colour;
var cart = new sn_sc.CartJS("Testlaptop");
var item =
{
'sysparm_id': '75e7a257dbd1011052939026ca9619bc',
'sysparm_quantity': '1',
'variables':{
'memory' : reqbody.Memory,
'colour': reqbody.Colour,
}};
var cartDetails = cart.addToCart(item);
//gs.log(JSON.stringify(cartDetails));
//var rc = cart.placeOrder();
}
catch(ex){
var res = {};
res["status"] = "Success";
res["message"] = ex.message;
response.setBody(JSON.stringify(res));
}