Populate select box variable based on the selections from reference and list collector variables

redth
Giga Expert

Hi,

I need to populate a select box variable based on the selections from a reference variable (app_name) and list collector variable (u_database_name1)

I'm using a script include and catalog client script to achieve that functionality. But it's not working. Can you please let me know where to correct the script?

Script Include:

var Getroles = Class.create();
Getroles.prototype = Object.extendsObject(AbstractAjaxProcessor, {
Getbusinessroles: function()
{
var v1 = this.getParameter('sysparm_appid');
var v2 = this.getParameter('sysparm_dbname');
var result = this.newItem("result");
var busrole = new GlideRecord('u_database_role');
busrole.addQuery('u_appid',v1);
busrole.addQuery('u_dbname',v2);
busrole.query();

if(busrole.next()){
return busrole.u_business_role;
}
},
type: 'Getroles'
});

Catalog client script:

Type: Onchange of u_database_name1

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var gr = new GlideAjax('Getroles');
gr.addParam('sysparm_name','Getbusinessroles');
gr.addParam('sysparm_appid', g_form.getDisplayBox('app_name').value);
gr.addParam('sysparm_dbname', g_form.getDisplayValue('u_database_name1'));
gr.getXML(getbusrole);
alert('bus1'+g_form.getDisplayBox('u_database_name1').value);
alert('bus11'+g_form.getDisplayBox('app_name').value);

function getbusrole(response){
//g_form.clearOptions('u_business_roles');
g_form.addOption('u_business_roles','','---None---');
g_form.addOption('u_business_roles','','Role not found');

var result = response.responseXML.documentElement.getAttribute("result");
g_form.addOption('u_business_roles', result);

}
}

1 ACCEPTED SOLUTION

That's actually good that they are both reference fields on the custom table, so in the client script

gr.addParam('sysparm_appid', g_form.getValue('app_name').toString());
gr.addParam('sysparm_dbname', g_form.getValue('u_database_name1).toString());

The script include depends on what tables are being referenced.  Is your app_name variable referencing the same table as your u_appid field?  Is your u_database_name1 variable referencing the same table as your u_dbname field?  If yes to both, no changes needed to the script include.  If either or both table fields are different, let me know how the variable sys_id relates to the field table.

View solution in original post

10 REPLIES 10

Brad Bowman
Kilo Patron
Kilo Patron

Hi akred,

I would put your alerts before the getXML, and make them consistent with the addParams (getDisplayValue vs getDisplayBox) to make sure you're passing the correct values to the script include for each parameter.  Y ou'll see that the list collector variable is storing a comma-separated string - one for each value selected.  I usually just use g_form.getValue which would give you a list of sys_ids to use in the GlideAjax, but if getDisplayValue is giving you a list of names, then OK.  app_name should probably also use getValue, depending on the field type of u_appid, but again, if getDisplayBox is giving you the value that matches that field, then all the best.  Once you confirm that you are getting the correct values for both of these, your script include needs some changes since you will/may have a list of dbname values.  The addQuery line for u_dbname should be

busrole.addQuery('u_dbname', 'IN', v2);  

The rest of the GlideRecord portion of the script, after the .query() line should look like this since you are returning multiple records with your query

while(busrole.next()){
answer.push(busrole.u_business_role.toString()); //adds the u_business_role from each returned record into an array
}

return answer.join(',');
}

You should add a line near the beginning of the script

 var answer = [];

This will return a comma-separated list of u_business_role values to the client script.  Your client script makes it look like you're only expecting one u_business_role returned for all of the selected dbnames if that's the case, you still need to change the addQuery line, but you can leave everything after that the way it was and you'll just get the role from whichever dbname record was returned first.  Add an alert after var result to see if you're getting the value(s) back from the server that you expect.  Lastly, for now, you need to specify three parameters with addOption - fieldName, choiceValue, and choiceLabel) - even if choiceValue and choiceLabel are the same.

Hi Brad,

Thanks for the input.

I have made the changes you suggested. But, the script include is returning empty values.

In the client script, I have used getDisplayBox for app_name (reference variable) as it's returning the name and used getValue for u_database_name1(list collector) now, it's returning sys_id. Can I leave this as it is or should I change it list collector so that it will return name instead of sys_id.

Here's the modified versions:

Script Include:

var Getroles = Class.create();
Getroles.prototype = Object.extendsObject(AbstractAjaxProcessor, {
Getbusinessroles: function()
{

var answer = [];
var v1 = this.getParameter('sysparm_appid');
var v2 = this.getParameter('sysparm_dbname');
var result = this.newItem("result");
var busrole = new GlideRecord('u_database_role_repository');
busrole.addQuery('u_appid',v1);
busrole.addQuery('u_dbname','IN',v2);
busrole.query();
while(busrole.next()){
answer.push(busrole.u_business_role.toString());
}
return answer.join(',');
},

type: 'Getroles'
});

Client Script:

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var gr = new GlideAjax('Getroles');
gr.addParam('sysparm_name','Getbusinessroles');
gr.addParam('sysparm_appid', g_form.getDisplayBox('app_name').value);
gr.addParam('sysparm_dbname', g_form.getValue('u_database_name1));
alert('appname'+g_form.getDisplayBox('app_name').value);
alert('dbname'+g_form.getValue('u_database_name1'));
gr.getXML(getbusrole);


function getbusrole(response){
//g_form.clearOptions('u_business_roles');
g_form.addOption('u_business_roles','','---None---');
g_form.addOption('u_business_roles','','Role not found');
var msg = response.responseXML.documentElement.getAttribute("result");

alert('answer' +msg);
for(var i=0;i<msg.length;i++)
{
g_form.addOption('u_business_roles',msg[i], msg[i]);
}

}
}

This depends on the field types on your custom table.  So v1 in the script include is going to be something like 'the name of the application'.  Is u_appid on u_database_role_respository a field with Type of String, and is there a record with appid = 'the name of the application' or whatever is in v1?

v2 contains a list of sys_ids, or maybe it can contain a list of names if you're able to get that to work with a list collector variable type.  If the field Type of u_dbname is String, you'll need a list of names that match records on this table.  If you can't get the list collector to pass in the display value/names, then another GlideRecord query will be needed in the script include to lookup the names based on the sys_ids.

If you want more than one u_business_role returned to the client for the purpose of adding an option for each, you'll need a modification in your client script to convert the comma-separated list of names into an array.  You might be able to combine this with the var msg line, or you might have to delcare a new array variable after your answer alert

msg.split(',');   

 

Coming to V1, it's a reference field both in the custom table and catalog item.

V2 is list collector in the catalog item and reference field in custom table. I'm not able to retrieve the display values from the client script for this, getting only sys_ids. Can you let me know what changes I may need to make in the script include to get the display values?