We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

get value MRVS scripted activity

Nikora
Giga Expert

hello teams,

 

i need some help to retrieve values of my MRVS from scripted activity,

the goal is to prepare a Json files to upload into the RITM,

export files to RITM is OK, export simple variable is ok, but i can't retrieve my mrvs info by IO or name,

mys script below:

 

(function executeRule(current, previous /*null when async*/) {
 
 
var ritmSysId = current.sys_id;
var RITMNumber = current.number;
 
    // Concerned system
  var  system = RITM.variables.v_dc_system;
 
var mrvs = ('IO:7046e0dcdb87ed108d889f5cd39619f8'); //IO du multirow
//var mrvs = JSON.parse(current.variables.multi_test);//array of objects with values
 
 
var rowCount = mrvs.getRowCount();
 
var obj = JSON.parse(mrvs); //The MRVS values are captured as a JSON string, which we parse, creating an array
 
//var volume = RITM.variables.Volume;
//var size = RITM.variables.Size_GB;
var arr=[];
 
for (var i = 0; i < rowCount; i++) { 
var row = mrvs.getRow(i);
arr.push(row.Volume, row.Size_GB);
 
}
    var partitions = JSON.stringify(arr);
 
 
 
 
 
 
 
var sa = new GlideSysAttachment();
 
var document = RITMNumber + " test variables " + system + "\n" + "partitions" + "ARR" + arr + "rowcount" + rowCount;
 
var ritmRec = new GlideRecord('sc_req_item');
ritmRec.get(ritmSysId);
 
sa.write(ritmRec, RITMNumber + "_json.json", "text/csv", document);
 
})(current, previous);
1 ACCEPTED SOLUTION

Thanks a lot for your help, you are a true Guru;)

 

finaly find the way: the good one to get the result espected is

mrvs.orderBy('created');
mrvs.orderBy('row_index');

 

result:

partitions["Volume : RootVG","Size_GB : 32","Volume : SoftVG","Size_GB : 40","Volume : DataVG","Size_GB : 100"]

 

//if i put this : 

mrvs.orderBy('row_index');

mrvs.orderBy('item_option_new');
the result is : 

partitions["Size_GB : 32","Volume : RootVG","Size_GB : 40","Volume : SoftVG","Size_GB : 100","Volume : DataVG"]

 

here my full script if it can help other

(function executeRule(current, previous /*null when async*/) {
	
	var arrjson=[]; //array file
	var ritmSysId = current.sys_id; //sysid RITM
	var RITMNumber = current.number; //Number RITM
	
	var BS = RITM.variables.v_rule_ritm_service.getDisplayValue(); //get BS
	var SDM = RITM.variables.v_gen_approval_user.getDisplayValue(); // get SDM BS
	var user = RITM.variables.v_gen_requested_for.getDisplayValue(); // get Requester
	

	arrjson.push("RITM Number:" + RITMNumber);
	arrjson.push("\n" + "BS:" + BS);
	arrjson.push("\n" + "SDM:" + SDM);
	arrjson.push("\n" + "user:" + user);
    // Concerned system
  var  system = RITM.variables.v_dc_system; //var system
	
	arrjson.push("\n" + "System:" + system);
	

	var mrvs = new GlideRecord('sc_multi_row_question_answer'); //query MRVS

	mrvs.addQuery('parent_id',ritmSysId); //Sysid RITM 
	mrvs.addQuery('variable_set','7046e0dcdb87ed108d889f5cd39619f8'); //IO multirow
	mrvs.orderBy('created');
	//mrvs.orderBy('item_option_new');
	mrvs.orderBy('row_index');  
		

//I used 'row_index' to group the rows together. 'created' might also work
//orderByDesc('row_index') also works if you want to sort by descending instead of ascending
	mrvs.query();
	
	
	var rowCount = mrvs.getRowCount();
	var arr=[];

	while (mrvs.next()) {
   // arr.push(mrvs.getValue('value'));
	arr.push(mrvs.getDisplayValue('item_option_new') + ' : ' + mrvs.getValue('value')); 
    //'item_option_new' is the name for the column labelled 'Question'
						}
				
    var partitions = JSON.stringify(arr);
	
	
	var sa = new GlideSysAttachment();


	var document = arrjson + "\n" + "partitions" + partitions; //Array doc

	var ritmRec = new GlideRecord('sc_req_item');
		ritmRec.get(ritmSysId); //sysID RITM to upload the file

		sa.write(ritmRec, RITMNumber + "_json.json", "text/csv", document); //write doc

})(current, previous);

 

View solution in original post

14 REPLIES 14

Hi Pschultz,

 

thanks again, it is better but not optimum, i was trying using for loop to get the rows in their good way, here somes results when apply the while, the order is different from one RITM to another

test 1 :

partitions["C:","32","50","E:"]

test 2 : 

partitions["50","C:","32","E:"]

 

(function executeRule(current, previous /*null when async*/) {
	
	var arrjson=[];
	var ritmSysId = current.sys_id;
	var RITMNumber = current.number;
	
	var BS = RITM.variables.v_rule_ritm_service.getDisplayValue();
	var SDM = RITM.variables.v_gen_approval_user.getDisplayValue();
	var user = RITM.variables.v_gen_requested_for.getDisplayValue();
	

	arrjson.push("RITM Number:" + RITMNumber);
	arrjson.push("\n" + "BS:" + BS);
	arrjson.push("\n" + "SDM:" + SDM);
	arrjson.push("\n" + "user:" + user);
    // Concerned system
  var  system = RITM.variables.v_dc_system;
	
	arrjson.push("\n" + "System:" + system);
	

	var mrvs = new GlideRecord('sc_multi_row_question_answer');

	mrvs.addQuery('parent_id',ritmSysId);
	mrvs.addQuery('variable_set','7046e0dcdb87ed108d889f5cd39619f8'); //IO du multirow
	mrvs.query();
	
	
	var rowCount = mrvs.getRowCount();
	var arr=[];
	
	while (mrvs.next()) {
    arr.push(mrvs.getValue('value'));
						}
				
    var partitions = JSON.stringify(arr);
	
	
	var sa = new GlideSysAttachment();


	var document = arrjson + "\n" + "partitions" + partitions;

	var ritmRec = new GlideRecord('sc_req_item');
		ritmRec.get(ritmSysId);

		sa.write(ritmRec, RITMNumber + "_json.json", "text/csv", document);

})(current, previous);

Hi Nikora,

 

You can set the GlideRecord to return results in order by using mrvs.orderBy('column_name').  Would it help to include the question as well?

 

 

var mrvs = new GlideRecord('sc_multi_row_question_answer');

mrvs.addQuery('parent_id',ritmSysId);
mrvs.addQuery('variable_set','7046e0dcdb87ed108d889f5cd39619f8'); //IO du multirow
mrvs.orderBy('row_index');  
//I used 'row_index' to group the rows together. 'created' might also work
//orderByDesc('row_index') also works if you want to sort by descending instead of ascending
mrvs.query();
	
var rowCount = mrvs.getRowCount();
var arr=[];
	
while (mrvs.next()) {
    arr.push(mrvs.getDisplayValue('item_option_new') + ' : ' + mrvs.getValue('value')); 
    //'item_option_new' is the name for the column labelled 'Question'
}

 

 

Also, per the API docs, you can call the orderBy() method more than once to order by multiple columns.

hi again,

the while loop seems to work as a wizard, try your 2 methods:

mrvs.orderBy('row_index');  

get result : partitions["Volume : RootVG","Size_GB : 32","Size_GB : 40","Volume : SoftVG","Size_GB : 100","Volume : DataVG"]

and with 

mrvs.orderBy('created'); 

get result:

partitions["Size_GB : 40","Size_GB : 32","Volume : RootVG","Size_GB : 100","Volume : DataVG","Volume : SoftVG"]

 

wheras my MRVS is : Capture d’écran 2023-05-26 221602.png

 

and this VRMS is autofilling with client script on change:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   try {
      if(isLoading || newValue == '') {
         return;
      }

      // Type appropriate comment here, and begin script below
		
	  var sysIdMRWS = ('IO:7046e0dcdb87ed108d889f5cd39619f8'); //IO multirow
	   var system = g_form.getValue('v_dc_system'); //system value (replace with your variable name)
	   
	   FillRows(sysIdMRWS,system); //call fonction
	   
   } catch(exception) {
      // Specify the name of the Client Script in the log message
      jslog('Error in Client Script <script_name>: ' + exception);
   }
	
	function FillRows (sysIdMRWS,system) {
		
		//alert(sysIdMRWS);
		//alert(system);
	
		if (system=='WINDOWS'){ //windows value
		
		g_form.setReadOnly(sysIdMRWS, true);
		g_form.setValue(sysIdMRWS, '');
		
			
				var obj = (g_form.getValue(sysIdMRWS).length != 0) ? JSON.parse(g_form.getValue(sysIdMRWS)): [];
				
			obj.push({Volume: 'C:',
				  Size_GB: '32'});
			obj.push({Volume: 'E:',
				  Size_GB: '50'});
			
		g_form.setValue(sysIdMRWS, JSON.stringify(obj));
		//alert('end windows');
		}else{
			if (system=='LINUX'){ //linux value
				g_form.setReadOnly(sysIdMRWS, true);
		g_form.setValue(sysIdMRWS, '');
		
			
				var obj = (g_form.getValue(sysIdMRWS).length != 0) ? JSON.parse(g_form.getValue(sysIdMRWS)): [];
			
			obj.push({Volume: 'RootVG',
				  Size_GB: '32'});
			obj.push({Volume: 'SoftVG',
				  Size_GB: '40'});
			obj.push({Volume: 'DataVG',
				  Size_GB: '100'});	
			
		g_form.setValue(sysIdMRWS, JSON.stringify(obj));
		//alert('end linux');
			}else{ //other value
				g_form.setReadOnly(sysIdMRWS, false);
				g_form.setValue(sysIdMRWS, '');
				//alert('end other');
			}
		}
		
						
			
}		
	
}

We can call the orderBy() method more than once to order by multiple columns.  This should sort into the group by row_index, and then sort by item_option_new to get the questions in the same order each time.

mrvs.orderBy('row_index');  
mrvs.orderBy('item_option_new'); 

 

Can you explain more about your requirements?  Are these values that are static, or do they change dynamically?

Thanks for helping,

My Requirements is simple in the fact, i have a requested catalog item form,

where in fonction of the system selected on a selected filed, i put a client side script to fill the mrvs,

this part is ok, if specific system, client can fill th mrvs by his own.

Once the RITM is submitted i need to create a sctasks(not the first task , because need people to fill some field) to get all the variables values,

included MRVS values, to put them on a file updated into the RITM, this is ok with scripted activity.

my issue with mrvs  is to get the result well formated into the file,

i have two columns (Volume and size) and i would like to put them into the file like row1(column1:Value,Column2:Value)

row2(column1:Value',Column2:Value)

but for a reason i can't explain, the while loop provide me random result, probably depending of TTL response? i don't know