How to set embedded widget options via clint script

Saroj Patel
Tera Expert

How can we set embedded widget options via client script

HTML Templates

<div class="form-group">
<p>
Embedded widget via Client Script
</p>
<sp-widget widget="c.clientScriptWidget"></sp-widget>
</div>

 

Client Script

function(spUtil){

spUtil.get('widget-cool-clock').then(function(response){
c.clientScriptWidget=response;
});
}

 

 

function(spUtil){ 

spUtil.get('widget-cool-clock',{c_color:"orange"}).then(function(response){  //not working
c.clientScriptWidget=response;
});

7 REPLIES 7

Tapish Sharma1
Kilo Sage

Hi, You can do it in server side script -

$sp.getWidget('widget_id', options);

 

Please mark helpful/correct

Yeah we can .... but is there any way to do with client script ?

 

The Clock Widget loads everything via options. Settings options is only possible via $sp.getWidget. You can use this fact to replace the spUtil.get with this:

Server Script:

(function() {

	if (input && input.widget && input.widgetOptions) {
		data.widget = $sp.getWidget(input.widget, input.widgetOptions);
		return;
	}
	
})();

Client controller:

api.controller=function(spUtil) {
  var c = this;
  c.server.get({
    widget: 'widget-cool-clock',
    widgetOptions: { c_color: "orange" }
  }).then(function(response) {
    c.clientScriptWidget = response.data.widget;
  });
};

 

Result:

find_real_file.png

 

Please let us know if you need further assistance, if not please mark the solving answer as the solution to close the thread.

by SN documentation it is possible to set options via spUtil :
https://docs.servicenow.com/bundle/tokyo-servicenow-platform/page/build/service-portal/concept/c_Nes...
https://developer.servicenow.com/dev.do#!/reference/api/tokyo/client/spUtilAPI#SPU-get_S

ex:

spUtil.get('pps-list-modal', {title: c.data.editAllocations, 
  table: 'resource_allocation', 
  queryString: 'GROUPBYuser^resource_plan=' + c.data.sysId, 
  view: 'resource_portal_allocations' }).then(function(response) {
    var formModal = response;
    c.allocationListModal = response;
  });  	

 

but it's not working  with wigdet-cool-clock, aperantly not all widgets ServiceNow treats equaly.

PS  thx for work around example.