In this blog post, I will share a code snippet to check if the logged-in user has a specific web role and explain how to conditionally implement logic based on the user’s role.
I am using the out-of-the-box Profile web page for this example. As shown in the screenshot below, I’ve placed the code snippet under the Copy (HTML) section of the web page.

Here’s what the code snippet does upon the page load:
- Checks the logged-in portal user’s web roles using user.roles Liquid tag:
- If the user has the “Sales Person” web role, assigns
'Sales Person'to theuserRolevariable. - If the user has the “Administrators” web role, assigns
'Administrators'to theuserRolevariable.
- If the user has the “Sales Person” web role, assigns
- Passes the assigned
userRolevalue into a JavaScript variableloggedinUser. Uses JavaScript to execute logic based on the user’s web role.
Here is the code snippet:
{% if user.roles contains 'Sales Person' %}
{% assign userRole = 'Sales Person' %}
{% elsif user.roles contains 'Administrators' %}
{% assign userRole = 'Administrators' %}
<script>
var loggedinUser;
$(document).ready(function() {
loggedinUser = '{{userRole}}';
if (loggedinUser == 'Administrators') {
// Perform administrator-specific actions
} else if (loggedinUser == 'Sales Person') {
// Perform salesperson-specific actions
}
});
</script>
The key takeaway from the above code snippet is to use the user.roles Liquid tag to retrieve the web roles of the logged-in user.
🙂

![[Step by Step] Beginner : Create a PCF control and add it to a custom page](https://rajeevpentyala.com/wp-content/uploads/2024/12/image-49.png)

Leave a comment