Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Client script to populate drop down variable based on another variable

servicenow_devo
Tera Expert

Hi,

I have a requirement to populate a variable in drop down list in catalog item based on other variable features. 

 

Scenario-

need to show computer model in model variable. But based on users company I need to show only few model from available list.

Eg -for one company need to show 2 choices 

Wheras for another need to show 4 choices from available list

Thankyou
1 ACCEPTED SOLUTION

Vishal Birajdar
Giga Sage

Hello @servicenow_devo 

 

We also had similar requirement but on different variables. You can follow the below steps to achieve this requirement.

 

Pre-requisites :

1.Need below Variables on Catalog Item

A] Variable Name      : Requested For

     Type                        : reference

     Referenced Table : sys_user

B] Variable Name      : Request Type

     Type                        : Select Box                       //I used a select box here 

     Referenced Table : question_choice

Dont add any choices in related list 

 

VishalBirajdar7_8-1676616274418.png

 

 

 

 

Step 1 : Create a new Entry in 'Question'[question] table. I have created 2 Entries for specific Company for demo 

E.g.,

For Company : ACME Italy created one entry 

Type : Single line text

Question : ACME Italy

 

For Company : Acer created one entry 

Type : Single line text

Question : Acer

 

VishalBirajdar7_0-1676614963909.png

 

Step 2 : Create a choices in 'question_choice' tables for each question

I'll show you for question 'Acer' just we have created in step 1

Select Question as a Acer in reference field as shown in below screen shot.

VishalBirajdar7_1-1676615157555.png

 

We can create a 'N' number of entries in "Question choice" table for question 'Acer' and 'ACME Italy'

VishalBirajdar7_2-1676615350667.png

 

For ACME Italy

VishalBirajdar7_3-1676615414946.png

 

Step 3 : Write a Below Client callable Script include and onChange client script to get choices

 

Script Include : 

 

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

getChoices: function() {
var reqFor = this.getParameter('sysparm_reqFor');
var company;
var choices=[];

var grUser = new GlideRecord('sys_user');
grUser.addQuery('sys_id', reqFor);
grUser.query();
if (grUser.next()) {
company = grUser.getDisplayValue('company').toString(); //will get Display name of company
}
//gs.log('getCompanyChoiceList:company=' + company);

//glide record on quetion choice table
var grChoices = new GlideRecord("question_choice");
grChoices.addEncodedQuery("question.question_text=" + company);
grChoices.query();
while (grChoices.next()) {
choices.push (grChoices.getValue('value').toString());
}
//gs.log('getCompanyChoiceList:choice=' + choices);
return JSON.stringify(choices);
},
type: 'getCompanyChoiceList'
});

VishalBirajdar7_4-1676615603202.png

 

onChnage Client script :

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

var reqFor = g_form.getValue('requested_for');
var gaINCUsers = new GlideAjax('getCompanyChoiceList'); // Glide Ajax
gaINCUsers.addParam('sysparm_reqFor',reqFor ); // variable values passed to script include
gaINCUsers.addParam('sysparm_name', 'getChoices'); // function name in script include
gaINCUsers.getXMLAnswer(callback);

function callback(answer) {

var results = JSON.parse(answer);
var getValue = g_form.getValue('request_type');
for(var i=0; i<results.length ;i++){
g_form.addOption('request_type',results[i],results[i]);
}
}
}

VishalBirajdar7_9-1676616373804.png

 

 

 

 

Output :

 

Test Case 1 : Requested for has company : Acer

VishalBirajdar7_5-1676615827341.png

 

Test Case 2 : Requested for has company : ACME Italy

 

VishalBirajdar7_6-1676615958876.png

You can adjust code with your variable names and with best practices.

 

Hope this will work for you...!!!

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

View solution in original post

6 REPLIES 6

Sonu Parab
Mega Sage

Hi @servicenow_devo ,
Have a look at this post. How to set the dropdown option based on previous reference field value? 

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!
Thank you

Hi @Sonu Parab .

thanks for responding. but this is based on a particular specific value Right and statically setting.

 

I need to Implement dynamically based on user company taken from requested for field from user table and use this for showing specfic choices.

 

Can you help me out showing a script for example

Thankyou

Hi @servicenow_devo , Could you please share the snippet of catalog item varibales and share the Variable type so that I can help you to resolve your issue.

Thnak you

Hello @Sonu Parab,

 

1.Requested for variable -reference to user table.

2. Request type-Choice drop down

Based on company of requestor for from user table we need to populated drop down choices.

Eg-for company x need to show 5 choices from drop down.

For Y need to show only 2 choices.

Choices are fixed in count but based on requesting user form should show only few choices.

 

Thankyou 

Thankyou