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

Chuck Tomasi
Tera Patron

Hi Andrew,



I'm no AngularJS expert, but have you tried using a $rootScope property?



You might also be able to use $on and $broadcast to communicate between the two widgets. I did something like that here:



Live Coding Happy Hour Recap 2017-03-03: Inter widget communications


Not applicable

There's a way to use spUtil to do this, but I don't know what it is. That's what i'm looking for.


Perhaps nathanfirth knows the spUtil method...


Not applicable

You know, I was going to tag him, but I do so often...