Built something you're proud of? Tell the story. A quick G2 review of App Engine or Build Agent helps other developers see what's possible on ServiceNow. Share your experience.

How do i get an option from a parent widget into an embedded widget?

Not applicable

I have a widget that has an option, called "message".   In one place, I have embedded this widget in another widget. I have created an option called "message" on the parent widget.

How do i link the parent widget's option to the embedded widget?

The solution is this:

function(spUtil) { /* widget controller */  

  var c = this;  

 

 

  spUtil.get("custom-search-page", {message: c.options.message}).then(function(response) {  

  c.data.parentMessage = response.options.incident_message;  

  c.data.parentMessageFlag = 'true';  

  });  

 

 

}

I'm not positive i understand exactly what is happening, but it's behaving the way i want it to.

The widget im using is embedded sometimes, and not at other times. The widget is a button with a string of text above it. I set the value of that string with instance options. I wanted to be able to set that text even when it's embedded in this widget.

from what i understand, the SPutil gets the message i ask for from the widget i specify, and returns a promise. When the promise comes back, i set a data paramenter to equal the response field, and set a flag to true.

in my server script i check to see if the flag is true. if it is, I display the parent message. otherwise i use the one local to the widget.

1 ACCEPTED SOLUTION

abcadaret ctomasi,



You can use the following in your server script:



data.widget = $sp.getWidget("widget-id", options);



The second parameter is the object being passed in to the widget, so this should pass all available "options" on the first widget in to the embedded widget. I haven't tested this, but 99% certain it'll work.



And then all you have to do is display the widget in your HTML:



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


View solution in original post

12 REPLIES 12

abcadaret ctomasi,



You can use the following in your server script:



data.widget = $sp.getWidget("widget-id", options);



The second parameter is the object being passed in to the widget, so this should pass all available "options" on the first widget in to the embedded widget. I haven't tested this, but 99% certain it'll work.



And then all you have to do is display the widget in your HTML:



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


Not applicable

nathanfirth



I ended up doing this in the controller:


function(spUtil) { /* widget controller */


  var c = this;




  spUtil.get("custom-search-page", {message: c.options.message}).then(function(response) {


  c.data.parentMessage = response.options.incident_message;


  c.data.parentMessageFlag = 'true';


  });




}


Not applicable

just to be clear, This goes in the server script of the embedded widget, and the widget id is the name of the parent widget, correct?


Andrew,



The script I showed goes in the parent server script, it passes the options in to the embedded widget. The disadvantage of doing it in the controller is that it requires an AJAX call to the server as opposed to handling everything on the server.


Not applicable

OK, thanks for clarifying that. Just so im clear, in your example, this creates the "widget" object on the embedded widget? or do i have to create it in the embedded widget as well?