Getting data from internal widget and passing the option schema

Bhimashankar H
Mega Sage

Hi Community,

 

I have requirement to show the data from table in table format but only latest 3 records so I have copied the 'Data table from instance definition' and added the record_limit option in option schema, but this widget is calling a internal widget (data.dataTableWidget = $sp.getWidget('widget-data-table', options);), so I have copied the this internal widget as well, and this is working fine as expected without record limit. I wanted to add the setLimit before query, and that will be from option schema(record_limit), but unable to access options.record_limit in internal widget mean it is not returning any data.

 

Please suggest any solutions how I can achieve this.

 

POV: These widgets are available in PDI.

 

Thanks,

Bhimashankar

2 ACCEPTED SOLUTIONS

Ratnakar7
Mega Sage
Mega Sage

Hi Bhimashankar,

By default, the outer widget doesn’t pass custom options down to the internal widget unless you explicitly include them in the options object passed to $sp.getWidget().

In your outer widget’s server script, the line:

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

 passes the whole options object as-is — but you might need to ensure options.record_limit is actually defined and passed down, especially if it’s newly added in your custom schema.

Now, Validate that options.record_limit exists in the outer widget

In the outer widget, check that options.record_limit is actually defined. Add a gs.info() or assign it to data:

data.record_limit = options.record_limit || 3; // Default to 3 if not set


Then, Explicitly pass it to the internal widget

Update your $sp.getWidget() call like this:

data.dataTableWidget = $sp.getWidget('widget-data-table', {
  table: options.table,
  filter: options.filter,
  columns: options.columns,
  record_limit: data.record_limit // pass it explicitly
});


Now, you can access and use options.record_limit in the internal widget

In the server script of your cloned widget-data-table, you can do:

var gr = new GlideRecord(options.table);
if (options.filter)
    gr.addEncodedQuery(options.filter);
gr.orderByDesc('sys_created_on');
gr.setLimit(parseInt(options.record_limit) || 3);
gr.query();

data.rows = [];
while (gr.next()) {
  var row = {};
  // populate row data here
  data.rows.push(row);
}

 Optionally, you can dd Schema Entry for record_limit in Outer Widget

In your outer widget's Option Schema, define:

{
  "record_limit": {
    "type": "number",
    "default": 3,
    "description": "Number of records to display"
  }
}


Thanks,
Ratnakar

View solution in original post

Hi @Ratnakar7 ,

 

Thank you for reply. I didn't tried it after that, this solution as well, but seems it will work.

 

Once again thank you for detailed explanation.

 

Regards,

Bhimashankar

View solution in original post

2 REPLIES 2

Ratnakar7
Mega Sage
Mega Sage

Hi Bhimashankar,

By default, the outer widget doesn’t pass custom options down to the internal widget unless you explicitly include them in the options object passed to $sp.getWidget().

In your outer widget’s server script, the line:

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

 passes the whole options object as-is — but you might need to ensure options.record_limit is actually defined and passed down, especially if it’s newly added in your custom schema.

Now, Validate that options.record_limit exists in the outer widget

In the outer widget, check that options.record_limit is actually defined. Add a gs.info() or assign it to data:

data.record_limit = options.record_limit || 3; // Default to 3 if not set


Then, Explicitly pass it to the internal widget

Update your $sp.getWidget() call like this:

data.dataTableWidget = $sp.getWidget('widget-data-table', {
  table: options.table,
  filter: options.filter,
  columns: options.columns,
  record_limit: data.record_limit // pass it explicitly
});


Now, you can access and use options.record_limit in the internal widget

In the server script of your cloned widget-data-table, you can do:

var gr = new GlideRecord(options.table);
if (options.filter)
    gr.addEncodedQuery(options.filter);
gr.orderByDesc('sys_created_on');
gr.setLimit(parseInt(options.record_limit) || 3);
gr.query();

data.rows = [];
while (gr.next()) {
  var row = {};
  // populate row data here
  data.rows.push(row);
}

 Optionally, you can dd Schema Entry for record_limit in Outer Widget

In your outer widget's Option Schema, define:

{
  "record_limit": {
    "type": "number",
    "default": 3,
    "description": "Number of records to display"
  }
}


Thanks,
Ratnakar

Hi @Ratnakar7 ,

 

Thank you for reply. I didn't tried it after that, this solution as well, but seems it will work.

 

Once again thank you for detailed explanation.

 

Regards,

Bhimashankar