mvrs : Need to count number of rows

Patrick Boutet
Mega Expert

I have a catalog item with a multiple row variable set. 

I would like to display (in another field of the form), information related to mvrs data
For example, number of rows.

I tried with onSubmit catalog script on mvrs but found that this kind of script can't access variables outside mvrs.

Do you have any idea ?

11 REPLIES 11

Kunal Varkhede
Tera Guru

Hi,

 

Try Below,

OnSubmit() Client Script

var length = '';
var mvrsValue = g_form.getValue('questions');
if(mvrsValue)               //if it has a value
{
  var jsonObj = JSON.parse(mvrsValue);
  console.log('There are ' + jsonObj.length + ' rows);
  length = jsonObj.length;

} else { // if i recall correctly, .getValue returns null if empty
  console.log('No Rows');
  length = jsonObj.length;
}
//CALL Script Include to Store Length in Another table
var ga = new GlideAjax('StoreDetails');//this is the script include
ga.addParam("sysparm_name", "storeLength"); //this is the function within the script include
ga.addParam("sysparm_length",length);
ga.getXML(callback);
function callback(response)
{
var answer = response.responseXML.DocumentElement.getAttribute("answer");
alert(answer);
}

 }

 

Script Include to store length to another table

var StoreDetails = Class.create();

StoreDetails.prototype = {

     
  storeLength : function() {

  var length = this.getParameter('sysparm_length');

  var gr = new GlideRecord('your table name');

  gr.addActiveQuery();

  gr.query();

  if(gr.next())

 {

   gs.addInfoMessage("length=" +length);

   gr.field_name = length;

   gr.update(); 

 }

    },

      type: 'getStandardFields'

};

 

Please mark correct/helpful answer if it help you in any way.

Thanks,

Kunal

 

Hi, 

Thank you very much for your answer. It really helps.
I also need to display this count in the cat item form. 


Is there a way (in client side)

  - to 'see' modification of length in a record of 'my table name' 
  - then retrieve this value
  - and display in form 

Hi,

 

Updated script:

 

OnSubmit() Client script:

var length = '';
var mvrsValue = g_form.getValue('questions');
if(mvrsValue)               //if it has a value
{
  var jsonObj = JSON.parse(mvrsValue);
  console.log('There are ' + jsonObj.length + ' rows);
  length = jsonObj.length;

} else { // if i recall correctly, .getValue returns null if empty
  console.log('No Rows');
  length = jsonObj.length;
}
//CALL Script Include to Store Length in Another table
var ga = new GlideAjax('StoreDetails');//this is the script include
ga.addParam("sysparm_name", "storeLength"); //this is the function within the script include
ga.addParam("sysparm_length",length);
ga.getXML(callback);
function callback(response)
{
var answer = response.responseXML.DocumentElement.getAttribute("answer");

alert(answer);  //this will show length


}

 }

Script Include:

var StoreDetails = Class.create();

StoreDetails.prototype = {

     
  storeLength : function() {

  var length = this.getParameter('sysparm_length');

  var gr = new GlideRecord('your table name');

  gr.addActiveQuery();

  gr.query();

  if(gr.next())

 {

   gs.addInfoMessage("length=" +length);

   gr.field_name = length;

   gr.update(); 
   

   // Add this line

   return gr.field_name;  //Pass this length to client script 


 
 }

    },

      type: 'getStandardFields'

};

 

Thanks,

Kunal

In callback function, you may display count with alert

But I need to put this value in a specific field 


And g_form("my_field',count) won't work because 'my_field' is not inside mrvs

We may create an action button/action or select zone in the form to read count in table and put it in the field but this is not user friendly (End user need to do something to read value)

asifnoor
Kilo Patron

Hi

If you want to access the data that is outside of MRVS into the variable set, then refer to this link. 

https://community.servicenow.com/community?id=community_article&sys_id=097a0fc01bcac0107a5933f2cd4bc...

Mark the comment as a correct answer and helpful if it helps.