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
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.

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

Pavankumar_1
Mega Patron

Hi @reddy8055 ,

refer below question it will helps you.

https://www.servicenow.com/community/developer-forum/get-mrvs-row-count-onsubmit/m-p/1545351

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

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**

Pavankumar_1
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.

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar