Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Pass a variable value from server script to client script in a widget

vignesh24
Kilo Contributor

Hi All

I wanted a widget which is a button to redirect to different catalog items, I dont want to hard code url using dev12345, rather i want it to be fetched from gs.getProperty('instance_name') which am able to get. But I am not able to pass it to the client script to use action.redirect(). How can this be achieved?

1 ACCEPTED SOLUTION

Gaurav Bajaj
Mega Sage

Hi,

 

If you have certain variable value in server script, you can just pass it into data object of widget server script.

i.e. data.myVariable = gs.getProperty('instance_name');

 

Now in client script,

Just use, $scope.data.myVariable to fetch the value set in server script part.

 

Please let me know if you are looking for something else.

 

Thanks

Gaurav

View solution in original post

3 REPLIES 3

Gaurav Bajaj
Mega Sage

Hi,

 

If you have certain variable value in server script, you can just pass it into data object of widget server script.

i.e. data.myVariable = gs.getProperty('instance_name');

 

Now in client script,

Just use, $scope.data.myVariable to fetch the value set in server script part.

 

Please let me know if you are looking for something else.

 

Thanks

Gaurav

Thanks Gaurav 🙂

Hi Gaurav,

seeing your post and looking to do exactly this...however my script isn't working...can you take a look?  I'm trying to pass the "data.queueNumber" value from the server to the client so I can use it in a "addInfoMessage".  The info message keeps showing "undefinded"...can you see what I'm doing wrong?  thanks

 

SERVER SCRIPT:

var gaIncCount = new GlideAggregate('incident');
gaIncCount.addQuery('active','true');
gaIncCount.addQuery('assignment_group','f57bbd87ebff23008f833c4fea887e46');
gaIncCount.addQuery('contact_type','walk-in');
gaIncCount.addAggregate('COUNT');
gaIncCount.query();

if(gaIncCount.next()){

var queueNumber;
data.queueNumber = gaIncCount.getAggregate('COUNT'); //this is what I want to pass to the client

}

 

CLIENT SCRIPT

 

function($scope, spUtil, $window) {

var c = this;
c.data.user = {value:'',displayValue:''};

c.click = function(){
c.data.user.displayValue='';
c.data.user.value='';
c.data.short_description = '';

}


c.addItem = function(){
var user = c.data.user.value;
var desc = c.data.short_description;

if(user == '' || user == 'undefined' || user == 'null' || desc == 'null' || desc == '' || desc == 'undefined'){
alert('Please enter your name and a description of your issue before submitting.');
return;
}
var queueNumber = $scope.data.queueNumber; //this is where I need to put the value from the Server Script
spUtil.addInfoMessage('Thank you, a ticket has been created on your behalf. You are number '+queueNumber+' in the queue. You will now be redirected to the Tech Hub home screen.');

c.server.update().then(function(response){
c.data = {};
$scope.c.affected_user.value = '';
}) }
}