Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Hide and Unhide button based on condition in Service Portal

vidhya_mouli
Tera Sage

When a user logs in and if the record exists in u_employee_sv table, then the  Open Record button must be visible.

If the record does not exist, then the Report Missing Record button must be visible.

 

Widget in the service portal:

Portal.png

HTML for the buttons:

  <button class="button-31" id="open-button" ng-click="c.openRecord(data.sys_id);">
  	Open Record
  </button>
  
  <br>
  
  <button class="button-31" id="report-button" ng-click="c.report();">
  	Report Missing Record
  </button>
  

 

Client Script:

api.controller=function() {
  /* widget controller */
  var c = this;
	
	c.openRecord = function(id){
		var url="?id=form&table=u_employee_sv&sys_id="+id;
		window.open(url,'_blank');
	}
	

};

 

Server Script:

(function() {
  /* populate the 'data' object */
  /* e.g., data.table = $sp.getValue('table'); */
	
	var employeeGR = new GlideRecord("u_employee_sv");
	employeeGR.addQuery('u_reference_1', gs.getUserID());
	employeeGR.query();
	
	//gs.addInfoMessage("ID: "+employeeGR.getRowCount());
	if(employeeGR.next()){
		data.empID = employeeGR.u_number.toString();
		data.name = employeeGR.u_reference_1.name.toString();
		data.email = employeeGR.u_reference_1.email.toString();
		data.department = employeeGR.u_department.name.toString();
		data.manager = employeeGR.u_reference_1.manager.name.toString();
		data.sys_id = employeeGR.getValue('sys_id');
		//data.description = employeeGR.u_employee_self_description.toString();
	} else {
		

	}

})();

 

How do I do this?

1 ACCEPTED SOLUTION

Sandeep Rajput
Tera Patron
Tera Patron

Here is the fix.

HTML for the buttons:

<div>
 <button class="button-31" id="open-button" ng-click="c.openRecord(data.sys_id);" ng-if="data.showOpen">
  	Open Record
  </button>
  
  <br>
  
  <button class="button-31" id="report-button" ng-click="c.report();"  ng-if="data.showMissing">
  	Report Missing Record
  </button>
</div>

Client Script: No change in client script

 

Server side script:

(function() {
  /* populate the 'data' object */
  /* e.g., data.table = $sp.getValue('table'); */
	
	var employeeGR = new GlideRecord("u_employee_sv");
	employeeGR.addQuery('u_reference_1', gs.getUserID());
	employeeGR.query();
	
	//gs.addInfoMessage("ID: "+employeeGR.getRowCount());
	if(employeeGR.next()){
		data.empID = employeeGR.u_number.toString();
		data.name = employeeGR.u_reference_1.name.toString();
		data.email = employeeGR.u_reference_1.email.toString();
		data.department = employeeGR.u_department.name.toString();
		data.manager = employeeGR.u_reference_1.manager.name.toString();
		data.sys_id = employeeGR.getValue('sys_id');
		//data.description = employeeGR.u_employee_self_description.toString();
                data.showOpen=true;
		data.showMissing = false;
	} else {
		
         data.showOpen=false;
	data.showMissing = true;
	}

})();

Hope this helps.

 

View solution in original post

3 REPLIES 3

Sandeep Rajput
Tera Patron
Tera Patron

Here is the fix.

HTML for the buttons:

<div>
 <button class="button-31" id="open-button" ng-click="c.openRecord(data.sys_id);" ng-if="data.showOpen">
  	Open Record
  </button>
  
  <br>
  
  <button class="button-31" id="report-button" ng-click="c.report();"  ng-if="data.showMissing">
  	Report Missing Record
  </button>
</div>

Client Script: No change in client script

 

Server side script:

(function() {
  /* populate the 'data' object */
  /* e.g., data.table = $sp.getValue('table'); */
	
	var employeeGR = new GlideRecord("u_employee_sv");
	employeeGR.addQuery('u_reference_1', gs.getUserID());
	employeeGR.query();
	
	//gs.addInfoMessage("ID: "+employeeGR.getRowCount());
	if(employeeGR.next()){
		data.empID = employeeGR.u_number.toString();
		data.name = employeeGR.u_reference_1.name.toString();
		data.email = employeeGR.u_reference_1.email.toString();
		data.department = employeeGR.u_department.name.toString();
		data.manager = employeeGR.u_reference_1.manager.name.toString();
		data.sys_id = employeeGR.getValue('sys_id');
		//data.description = employeeGR.u_employee_self_description.toString();
                data.showOpen=true;
		data.showMissing = false;
	} else {
		
         data.showOpen=false;
	data.showMissing = true;
	}

})();

Hope this helps.

 

Thank you. It worked.

Glad it helped.