how to get past widget options in server script from the embedded widget in Service Portal
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2023 03:58 AM
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 ?
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>
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2023 04:16 AM
@TouriaL You must add the below line before getting the values from the options
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.
ServiceNow Community Rising Star, Class of 2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2023 07:50 AM
I have this error:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2023 07:56 AM - edited 01-13-2023 07:58 AM
@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.
ServiceNow Community Rising Star, Class of 2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2023 08:27 AM
@jaheerhattiwale thank you, but not working :
at the first script executions, I get back the options well,
but when it arrives on the line if (input && input.action == "firstLoad") { the options are retreaded and become: