๐ Scenario-Based Question
ย
๐ Custom Greeting & Task Display for Logged-in User on Portal
We want to greet the currently logged-in user by their first name and show the latest task they have opened. To achieve this:
1. Server-side Script
(function() { var usr = gs.getUserID(); var userGR = new GlideRecord('sys_user'); if (userGR.get(usr)) { data.fname = userGR.getValue('first_name'); var taskGR = new GlideRecord('task'); taskGR.addEncodedQuery('opened_by=' + userGR.getValue('sys_id')); taskGR.orderByDesc('sys_created_on'); taskGR.query(); if (taskGR.next()) { data.task = taskGR.getValue('number'); } } })();
2. Client-side HTML Template
<div> <h1>Hello {{data.fname}}</h1> <p>TASK opened by you ๐</p> <p>{{data.task}}</p> </div>
Steps to implement:
- Open your widget in the Widget Editor.
- Paste the server-side code into the Server Script section.
- Use the HTML template code in the HTML Template section.
- Save and test on your custom Service Portal.
๐ Now your portal visitors will see a friendly greeting along with the most recent task they opened!