How to pass dynamic values to variables for cart item in cartjs in scripted REST API?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-03-2022 05:31 AM
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.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-04-2022 03:19 AM
Can you please share what you received in response Body.
Try printing logs when you parse response and share the JSON.
Thanks
Anil Lande
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-04-2022 03:21 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-04-2022 03:25 AM
It is empty, so obviously you will not get anything in variables.
Thanks
Anil Lande
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-04-2022 04:19 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-04-2022 04:21 AM
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));
}