GlideRecord to populate options dropdown

Mark94
Kilo Expert

Hi,

I'm building a form that when a user is selected, I can populate an options list. Whilst I can manually populate the list, I am unable to look up the values I'd like using the GlideRecord Command.

I've added the Isolate Script box and unticked it as the help advised, but still no records are returned (confirmed using the alert command I've added in - see the screenshot). I've tested the script in isolation and it runs fine, so is this a restriction of where I'm trying to run it, and if so, any ideas how I get around it?

 

find_real_file.png

1 ACCEPTED SOLUTION

Darshani Sambar
Giga Contributor

Hi,

this is client script,

function onChange(control, oldValue, newValue, isLoading) {

   if (isLoading || newValue == '') {
      return;
   }
 
    var getval = g_form.getValue('request_for');
 
var ga = new GlideAjax('populateManager');
ga.addParam('sysparm_name','getAssets');
ga.addParam('sysparm_req_for',getval );
ga.getXML(getResponse);
 
function getResponse(response)
{
var strOption = '';
var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer != null) {
 
   strOption = answer.split(',');
 
   }
 
 
for(var i=0;i<strOption.length;i++) {
 
   g_form.addOption('asset', strOption[i], strOption[i]);
 

   }

script include:

 

var populateManager = Class.create();

populateManager.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getManager: function()
{
var gr=new GlideRecord("sys_user");
gr.addQuery("sys_id",this.getParameter('sysparm_req_for'));
gr.query();
 
while(gr.next())
{
return  gr.manager.name;
}
},
 
getAssets: function()
{
var arr = [];
var sysId =this.getParameter('sysparm_req_for');
var gr = new GlideRecord('alm_asset');
gr.addQuery('assigned_to', sysId);
gr.query();
while(gr.next())
{
arr.push(gr.display_name.toString());
}
if(arr.length > 0)
return arr.toString();
else
return '';
},
 
getCI :function()
{
var arr = [];
var sysId =this.getParameter('sysparm_req_for');
var gr = new GlideRecord('cmdb_ci');
gr.addQuery('assigned_to', sysId);
gr.query();
while(gr.next())
{
arr.push(gr.name.toString());
}
if(arr.length > 0)
return arr.toString();
else
return '';
},
 
type: 'populateManager'

});

 

 

Use this code and replace fields with your matching table fields. Use array concept given here you can check the records in drop down list.

 

 

 

 

 Please mark it as helpful/correct based on impact.

Thanks

Darshani

View solution in original post

5 REPLIES 5

Michael Jones -
Giga Sage

As you are calling this client-side, you will need to use GlideAjax to send a request to the server and return the information. 

Here is an example that pulls records from the cmdb_ci_spkg table and populates a select box named "software" with options. 

 

Name: populateSelectBox

Client-Callable = true

 

var populateSelectBox = Class.create();
populateSelectBox.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    type: 'populateSelectBox',
    getSoftware: function() {
		
		//Queries the cmdb_ci_spkg table for all records with the attribute showInPortal
		//and constructs a JSON Object as a response with name and sys_id
        var retStr = [];
        var jStr = '[';
        var gr_software = new GlideRecord('cmdb_ci_spkg');
        gr_software.addEncodedQuery('attributesLIKEshowInPortal');
        gr_software.orderBy('name');
        gr_software.query();
        while (gr_software.next()) {
            jStr += '{"name":"' + gr_software.getValue('name') + '","id":"' + gr_software.getValue('sys_id') + '"},';
        }
        jStr = jStr.substring(0, jStr.length - 1);
        jStr += "]";
        return jStr;
    }
});

 

Then, in your catalog call this script onload to set the values (this assumes that none was included in the variable definition). Just replace "software" with the name of your variable. 

 

function onLoad() {

//Call GlideAjax to populate the software list
	
    var ga = new GlideAjax('populateSelectBox');
    ga.addParam('sysparm_name', 'getSoftware');
    ga.getXML(ajProcessor);
	

    function ajProcessor(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");

        var obj = JSON.parse(answer);
        var i;
        for (i = 0; i < obj.length; i++) {
//replace software with your variable name
            g_form.addOption('software', obj[i].id, obj[i].name);
        }
    }
}

 

If this was helpful or correct, please be kind and remember to click appropriately!

Michael Jones - Proud member of the CloudPires team!

I hope this helps!
Michael D. Jones
Proud member of the GlideFast Consulting Team!

Manas Kandekar
Kilo Guru

Hi

Using GlideRecord in client side is not a best practice to follow, instead use GlideAjax and script include behavior to achieve your requirement.

Mark94
Kilo Expert

Crikey. Ok. I'll give that a go. I'll let you know how I get on.

Thank you very much!

Kind Regards,

Mark.

Darshani Sambar
Giga Contributor

Hi,

this is client script,

function onChange(control, oldValue, newValue, isLoading) {

   if (isLoading || newValue == '') {
      return;
   }
 
    var getval = g_form.getValue('request_for');
 
var ga = new GlideAjax('populateManager');
ga.addParam('sysparm_name','getAssets');
ga.addParam('sysparm_req_for',getval );
ga.getXML(getResponse);
 
function getResponse(response)
{
var strOption = '';
var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer != null) {
 
   strOption = answer.split(',');
 
   }
 
 
for(var i=0;i<strOption.length;i++) {
 
   g_form.addOption('asset', strOption[i], strOption[i]);
 

   }

script include:

 

var populateManager = Class.create();

populateManager.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getManager: function()
{
var gr=new GlideRecord("sys_user");
gr.addQuery("sys_id",this.getParameter('sysparm_req_for'));
gr.query();
 
while(gr.next())
{
return  gr.manager.name;
}
},
 
getAssets: function()
{
var arr = [];
var sysId =this.getParameter('sysparm_req_for');
var gr = new GlideRecord('alm_asset');
gr.addQuery('assigned_to', sysId);
gr.query();
while(gr.next())
{
arr.push(gr.display_name.toString());
}
if(arr.length > 0)
return arr.toString();
else
return '';
},
 
getCI :function()
{
var arr = [];
var sysId =this.getParameter('sysparm_req_for');
var gr = new GlideRecord('cmdb_ci');
gr.addQuery('assigned_to', sysId);
gr.query();
while(gr.next())
{
arr.push(gr.name.toString());
}
if(arr.length > 0)
return arr.toString();
else
return '';
},
 
type: 'populateManager'

});

 

 

Use this code and replace fields with your matching table fields. Use array concept given here you can check the records in drop down list.

 

 

 

 

 Please mark it as helpful/correct based on impact.

Thanks

Darshani