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

Can you also set the data.count to 0 in Server Script

 

data.count = 0;
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
 }


}

 


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

Hi Sanjiv,

thanks...Ok so I added that, still getting this response in the addInfoMessage in the client.  Is my client script correct?

find_real_file.png

Hi again Sanjiv,

I figured it out, I needed to move the data.count to be inside the "c.server.update()" function...got it working.  Here's my final 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;
}
c.server.update().then(function(response){
var queueNumber = c.data.count; //needed to move this here

spUtil.addInfoMessage('Thank you, a ticket has been created on your behalf. You are number '+queueNumber+'. You will now be redirected to the Tech Hub home screen.');
setTimeout(myFunction, 3000);
function myFunction(){
$window.location.href = 'https://dev54219.service-now.com/th_proto?id=techhub_index';
}
c.data = {};
$scope.c.affected_user.value = '';

}) }
}

thanks again as always!

Maybe one more question in case you've got time...any idea how I can get rid of those errors in the console.log?  the script is working, but I'd like it to work without the errors.  Any thoughts?  thanks!