Join us Dec. 16 for The Community Hour, our first global virtual User Group event!

How to get length of MRVS row count from client script

reddy8055
Tera Contributor

Hi,

 

How can I get length of MRVS row count. I am tried using the below script to get length, I am able to get length in CMS view (alerts are showing correctly).But in Portal, I do not see any alerts if I use JSON.parse, I am able to see alerts incorrectly if i use stringify. Any help??

 

var mrvs = g_form.getValue('vta_training_details'); // internal name of your MRVS
//alert(mrvs);
var obj = JSON.parse(mrvs);
alert(obj);
var MRVSlength = obj.length;
alert(MRVSlength);

 

thanks,

8 REPLIES 8

jaheerhattiwale
Mega Sage

@reddy8055 On client script "UI Type" field might be set to Desktop please change it to All. That should fix the issue.

jaheerhattiwale_0-1671253605777.png

 

Please mark as correct answer if this solves your issue.

PavanK960672992
Mega Patron

reshmapatil
Tera Guru

Hi @reddy8055 ,

 

Try to use

A MVRS is literally stored as JSON.  You can parse the JSON and get it's length.

e.g.

g_form.getValue('questions'); returns below
/*
"[
  {
    \"order\":\"100\",
    \"question\":\"What do you need help with?\",
    \"required\":\"Yes\",
    \"help_text\":\"\"
  }
]"
*/

So with that... you can literally just say run a script like so;

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);
} else { // if i recall correctly, .getValue returns null if empty
  console.log('No Rows');
}

 

Check the below links for more details:

https://www.servicenow.com/community/developer-blog/json-parsing/ba-p/2279166

 

https://www.servicenow.com/community/developer-forum/json-parse-not-working-in-service-portal/m-p/14...

 

Regards,

Reshma

**Please mark my answer correct or helpful based on the impact**

PavanK960672992
Mega Patron

Hi @reddy8055 ,

If my response helped you to resolved the issue then close the question by Accepting solution and hit thumb icon. From Correct answers others will get benefited in future.