The CreatorCon Call for Content is officially open! Get started here.

Passing Variables from Server script to Client controller in a widget

meghanareddy206
Tera Contributor

I've created a custom widget and the purpose of it is to execute a rest api call from server side code and get the access token(server side) and pass it to client controller script to perform SSO with a third party vendor. I'm able to execute both scripts separately and got logs for it. All I need is to pass the token and user's email through variables from server side to client controller script, I've created the data object in server side script and using $scope or c.variable to retrive the data, but I'm not getting the data back to client script.

 

Here is an example code:

server side:

(function() {

data.name = [];
// get current user email
var uemail;
var usr = new GlideRecord('sys_user');
usr.addQuery('sys_id', gs.getUserID());
usr.query();
if(usr.next())
{
uemail = usr.email;
}
data.name.push({email: uemail});
gs.log("server side user>>>" +uemail + "and " + data.name, "Test code");

})();

 

client code:

function($scope) {
var c = this;
var temp = c.data.email;
alert("client code>>>" +temp); // not working

}

Do I need to define anything in the HTML template? I've included <input type="text" ng-model="c.data.name"/> in HTML too, but wont work. Any help is appreciated.

1 ACCEPTED SOLUTION

Did you try this code?

 

 

server side:

(function() {

data.name = '';

data.email = '';

data.code = '';

// get current user email
var uemail; 
var usr = new GlideRecord('sys_user');
usr.addQuery('sys_id', gs.getUserID());
usr.query();
if(usr.next())
{
data.email = usr.email;

data.code = 'your code';
}
//data.name.push({email: uemail});
//gs.log("server side user>>>" +uemail + "and " + data.name, "Test code");

})();

 

client code:

function($scope) {
var c = this;
var temp = c.data.email;
alert("client code>>>" +temp); // not working

alert("client code>>>" +c.data.code); // not working

}


Please mark this response as correct or helpful if it assisted you with your question.

View solution in original post

14 REPLIES 14

I'm able to achieve this by using .toString() for email, otherwise it was sending email as an object, I appreciate your help.

Can you change this line data.email = usr.email; to data.email = usr.getValue('email');


Please mark this response as correct or helpful if it assisted you with your question.

I was able to achieve this using .toString() for the email object, else it is sending the data as glide object, I appreciate your help with this.

Great. Can you mark this thread answered so that it will be useful for others


Please mark this response as correct or helpful if it assisted you with your question.

Hi Sanjiv,

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.count" 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 Sanjiv!

 

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

var count = 0;

if(gaIncCount.next()){

count = gaIncCount.getAggregate('COUNT');
console.log('row count is '+count);//shows correctly

data.count = gaIncCount.getAggregate('COUNT'); //this is what I need to send to client
console.log('data.count is '+data.count);//shows correctly
}

}

 

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 = c.data.count; //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 = '';
}) }
}