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.

 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

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.

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

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


Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP

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