- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 02-12-2022 04:20 PM
How to set a MRVS (Multi Row Variable Set) for CartJS API
Intro: A Multi RowVariable Set (MRVS) can be set in a server or background script with proper syntax formatting. The MRVS is a JSON object that has been passed through JSON.stringify() and is surrounded with square brackets "[" and "]".
The basic steps to get the data formatted correctly is to:
- Create a JSON object with properties and values of a MRVS
- JSON.stringify() the JSON object
- Add square brackets [ ] to the start and end of the string.
Shown below are two examples of how to create the data along with a function that will convert a JSON object to a string in the proper MRVS syntax.
Example 1 :
Create a JSON object
var mrvsData = {
variableString1 : "string",
booleanValue1 : true
};
Perform a JOSN.stringify(mrvsData) on the data to get:
'{"variableString1":"string","booleanValue1":true}'
Now surround the string with [ ]
'[{"variableString1":"string","booleanValue1":true}]'
This data can now be used to set a variable in the CartJS API
Example 2:
Here is a full code example of a catalog item that has a MRVS utilizing the CartJS API
var cart = new sn_sc.CartJS();
var item = {
'sysparm_id': 'a9cad13a2f110110c9ebdcb6f699b6fa',
'sysparm_quantity': '1',
'variables': {
'string_on_base_vars': 'Sample String Value',
'mrvs_demo': '[{"mrvs_date_var":"2022-02-04","mrvs_checkbox_var":"true"}]'
}
};
var cartDetails = cart.addToCart(item);
var checkoutInfo = cart.checkoutCart();
gs.info(checkoutInfo);
Function to convert JSON values to MRVS data:
/*
Converts a JSON value into MRVS (Multi-Row Variable Set) to be used by the Cart JS Api.
@param{string|object} - JSON object or string value to be converted to MRVS (Multi Row Variable Set) string
@return{string} String value to be used for a MRVS (Multi Row Variable Set)
*/
function convertJsonToMrvs(jsonValue){
//Handle multiple input types
if(typeof jsonValue === "string"){
try{
gs.debug("String received for f(x) convertJsonToMrvs")
var objJson = JSON.parse(jsonValue);
jsonValue = objJson;
}catch(error){
gs.error("Unable to convert: " + jsonValue + " into a JSON object. Source, F(x) convertJsonToMrvs");
}
}else if (typeof jsonValue === "object"){
gs.debug("JSON object recevied for f(x) convertJsonToMrvs");
}else{
gs.error("Improper object passed into F(x) convertJsonToMrvs");
}
//Perform conversion
var strMrvsData = JSON.stringify(jsonValue);
strMrvsData = "[" + strMrvsData + "]";
gs.debug("Return Value for f(x) convertJsonToMrvs: " + strMrvsData);
return strMrvsData;
}
- 5,288 Views
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Great information, I have a small question. If in a scenario where there is a onChange client script written on one of the field which is Auto populating other fields in the multirow variable set.
So when we provide values to be populated in Multirow variable set through CartJs would the client script get triggered or do we have to explicitly provide value for other fields.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi Constantine,
Thanks for posting this. I have a use case where I've needed to do this for the first time.
You can create the mrvsData variable as an array and then stringify the data in one step, without having to wrap the resultant string in '[' and ']' afterwards.
For instance:var mrvsData = [{ variableString1 : "string", booleanValue1 : true }]
;
var strMrvsData = JSON.stringify(mrvsData);
That makes it easier to include multiple rows:
var mrvsData = [{ variableString1 : "string", booleanValue1 : true },{ variableString1 : "string", booleanValue1 : true },{ variableString1 : "string", booleanValue1 : true }]
;
var strMrvsData = JSON.stringify(jsonValue);
A peculiar thing I've noticed, in my customer DEV instance and in my PDI, the multi row variable set data does not get included in the output when I do this part:
var cartDetails = cart.addToCart(item);
var checkoutInfo = cart.checkoutCart();
gs.info(checkoutInfo);
even though they do correctly appear in the RITM that gets generated.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
The sn_sc.CartJS() will run only on the server side. Client side scripts will not run. Any logic being done client side would need to be duplicated into the server side code.
Hope that helps,
Constantine
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi
Will this work for updating quantity in cart based on MRVS rows? I'm using Employee Centre and using client script seems to work in native UI but not in Employee Centre
Thanks