How to populate Multi Row Variable set from Table

reuven paz1
Kilo Contributor

Hello team 🙂

I have a catalog item with a multi row variable set (mrvs  called contact). I need to populate 10 record from contact (customer_contact) table.I want to populate this MRVS onLoad.

I have built the following script include to get the data:

var getContactDataList = Class.create();
getContactDataList.prototype = Object.extendsObject(AbstractAjaxProcessor,{
    
    getDataList: function() {
        
        // setup variables
        
        var query_param = this.getParameter('sysparm_query_param');
        var array = [];
        var result = '';
        var gr = new GlideRecord('customer_contact');
        
        
        gr.addQuery('first_name');
        gr.query();
        while(gr.next()){
            
            // push selected values into array
            
            array.push({
                first_name: gr.getValue('first_name'),
                email: gr.getValue('email'),
                last_name: gr.getValue('last_name')
        });
        }
        // store result in JSON formatted string
        
        result = JSON.stringify(array); 
        return result;        
    },

    type: 'getContactDataList'
});

I've created an onLoad client catalog script and associated it with the variable set "contact":

function onLoad() {
  
	
		// call script include
	var ga = new GlideAjax('getContactDataList');
	
	// function to call
	
	ga.addParam('sysparm_name', getDataList);

	ga.getXML(updateDataList);
}

 function updateDataList(response){
 
var val =  response.responseXML.documentElement.getAttribute("answer");
 // set mrvs with retrieved values
 g_form.setValue('contact', val);
 }
	

Nothing is being populated in the MRVS onLoad.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

update as this

Script Include:

var getContactDataList = Class.create();
getContactDataList.prototype = Object.extendsObject(AbstractAjaxProcessor,{

	getDataList: function() {
		// setup variables
		var array = [];
		var gr = new GlideRecord('customer_contact');
		gr.setLimit(10);
		gr.query();
		while(gr.next()){
			// push selected values into array
			array.push({
				first_name: gr.getValue('first_name'),
				email: gr.getValue('email'),
				last_name: gr.getValue('last_name')
			});
		}
		// store result in JSON formatted string
		return JSON.stringify(array); 
	},

	type: 'getContactDataList'
});

Client Script: Applies to Catalog Item and not Variable Set

function onLoad() {
	
	// call script include
	var ga = new GlideAjax('getContactDataList');
	// function to call
	ga.addParam('sysparm_name', 'getDataList');
	ga.getXMLAnswer(updateDataList);
}

function updateDataList(response){
	var answer =  response;
	g_form.setValue('contact', answer);
}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

3 REPLIES 3

Brad Bowman
Kilo Patron
Kilo Patron

If the MRVS is blank, and you want to populate it when the Catalog Item, RITM, or Catalog Task loads, then your onLoad Catalog Client Script needs to be associated with the Catalog Item, not within the MRVS.  As with all script troubleshooting, add alerts to your Client Script and gs.info, etc to your Script Include to ensure it's running, and to see how far it is getting, records returned, variables populated, etc.

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

update as this

Script Include:

var getContactDataList = Class.create();
getContactDataList.prototype = Object.extendsObject(AbstractAjaxProcessor,{

	getDataList: function() {
		// setup variables
		var array = [];
		var gr = new GlideRecord('customer_contact');
		gr.setLimit(10);
		gr.query();
		while(gr.next()){
			// push selected values into array
			array.push({
				first_name: gr.getValue('first_name'),
				email: gr.getValue('email'),
				last_name: gr.getValue('last_name')
			});
		}
		// store result in JSON formatted string
		return JSON.stringify(array); 
	},

	type: 'getContactDataList'
});

Client Script: Applies to Catalog Item and not Variable Set

function onLoad() {
	
	// call script include
	var ga = new GlideAjax('getContactDataList');
	// function to call
	ga.addParam('sysparm_name', 'getDataList');
	ga.getXMLAnswer(updateDataList);
}

function updateDataList(response){
	var answer =  response;
	g_form.setValue('contact', answer);
}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Dear Ankur Bawiskar,

Thank for your quick reply.

i have tried what you mention above and still not getting the desired results

 

note: Applies to Catalog Item