Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

how to get past widget options in server script from the embedded widget in Service Portal

TouriaL
Tera Contributor

I have two widgets, I call one on the other. The problem is that I can not get back the options on the child widget’s script server but on the client script I get them well (see screenshot).
Can you help me to fix this problem ?

TouriaL_0-1673610880003.png

 

Parent widget:
server script: widget embedded in the server script
var instanceOptionsDecoded={
message: "test message",
colonne: "test colonne"
}
data.embeddedWidget = $sp.getWidget(widgetId,instanceOptionsDecoded);
html:
<sp-widget widget="data.embeddedWidget"></sp-widget>

child widget:
Customer Controller:
api.controller=function($scope, $window) {
/* widget controller */
var c = this;
c.parameters = {
action: "firstLoad"
};
c.server.get(c.parameters). then(function(response){
c.data = response.data;
c.isLoading = false;
});
server script:
(function() {
/* fill in the data object*/
/* e.g. data.table = $sp.getValue('table'); */
if (input && input.action == "firstLoad") {
data.message = options.message;
data.colonne = options.colonne;


})
html:
<div>
<h1>Options column: {{c.data.colonne}}</h1>
<h1>Option message: {{c.data.message}}</h1>

<pre>{{c.options | json}}</pre>
</div>

 

5 REPLIES 5

jaheerhattiwale
Mega Sage

@TouriaL You must add the below line before getting the values from the options

 

optCopy(['message','colonne']);
 
So the code should be
 

server script:
(function() {
/* fill in the data object*/
/* e.g. data.table = $sp.getValue('table'); */

 

optCopy(['message','colonne']);


if (input && input.action == "firstLoad") {
data.message = options.message;
data.colonne = options.colonne;


})

 

Please mark as correct answer if this solves your issue.

 
Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

I have this error:

TouriaL_0-1673625002446.png

 

@TouriaL Update the script as below

 

server script:
(function() {
/* fill in the data object*/
/* e.g. data.table = $sp.getValue('table'); */

 

optCopy(['message','colonne']);


if (input && input.action == "firstLoad") {
data.message = options.message;
data.colonne = options.colonne;

}

// copy to data from input or options
function optCopy(names) {
names.forEach(function(name) {
data[name] = input[name] || options[name];
})
}
})

 

Please mark as correct answer if this solves your issue.

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

@jaheerhattiwale thank you, but not working :

TouriaL_0-1673626448453.png

at the first script executions, I get back the options well,

TouriaL_1-1673627119188.png

but when it arrives on the line if (input && input.action == "firstLoad") { the options are retreaded and become: 

TouriaL_2-1673627209291.png