Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to insert the value into MRVS while creating catalog using script

ramesh_r
Mega Sage

Hi All,

 

I am creating catalog using below script and i can able to set the value to the normal variables but i am not able to set value to the MRVS.

 

Please help me how to set value in the MRVS

 

var cartId = GlideGuid.generate(null);
var adhoc = new Cart(cartId);
var item = adhoc.addItem('a192a8ba1304ba00d9ff30ded144b02d', 1);
adhoc.setVariable(item, 'servicenow_group_descriptions', 'Test');

var rc = adhoc.placeOrder();

 

 

1 ACCEPTED SOLUTION

Hello Ramesh,

 

Try below script with cartJs() APi, I tested, and it is working:

 

var sn_groups = "d476a4e5073a91104154fa9e7c1ed072";
var servicenow_group_descriptions = "Test";
var business_justification = "Test";
var requested_by_date = "Test";

var arr = [];
var Obj = {};
Obj.sn_groups = sn_groups;
Obj.servicenow_group_descriptions = servicenow_group_descriptions;
Obj.business_justification = business_justification;
Obj.requested_by_date = requested_by_date;

arr.push(Obj);

var adhoc = new sn_sc.CartJS();
var requestDetails = {
	"sysparm_id": "a192a8ba1304ba00d9ff30ded144b02d",
	"sysparm_quantity": "1",
	"variables": {
		"requested_for": sn_groups,
		"servicenow_group_membership_request": JSON.stringify(arr)
	}
};


var cartDetails = adhoc.orderNow(requestDetails);
gs.log(cartDetails);
If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali

View solution in original post

3 REPLIES 3

Ahmmed Ali
Giga Sage
Giga Sage

Hello,

 

You need to set MVRS variable set value with JSON data.

 

For example, if MVRS name is test_mvrs and you have two variables var1 and var2 in the MVRS. Then you can set value in above script as 

var tempObj = [];

var obj1 = {}; //This represent one row in MVRS and below are columns in MVRS

obj1.var1 = "value 1";

obj1.var2 = "Value 2";

tempObj.push(obj1);

//You can add multiple rows as above pushing to the array

adhoc.setVariable(item, 'test_mvrs', JSON.stringify(tempObj)); //Set array JSON string as value to MVRS

 

Thank you,

Ali

 

If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali

Hi Ali,

 

I am running below script its creating RITM but not setting the MRVS values

 

 

var sn_groups = "d476a4e5073a91104154fa9e7c1ed072";
var servicenow_group_descriptions = "Test";
var business_justification = "Test";
var requested_by_date = "Test";

var arr = [];
var Obj = {};
Obj.sn_groups = sn_groups;
Obj.servicenow_group_descriptions = servicenow_group_descriptions;
Obj.business_justification = business_justification;
Obj.requested_by_date = requested_by_date;

arr.push(Obj);

var cartId = GlideGuid.generate(null);
var adhoc = new Cart(cartId);
var item = adhoc.addItem('a192a8ba1304ba00d9ff30ded144b02d', 1);
adhoc.setVariable(item, "requested_for", sn_groups);
adhoc.setVariable(item, "servicenow_group_membership_request", JSON.stringify(arr));

var rc = adhoc.placeOrder();

 

 

Hello Ramesh,

 

Try below script with cartJs() APi, I tested, and it is working:

 

var sn_groups = "d476a4e5073a91104154fa9e7c1ed072";
var servicenow_group_descriptions = "Test";
var business_justification = "Test";
var requested_by_date = "Test";

var arr = [];
var Obj = {};
Obj.sn_groups = sn_groups;
Obj.servicenow_group_descriptions = servicenow_group_descriptions;
Obj.business_justification = business_justification;
Obj.requested_by_date = requested_by_date;

arr.push(Obj);

var adhoc = new sn_sc.CartJS();
var requestDetails = {
	"sysparm_id": "a192a8ba1304ba00d9ff30ded144b02d",
	"sysparm_quantity": "1",
	"variables": {
		"requested_for": sn_groups,
		"servicenow_group_membership_request": JSON.stringify(arr)
	}
};


var cartDetails = adhoc.orderNow(requestDetails);
gs.log(cartDetails);
If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali