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.

add drop down menu to widget

asd22
Tera Contributor

Hello. 

 

i need to add one of my dropbox to a widget, can i do this in html? Where i get the dropbox from backend and display it in the widget? 

dropbox name u_choice_3

1 ACCEPTED SOLUTION

Nawal Singh
Tera Guru

Hi @asd22 ,

Create a new widget and add the code below- 

 

Server script- 

(function() {
  // Example: get choices from a Choice table or custom logic
  var choices = [];
  var gr = new GlideRecord('sys_choice');
  gr.addQuery('name', 'your_table_name'); // specify table if using choices
  gr.addQuery('element', 'u_choice_3');   // field name for the choice field
  gr.query();
  while (gr.next()) {
    choices.push({
      label: gr.label.toString(),
      value: gr.value.toString()
    });
  }

  data.choices = choices;
})();

 

Client script- 

 

function($scope) {
  $scope.selectedChoice = '';
  
  $scope.onChoiceChange = function() {
    console.log("Selected choice:", $scope.selectedChoice);
  };
}

 

HTML Code- 

 

<div>
  <label for="u_choice_3">Choose an option:</label>
  <select id="u_choice_3"
          ng-model="selectedChoice"
          ng-options="c.value as c.label for c in data.choices"
          ng-change="onChoiceChange()">
    <option value="">-- Select --</option>
  </select>
</div>

 

If you found my response helpful, please mark it as helpful and accept it as the solution.

Thank you
Nawal Singh

View solution in original post

3 REPLIES 3

Nawal Singh
Tera Guru

Hi @asd22 ,

Create a new widget and add the code below- 

 

Server script- 

(function() {
  // Example: get choices from a Choice table or custom logic
  var choices = [];
  var gr = new GlideRecord('sys_choice');
  gr.addQuery('name', 'your_table_name'); // specify table if using choices
  gr.addQuery('element', 'u_choice_3');   // field name for the choice field
  gr.query();
  while (gr.next()) {
    choices.push({
      label: gr.label.toString(),
      value: gr.value.toString()
    });
  }

  data.choices = choices;
})();

 

Client script- 

 

function($scope) {
  $scope.selectedChoice = '';
  
  $scope.onChoiceChange = function() {
    console.log("Selected choice:", $scope.selectedChoice);
  };
}

 

HTML Code- 

 

<div>
  <label for="u_choice_3">Choose an option:</label>
  <select id="u_choice_3"
          ng-model="selectedChoice"
          ng-options="c.value as c.label for c in data.choices"
          ng-change="onChoiceChange()">
    <option value="">-- Select --</option>
  </select>
</div>

 

If you found my response helpful, please mark it as helpful and accept it as the solution.

Thank you
Nawal Singh

Hello this did not work, it did not get the dropbox thats already made, it just made a new dropbox

Hi @asd22 ,

can you please share what you have already done so far and where you are stuck.so I can assist further. please share screenshot .