Recordwatch question..

PeteM1
Tera Contributor

Hello all.

 

I cloned a copy of the datatable from instance definition widget and added the following code for a record watcher:

 

spUtil.recordWatch($scope, "x_325383_modules_exm_module_table", "", function(name) {
console.info('this is a test');
spUtil.update($scope);
});

 

Other than that bit of code, the widget was unchanged. When I add a new record to the table then go back to view the results, all records are removed (the console.info is printed to the console). It's only when I do a browser refresh do I see them again. Is there another modification I need to make to the widget in order for the the records to update and remain?

 

Any thoughts would be appreciated.

1 ACCEPTED SOLUTION

@PeteM1 Tried and tested 100% working solution.

 

Replace the recordwatch function as below:

spUtil.recordWatch($scope, "x_325383_modules_exm_module_table", "", function(name) {
spUtil.get("widget-data-table", $scope.data.optionValues).then(function(response) {
$scope.data.dataTableWidget = response;
});
});

 

And add the below line in Server Script above "data.dataTableWidget" line as shown in image below:

data.optionValues = options;

 

jaheerhattiwale_0-1672628904128.png

 

 

Please mark as correct answer if this solves your issue.

 

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

View solution in original post

8 REPLIES 8

jaheerhattiwale
Mega Sage
Mega Sage

@PeteM1 send the server side code as well. It depends on that.

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

Hello @jaheerhattiwale and thank you for the quick response. The server code is unchanged from the OOB widget. Below is the code I am currently using.

 

Thanks again. 

(function(){
/* "use strict"; - linter issues */
// populate the 'data' object
var sp_page = $sp.getValue('sp_page');
var pageGR = new GlideRecord('sp_page');
pageGR.get(sp_page);
data.page_id = pageGR.getValue("id");
$sp.getValues(data);
if (data.field_list) {
data.fields_array = data.field_list.split(',');
} else {
data.field_list = $sp.getListColumns(data.table);
}

if (input) {
data.p = input.p;
data.o = input.o;
data.d = input.d;
data.q = input.q;
}
data.p = data.p || 1;
data.o = data.o || $sp.getValue('order_by');
data.d = data.d || $sp.getValue('order_direction');

data.page_index = (data.p - 1);
data.window_size = $sp.getValue('maximum_entries') || 10;
data.window_start = (data.page_index * data.window_size);
data.window_end = (((data.page_index + 1) * data.window_size));
data.filter = $sp.getValue("filter");

var gr = new GlideRecordSecure(data.table);
if (!gr.isValid()) {
data.invalid_table = true;
data.table_label = data.table;
return;
}
data.table_label = gr.getLabel();

options.table = data.table;
options.fields = data.field_list;
options.o=data.o;
options.d= data.d;
options.filter=data.filter;
options.window_size=data.window_size;
options.view = data.view;
options.useInstanceTitle = true; // to make sure Data Table widget uses headerTitle always
options.headerTitle = options.title;
options.show_breadcrumbs=true;

data.dataTableWidget = $sp.getWidget('widget-data-table', options);
})();

@PeteM1 let me check this on my PDI and come back here 

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

Hey @jaheerhattiwale - again...thank you. 

 

Just wanted to add the steps I took…

1) cloned the widget mentioned above.

2) added the cloned widget to a new page I created

3) specified the table, fields, etc. 

4) tested (and this is where the records disappeared)