How to set the Values in a drop down based on the select value in 1st drop down using catalog client script

Gowtham Kodali
Tera Contributor

Hi,

 

I have 2 drop downs in a catalog item I added all the possible values  to the drop down ,

Now if I select option A in the First drop down I have to get the options   X,Y,Z in the second drop down

if I select option B in first dropdown I have to get options  R,S,T

 

I have used a catalog client script and 

 

i am trying like this 

 

var val = g_form.getValue('DropDown1 fieldName');

if(val == 'dropdown option value')

{

g_form.setValue('dropdown2fieldname','dropdown2optionvalue')

}

any wrong in that

how can i achieve this 

1 ACCEPTED SOLUTION

asifnoor
Kilo Patron

Hi

There are 2 ways.

1. you can configure option2 to be dependent on option 1. In this case the values of dropdowns should come from database.

2. If the values of dropdowns are static, then write onChange client script on option 1 and then add code like below

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
   if(newValue == "A") {
       g_form.clearValues("option2");//put actual field name
       g_form.addOption("option2","X","X");
       g_form.addOption("option2","Y","Y");
       g_form.addOption("option2","Z","Z");
   }
}

Mark the comment as a correct answer and helpful if this helps.

View solution in original post

18 REPLIES 18

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Gowtham,

Please check below points

1) ensure you write onChange catalog client script on Variable 1

2) Ensure you give proper drop down values and label when you use g_form.addOption('variable', 'choiceValue', 'choiceLabel')

3) ensure you compare proper choice values for variable 1

Regards
Ankur

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

Ankur Bawiskar
Tera Patron
Tera Patron

@Gowtham Kodali 

Hope you are doing good.

Let me know if I have answered your question.

If so, please mark appropriate answer as correct & helpful to close the thread.

If not, please let us know if you need some more assistance.

Regards
Ankur

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

Ankur Bawiskar
Tera Patron
Tera Patron

@Gowtham Kodali 

Hope you are doing good.

Let me know if I have answered your question.

If so, please mark appropriate answer as correct & helpful to close the thread.

If not, please let us know if you need some more assistance.

Regards
Ankur

 

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

Ankur Bawiskar
Tera Patron
Tera Patron

@Gowtham Kodali 

Hope you are doing good.

Let me know if I have answered your question.

If so, please mark appropriate answer as correct & helpful to close the thread.

If not, please let us know if you need some more assistance.

Regards
Ankur

 

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