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

Email notification at the click of a button in Service Portal

vidhya_mouli
Giga Sage

When an employee clicks on the button, it should send a notification to all users from the "Employee Administration"  (who has the role "emp_admin") group with Subject: "User missing Employment record: <user ID>" and Body:"Create the missing Employment record by pressing the <LINK>". The <LINK> should be configured so that clicking it should automatically open a form with the creation of new "u_employee_sv" record and the user information automatically prefilled in that form. 

 

Widget:

Portal.png

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');
	}
	

c.reportMissing = function(id){
  // Get all users with the emp_admin role
  var empAdmins = new GlideRecord('sys_user_has_role');
  empAdmins.addQuery('role.name', 'emp_admin');
  empAdmins.query();
  
  while(empAdmins.next()) {
    //Send email notification to each emp_admin user
    var email = new GlideRecord("sys_email");
    email.initialize();
    email.mailbox.setDisplayValue("Outbox");
    email.subject('User missing Employment record: ' + id);
    email.body('Create the missing Employment record by pressing the <a href="' + gs.getProperty('glide.servlet.uri') + 'sp?id=form&table=u_employee_sv&sys_id=-1&sysparm_query=user=' + id + '">LINK</a>.');
    email.recipients = empAdmins.user.email.getDisplayValue(); // Set email recipients to the emp_admin user's email
    email.type = "send-ready";
    email.content_type = "multipart/mixed";
    email.insert(); 
  }
  alert("Notification has been sent!!!" + id);
}


	
};

 

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());
	data.userID = 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.showOpen = true;
		data.showMissing = false;
	} else {
		data.showOpen = false;
		data.showMissing = true;

	}

})();

HTML:

<div>
  <h1> My Employment Record</h1>
  
  <table>
    <tr>
    	<th>Employee ID</th>
      <td>{{data.empID}}</td>
    </tr>
    
    <tr>
    	<th>Name</th>
      <td>{{data.name}}</td>
    </tr>
    
    <tr>
      <th>Email</th>
      <td>{{data.email}}</td>
    </tr>
    
    <tr>
      <th>Department</th>
      <td>{{data.department}}</td>
    </tr>
    
    <tr>  
      <th>Manager</th>
      <td>{{data.manager}}</td>
    </tr>
    
  </table>
  <br><br>
  
  <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.reportMissing(data.userID);" ng-if="data.showMissing">
  	Report Missing Record
  </button>
</div>

 

The email is not getting triggered. How do I modify my code?

 

1 ACCEPTED SOLUTION

Hello Vidhya,

 

You don't have to write GlideRecord in Client Controller.

Refer the below link to understand communicating between client controller and server side -

https://serviceportal.io/communicating-between-the-client-script-and-the-server-script-of-a-widget/

 

View solution in original post

3 REPLIES 3

nayanawadhiya1
Kilo Sage

Hello Vidhya,

 

Your Email creation code syntax is wrong. we don't have subject and body as function so we have change it like below or you can use setValue function to set a values on the fields - 

var email = new GlideRecord("sys_email");
    email.initialize();
    email.mailbox.setDisplayValue("Outbox");
    email.subject = 'User missing Employment record: ' + id;
    email.body = 'Create the missing Employment record by pressing the <a href="' + gs.getProperty('glide.servlet.uri') + 'sp?id=form&table=u_employee_sv&sys_id=-1&sysparm_query=user=' + id + '">LINK</a>.';
    email.recipients = empAdmins.user.email.getDisplayValue(); // Set email recipients to the emp_admin user's email
    email.type = "send-ready";
    email.content_type = "multipart/mixed";
    email.insert();

 

Thank you for your response. However, it is still not working. When I click on the button nothing happens.

Hello Vidhya,

 

You don't have to write GlideRecord in Client Controller.

Refer the below link to understand communicating between client controller and server side -

https://serviceportal.io/communicating-between-the-client-script-and-the-server-script-of-a-widget/