Pass data from Server Side to Client Side to HTML in ServicePortal Widget

suresh91
Tera Guru

Hi Everyone,

I have an requirement that, I need to send the data from server side to HTML check box. Based on server side data, i need to show the checkbox field either true or false. I have tried with using below data and unable to fetch and show the data in service portal. Please check below code and correct me.

 

HTML Code:

<div >
<label><input type="checkbox" ng-model="chkbx" ng-checked="data.u_customfield" ng-click="checkNotify(chkbx)" ng-checked="false"> testing</label>
</div>

 

Server side:

 

var gr_notify = new GlideRecord('sys_user');
gr_notify.addQuery('sys_id', gs.getUserID());
gr_notify.query();
if(gr_notify.next())
{
var u_customfield = gr_notify.u_customfield;
}
data.u_customfield = u_customfield;
 
Could you please help me, how to show check box field either true or false based n response from server side?
 
Thank you.
1 ACCEPTED SOLUTION

Hi @suresh91 

 

This should work:

 

HTML:

<div >
<label><input type="checkbox" ng-model="chkbx" ng-checked="data.u_customfield" ng-click="c.checkNotify(chkbx)"> testing</label>
</div>

 

Client:

api.controller=function($scope) {
	/* widget controller */
	var c = this;


	c.checkNotify = function(val) {


		c.data.action = "getField";
		c.data.value =  val;


		alert("test Before " + val);

		c.server.update().then(function(response){
			alert("test After " + response.u_customfield);

		})
	}
}

Server:

(function() {

	if (input){
		if (input.action == "getField"){
			var gr_notify = new GlideRecord('sys_user');
			gr_notify.addQuery('sys_id', gs.getUserID());
			gr_notify.query();
			if(gr_notify.next())
			{
				gs.addInfoMessage(gr_notify.getDisplayValue())
				data.u_customfield = gr_notify.u_customfield;
				return data;
			}
		}
	}

})();

Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

View solution in original post

7 REPLIES 7

Peter Bodelier
Giga Sage

Hi @suresh91 

 

Please add your client script as well.


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

Hi Peter,

 

Thanks for your response. Please find below client script

$scope.checkNotify = function(val) {
        alert("test" + val);
        $scope.data.u_customfield = val;
        $scope.server.update().then(function() {
            $scope.data.u_customfield = val;
        });
But i have used the above code for update data in table. Do i need to change or add anything?

    

Hi @suresh91 

 

This should work:

 

HTML:

<div >
<label><input type="checkbox" ng-model="chkbx" ng-checked="data.u_customfield" ng-click="c.checkNotify(chkbx)"> testing</label>
</div>

 

Client:

api.controller=function($scope) {
	/* widget controller */
	var c = this;


	c.checkNotify = function(val) {


		c.data.action = "getField";
		c.data.value =  val;


		alert("test Before " + val);

		c.server.update().then(function(response){
			alert("test After " + response.u_customfield);

		})
	}
}

Server:

(function() {

	if (input){
		if (input.action == "getField"){
			var gr_notify = new GlideRecord('sys_user');
			gr_notify.addQuery('sys_id', gs.getUserID());
			gr_notify.query();
			if(gr_notify.next())
			{
				gs.addInfoMessage(gr_notify.getDisplayValue())
				data.u_customfield = gr_notify.u_customfield;
				return data;
			}
		}
	}

})();

Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

Utpal Dutta
Tera Guru

Hi Suresh,

I see that you are using ng-checked 2 times. One with server value and another that you're making it false. Please remove the other one which is making in false and see if it is working.

 

Please mark my answer correct if it does.

 

Thanks,

Utpal