asifnoor
Kilo Patron

Hi,

In this article, I will share a code snippet on how to auto populate a multi variable set based on a specific value in the record producer.

In my example, based on a field change in record producer I want to fetch the entries from the database and prefill them in the multirow variable set. 

Client Script

function onChange(control, oldValue, newValue, isLoading) {
  if (isLoading || newValue == '') {
    return;
  }
  var ga = new GlideAjax('MyScriptInclude');
  ga.addParam('sysparm_name', 'getItems');
  ga.addParam('sysparm_type', newValue);
  ga.getXML(parseData);

  function parseData(response) {
    var answer = response.responseXML.documentElement.getAttribute('answer');
    var oDetails = JSON.parse(answer);
    var my_varset = g_form.getField("my_multirow_varset"); //get your multivarset
    for(var sKey in oDetails){
      if (sKey == "my_multirow_varset") {
	g_form.setValue(sKey, oDetails[sKey]); //This will set the multirow once the key is matched.
      } else if (sKey == "total_rows") {
          //sets the max rows to the available count, bcoz of which the add button will be disabled. you can also set max_rows_size=0 to disable add.
	  my_multirow_varset.max_rows_size = oDetails[sKey]; 
      }
    }
		
    if(answer == 0) {
      //No data for this Type. So hide the multirow var set
      g_form.addInfoMessage('There are no Items for this Type '+newValue);
      g_form.setVisible('my_multirow_varset',false);
    }
  }
}

Script Include

var MyScriptInclude = Class.create();
MyScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {
  getItems: function(){
    var itemID = this.getParameter('sysparm_type');
    var oDetails = {};
    var aDetails = [];
    oDetails = new MyScriptInclude().getAllDetails(itemID);
    aDetails = [{
       "total_rows": oDetails["total_rows"],
       "my_multirow_varset": JSON.stringify(oDetails["my_multirow_varset"])
    }];
    return JSON.stringify(aDetails[0]);
  },
  getAllDetails: function(itemID){
    var oDet = {};
    var aDet = [];
    var total_rows=0;
    var grItem = new GlideRecord("mytable");
    grItem.addQuery('u_item_id', itemID);
    grItem.query();
    while(grItem.next()){
      total_rows++;
      oDet = {"type": grItem.u_type+ "",
              "title": grItem.u_title+ "",
              "description": grItem.short_description+ "",
              "in_stock": grItem.u_stock+ "",
              "supervisor": grItem.u_manager+ "",
              "item_number": grItem.u_number+ ""
	     };
	aDet.push(oDet);
      }
      return {"total_rows": total_rows, "my_multirow_varset": aDet};
   },

  type: 'MyScriptInclude'
});

To disable edit and delete icons, make the multirow variable set readonly.

You can create a ui policy and can make it readonly or from the client script, you can do it

g_form.setReadOnly('my_multirow_varset',true);

To disable Add button, you can call

my_multirow_varset.max_rows_size=0;

Let me know if you have any questions in the comments below.

Mark the article as helpful and bookmark if you found it useful.

 

Comments
asifnoor
Kilo Patron

Hi,

If you want to add the rows dynamically to the MRVS based on any field input, check this article.

https://community.servicenow.com/community?id=community_article&sys_id=84fd4a841b42c0107a5933f2cd4bc...

jbrooke
Tera Contributor

Hello Asifnoor,

Thank you for the above code. I was exactly what I needed for my service portal form. I am unable to get the following statement to work

my_multirow_varset.max_rows_size = oDetails[sKey];

With the above statement in the change script, the code does not execute and no error message are logged. If I comment this statement out, my multi-row variable set it populated. I would like to set the max rows, but it is not working. Do you have a suggestion?

I am on New York version.

Thanks

Jana

asifnoor
Kilo Patron

Hi,

Print the value of the key and check if its entering inside the block or not.

else if (sKey == "total_rows") {
console.log("sKey is "+sKey);
          //sets the max rows to the available count, bcoz of which the add button will be disabled. you can also set max_rows_size=0 to disable add.
	  my_multirow_varset.max_rows_size = oDetails[sKey]; 
      }

Also, check in the browser console if you see any error.

Mark the comment as helpful if it helps.

jbrooke
Tera Contributor

Hello,

Yes there is value in total_rows. I even tried setting to a constant. I tried setting to 0 and the statement does not execute. Also, the statement getField doesn't seem to work. Maybe this is causing the set max rows not to work. Any suggestions will be appreciated.

 

Thanks

Jana

asifnoor
Kilo Patron

Hi,

Sorry for the delay.

Set your client script UI type to Mobile/Service Portal and getField will work.

Mark the comment as helpful if it helps.

jbrooke
Tera Contributor

Hello,

I had the type set to All. I was testing with the Try it button and it wasn't working. Then I set my item up on the service portal and it worked.

Thanks for this information. It was very helpful.

 

asifnoor
Kilo Patron

Glad it worked 🙂

Indup
Tera Expert
Very good article. Thank you Asif Regards, Indup
Antonio24
Giga Contributor

hi asinfnoor,

thanks for the article, that was what i was looking for.

 

Dunno if you can help with my issue: my requirement is to use a mrvs in RITM form, the agent will fill a reference field (refered to custom bundle table, my customer decided to call it "Template") and I want that all related records of choosen template will fill a mrvs, for tracking and to add/remove elements without update the template (if i remove elements from the mrvs nothig should happen, if i add element/s then we need the primary contact approvation).

Hope you can help to achive that, if you need more information you can send me an email at "antonio.lastrina@gmail.com"

 

Thanks in advice.

Geo George
Kilo Guru
var grItem = new GlideRecord("mytable");
    grItem.addQuery('u_item_id', itemID);

What would be the value in the place u_item_id, if I take incident table.

Sagar Anandpara
Tera Contributor

This is amazing, works very well. Thanks @asifnoor .

I got couple of queries please.

1. Is is possible to sort the entries pre-filled in the MRVS.?

2. Is there pagination possible.?

3. How will the system perform, and MRVS look if we have more than 50 rows to be prefilled in the MRVS. Is there a property we can setup to add pagination to display only 10 records, so 5 pages for total 50 entries in the MRVS.?

 

 

Community Alums
Not applicable

Hi i have to insert a empty row if a catalog item is selected 

AbdulKhader_0-1668497610257.png

 

asifnoor
Kilo Patron

1. u_item_id can be a sys_id or any other id. Depends on what data you want to query from your table, use that query.

2. Pagination, you have to cusotm implement it. By default it is not possible in the MRVS. And if here are around 50 rows, then the loading of the page will take time.

Mickeybj
Tera Contributor

Hi Asif,

 

Can we hide a variable under MRVS based on an option selected in a variable in record producer??

bbottom
Tera Contributor

Hi Asifnoor,

This is great information on MRVS's. I was wondering if there is a way to display multiple Rows and add numbering to the first field of each row using a number generator and start with the next number from the last recorded number. I have a client that wants to auto generate photo numbers based on the number of photo's taken. I have a Variable on the request that ask for the number of photo's.

 

Thanks

Barry

Version history
Last update:
‎11-15-2019 05:26 AM
Updated by: