How to call Server script from HTML template of the widget without using Client controller?

Subhashree3
Giga Contributor

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.

find_real_file.png

I want the server script to be called on click of this button widget.

find_real_file.pngIn 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.

1 ACCEPTED SOLUTION

ChrisBurks
Mega Sage

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

find_real_file.png

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

find_real_file.png

 

View solution in original post

5 REPLIES 5

Nitesh Alashe1
Giga Expert

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

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.

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.

Tommy Jensen
Giga Guru

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.