Dynamically populating widget from Server Script

robinson_gregor
Kilo Contributor

Hi,

I am experiencing some odd behavior I am hoping someone can help me out with.

I have a widget called "Form Container Widget" that makes a decision in the Server Script which widget it is going to embed in the widget.

My HTML Template has the following code which is essentially displaying the embedded widget that gets set in the server script.

<div>
   <sp-widget widget="data.formWidget"></sp-widget>
</div>

My client script is listening for a change in the view and calling the server to update the view.

$rootScope.$on('viewChanged', function(event, data) {
   c.data.viewUpdated = true;
   c.data.currentView = data.viewId;
   c.server.update();	
});

My Server Script has the following code that is called when the $rootScope detects a change in view. This should essentially refresh the embedded widget in the HTML.

if(input && input.viewUpdated) {
    var widgetName = null;
    var gr = new GlideRecord('table_name');
    gr.addQuery('view', input.currentView);
    gr.query();
    if(gr.next()) {
       widgetName = gr.widget.id;
       data.formWidget = $sp.getWidget(widgetName);
    }
}

This is working fine on the initial load, but anytime the widgetName is updated and I am updating the data.formWidget field, it is not changing the embedded widget for some reason.

Any help is greatly appreciated!

4 REPLIES 4

Gurpreet07
Mega Sage

Seems like you are not providing the widget parameters... You could try to make a small change in server code.... 

data.formWidget = $sp.getWidget(widgetName,data);

Hi Gurpreet,

 

The widgets I am attempting to dynamically display do not accept any parameters for this test, so that is not the issue.

The dynamic display works fine for the first time the main widget loads, but when I am updating data.formWidget when the widget changes on the $rootScope, it isn't changing the widget as I expect.

Hi Robinson, May I know if the above is resolved? If so, can u pls share the solution!

othiagoveloso
Tera Contributor

Hi, have you tried resetting data.formWidget every time input.viewUpdated is called?

if (input && input.viewUpdated) {
    data.formWidget = null;
    var gr = new GlideRecord('table_name');
    gr.addQuery('view', input.currentView);
    gr.query();
    if (gr.next()) {
        data.formWidget = $sp.getWidget(gr.widget.id);
    }
}