- 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-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');
}
}
})();