- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-07-2020 04:48 AM
Hi All,
Is there any way to call the Server script from HTML template of the widget? I am not doing any changes in the Client controller. PFB code.
I want the server script to be called on click of this button widget.
In the Server script, I am querying task table to update state of the current request. This update has to be done on click of this Button.
Please let me know your suggestions.
Thank You.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-08-2020 06:14 AM
Server script is always going to run first when loading. In order to prevent this any code needed to be run upon some trigger should be wrapped in a conditional statement checking the input object.
Then if you absolutely want to just do this in the HTML template then you could use c.server.get() passing an object and it will trigger your server script.
HTML template
<div>
<!-- your widget template -->
<button class="btn btn-primary" ng-click="c.server.get({'action':'doit'})">
Execute Now
</button>
</div>
Server script:
(function() {
/* populate the 'data' object */
/* e.g., data.table = $sp.getValue('table'); */
if(input.action == 'doit'){
if(input.action == 'doit'){
var inc = new GlideRecord('incident');
inc.setLimit(1);
inc.query();
if(inc.next())
$sp.log(inc.number)
}
}
})();
The caveat about doing it this exact way is that you'll lose out on using the promise and the ability to update your scope with that data.
So the better way would be to use your client controller and wrap that method in a function to use.
HTML template:
<div>
<!-- your widget template -->
<button class="btn btn-primary" ng-click="c.doIt()">
Execute Now
</button>
<h3>
New Data
</h3>
<span class="text-danger">{ { c.newdata } }</span>
</div>
Client Controller:
function() {
/* widget controller */
var c = this;
c.doIt = function(){
c.server.get({'action':'doit'})
.then(function(resp){
c.newdata = resp.data.newdata;
})
}
}
Server Script:
(function() {
/* populate the 'data' object */
/* e.g., data.table = $sp.getValue('table'); */
if(input.action == 'doit'){
var inc = new GlideRecord('incident');
inc.setLimit(1);
inc.query();
if(inc.next()){
$sp.log(inc.number)
data.newdata = inc.getDisplayValue('number');
}
}
})();

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-07-2020 04:53 AM
Hello,
Eg.
In Server Script data.result='True';
In HTML you can access using data.result in double braces
Mark Correct if it helps for you.
-BR
Nitesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-07-2020 04:57 AM
Hi Nitesh,
Could you please let me know how to access data object of Server script directly in the html template click operation?
Thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-07-2020 05:05 AM
Hi,
Could you please let me know how to call the data object in that HTML template? I want it to be called on click of the button. Thank you.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-07-2020 04:58 AM
The way you have setup your server script it will run as soon as the widget is loaded.
To run the query only when the button is clicked you have to trigger it via the client script which then calls the server and execute the script. In the server script put the code inside an if statement that checks if "input" is defined. If it is it means the server was called from the client script.