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

Hi Darshani,

Thank you very much. I was able to get that script up and running straight away, and it pulled in various assets, and from there I was able to modify it to pull in the info I wanted from the custom table. Fantastic.

Many thanks and Kind Regards,

Mark.