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.

Need to implement dynamic filter based on drop down value selection

srinivas48
Tera Contributor

Hi All,

I have a requirement is about dynamic filter I'm facing a challenge while implementing this requirement can anyone suggest me to correct my code. I have created a custom widget it contains one drop down filed that is "Entity" and inside drop down values are fetching from custom table "x_nooy_gbs_o2i_entity". entity drop down filed having only two values 1)Nokia 2)fALU based on Entity selection I need to make dynamic filter In Server Script I'm calling "Data table widget(Cloned), I have implemented that with both values the filter is working fine but `by changing the entity value that time it is not working correctly. When I open first time If I choose Nokia as entity then it is showing correct filter but If I change it to fALU then it is not showing the widget here If I reload the page and then after If I choose fALU then for falu value also the filter is showing correctly. Can anyone help me out to achieve this requirement 

 

HTML: 

 

<div class="row">
<div class="col-md-12">
<span>
<h3 class="heading">
<p><b>Region</b></p>
</h3> </span>
</div>
<div class="clearfix">

</div>

<div class="col-md-4">
<label for="Entity">Entity:</label>
<select id="Entity" ng-model="c.myValue"
ng-options="opt for opt in c.data.choices"
class="form-control" ng-change="c.passoptions()">
<option value="">-- None --</option>
</select>
</div>
</div>

<br>

<!-- <div ng-if="c.showWidget">
<sp-widget ng-if="c.data.tableWidget" widget="c.data.tableWidget"></sp-widget>

</div> -->

<!--<div ng-hide="data.hideWidget"> -->
<div>
<sp-widget widget="data.tableWidget"></sp-widget>
</div>

 

Client Script:

 

api.controller = function(spUtil) {
/* widget controller */
var c = this;
c.passoptions = function() {

if (c.myValue != null) {
c.data.filterValue = c.myValue;
c.data.action = "filterChange";
c.server.update();
}

};
};

 

Server Script:

 

(function() {
/* populate the 'data' object */
/* e.g., data.table = $sp.getValue('table'); */


var arr = [];
var gr = new GlideRecord('x_nooy_gbs_o2i_entity');
gr.addEncodedQuery('entity_nameISNOTEMPTY');
gr.query();
while (gr.next()) {
var en = gr.getValue('entity_name');

arr.push(en);
}

data.choices = arr;
data.page = $sp.getParameter('id');
if (input && input.action == 'filterChange') {

var filterValue = input.filterValue;
var updateOptions = {};
updateOptions.table = 'x_nooy_gbs_o2i_region';

if (filterValue == 'fALU') {
updateOptions.filter = 'entity_code.entity_name=' + filterValue;
}
if (filterValue == 'Nokia') {
updateOptions.filter = 'entity_code.entity_name=' + filterValue;
}

updateOptions.fields = 'region_code,entity_code,region,active,sys_created_on,sys_created_by,sys_updated_on,sys_updated_by';

updateOptions.show_breadcrumbs = true;
updateOptions.show_new = true;


data.tableWidget = $sp.getWidget("o2i_region_data_table", updateOptions);

}
})();

 

srinivas48_2-1675576940936.png

 

srinivas48_1-1675576894694.png

 

srinivas48_0-1675576672093.png

I'm showing widget with Nokia and fALU filter after reloaded the page for each time but when I change to other entity option the filter is not working in onchange 

Kindly refer the above attachment for better understanding 

1 REPLY 1

Sonu Parab
Mega Sage

Hi @srinivas48 ,
Have a look this post https://www.servicenow.com/community/hrsd-forum/how-to-get-a-dynamic-filter-be-visible-in-drop-down-... 

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