Using sysparm_fixed_query as a filter in "Data table from Instance Definition" widget

gratziela
Tera Contributor

Hi,

I want to add in a widget something like "sysparm_fixed_query" used in the backend of ServiceNow.

I use a copy of "Data Table From Instance Definition" widget and add some parameters for the other widget that is called:

data.table_label = gr.getLabel();
var widgetParams = {
table: "sn_customerservice_case",
fields: data.field_list,
o: "state",
d: data.d,
filter: "account=" + gr2.getValue("account"),
window_size: data.window_size,
view: 'sp',
title: options.title,
show_breadcrumbs: true,
enable_filter: true
};
data.dataTableWidget = $sp.getWidget('widget-data-table', widgetParams);

Is there a way to add some parameter that will apply my fixed query and it couldn't be removed? Or what should I change?

 

NOTE: I don't want to hide the filter/breadcrumbs, I just want to fix part of the filter and the user to be able to add more conditions, but not to remove mine. So in this case... show_breadcrumbs to false or enable_filter to false won't work.

 

Thanks in advance!

6 REPLIES 6

Yevgeny Pletnev
Kilo Contributor

Hey guys,

I didn't have much time to get myself deep in with the documentation, but from the first look it appears that ServiceNow is pretty much strict about limitations that we experience. You can see $sp.getFilterBreadcrumbs() is being called in the actual Filter Breadcrumbs sp widget, and whenever I come near these $sp functions I know in advance that any research will return no result, leaving me among the other people complaining on how ServiceNow won't shed some light on how to actually work with these functions. 

However, it seems like we're still not so restrained at this point. I'm trying to implement a similar customization the OP seeks to have and just wanted to share the progress I was able to achieve this far:

 

In my copy of data-table-widget widget implementation, I've made a few changes to the Server script:

First, I've put this line to have the initial query of the instance stored: 

 find_real_file.png

Then I perform a check to see if there are any keywords in my query (keywords are added by the search bar in widget's header), and in case I have them, I drop off the preceding conditions and call the breadcumbs widget by supplying it with keywords conditions only.

find_real_file.png

 

Now we can use the searchbar to apply keyword filters to our query! 

find_real_file.png

Everything goes terribly wrong if we click any of it though. By the way, If no keywords are specified, it shows only the first crumb, which is 'All'. By default users get to see all the records with no filters if they click it, but bear with me, we're not just quite there yet.

 

Now, upon further investigation of the widget-filter-breadcrumbs widget we witness that all the interesting logic is covered from us under the hood of that $sp.getFilterBreadcrumbs() method call which apparently does all the magic. However, it was worth openning this code, because it had revealed the way it actually manipulates the data-table-widget widget by raising 'widget-filter-breadcrumbs.queryModified' event and passing the new filter it has casted there. Now we find this event's listener in the Client script of our data-table-widget and deny all the magic outside Hogwarts by reseting the query to filter value we stored at the beggining:

find_real_file.png

And now, well, I guess, hooray! At least keyword filtration is enabled on our widget!

I do sincerely hope that this method I've described is the most psychotic way to reinvent the wheel there even possible, and I do look up for any comments or suggestions, but for now I have enabled at least one feature this topic relates to in my implementation.

Btw, please note that line numbers I have on the screenshots may not match yours since I've added some change to the original code. And my system is on Jakarta.

danr2c2
Tera Expert

This can be done by using option.fixed_query in the server script or adding it into the option schema of the widget instance.  I chose the option schema myself to ensure reusability of the widget.

{"hint":"Encoded query string","name":"fixed_query","section":"Data","label":"Fixed query","type":"string"}