We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

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

suresh91
Kilo Sage

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

Hi Dutta,

 

Thanks for your response.

I did the change but still not getting value. I tried to add info message like below and it showing value in portal

gs.addInfoMessage("testing record value---------->"+data.u_customfield);
 

VishalB06557037
Giga Sage

Hi @suresh91 

 

Can you try declaring variable outside if condition like below : 

 

/*Declare variable here*/
var u_customfield;

var gr_notify = new GlideRecord('sys_user');
gr_notify.addQuery('sys_id', gs.getUserID());
gr_notify.query();
if(gr_notify.next())
{
/*initialize here*/
  u_customfield = gr_notify.u_customfield;
}
data.u_customfield = u_customfield;

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

Hi Vishal,

 

Thanks for your response.

I tried the above one but still not showing data value.