hide a variable set on email notification
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-01-2024 07:36 AM
Hey,
I have 2 variable sets that should be visible only if the request is created in the order guide.
I created 2 UI policies that hide these fields otherwise but now I have a problem.
I have email notifications with approvals where these variable sets should also be hidden.
For start date and job title it works fine.
However I don't know why for name, surname and manager the fields are still visible in the email even though the UI policy is created the same.
What am I doing wrong?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-01-2024 08:02 AM
Hey,
The issue you're facing is because **UI Policies** only affect the user interface within ServiceNow (e.g., forms and catalog items) and don't influence **email notifications**. Since emails are generated server-side, the UI Policies that hide fields won't apply to the email content.
### Why it's happening:
- UI Policies work on forms but don't apply when emails are being sent.
- Email notifications use the data directly from the record without checking UI Policy conditions.
### How to fix it:
1. **Modify the Email Template**:
You can update your email template to check if the request is created from the **Order Guide** and only display the fields (like `name`, `surname`, and `manager`) when applicable. You can add conditional logic in your template to control this.
Example:
```html
<% if (current.request_from_order_guide == 'true') { %>
Name: ${name}
Surname: ${surname}
Manager: ${manager}
<% } %>
```
2. **Email Notification Conditions**:
Add conditions to your **Email Notification** so that it only includes these fields if the request is from the **Order Guide**. For example:
```javascript
current.request_from_order_guide == true
```
3. **Business Rule**:
If needed, you can create a **Business Rule** that clears or hides the `name`, `surname`, and `manager` fields in the email if the request isn’t from the **Order Guide**.
Example:
```javascript
(function executeRule(current, previous /*null when async*/) {
if (current.request_from_order_guide != 'true') {
current.name = '';
current.surname = '';
current.manager = '';
}
})(current, previous);
```
4. **Notification Variables Condition**:
Modify the **Notification Variables** to include conditions for hiding fields when they're not needed.
Let me know if you need help with any of the above solutions!
Best regards,
[Your Name]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-01-2024 08:18 AM
Check Email Template:
- Ensure that the email notification's template includes logic to hide or include fields based on the criteria you need.
- If you are using a standard email template, it will not automatically respect UI policies.
Use Email Script:
- You can use the Email Script section within the email notification to control visibility dynamically. Here’s an example of how to use a script to conditionally include fields based on the order guide:
javascript:
// Example Email Script
var isOrderGuide = current.variables.order_guide; // Change this to your condition
if (isOrderGuide) {
// Include the fields you want to show
email.body += '<b>Name:</b> ' + current.variables.name + '<br>';
email.body += '<b>Surname:</b> ' + current.variables.surname + '<br>';
email.body += '<b>Manager:</b> ' + current.variables.manager + '<br>';
} else {
// Optionally, include a message for when fields are hidden
email.body += 'The requested information is not available.';
}
Check Field Names:
- Double-check that you are referencing the correct variable names in your email template or script. Sometimes, variable names can differ slightly from what you expect.
Review UI Policy Conditions:
- Verify that the UI policies you created to hide the fields are correctly configured and are indeed firing based on the conditions set (e.g., check the conditions and scripts to ensure they function as intended).
Notification vs. Form Conditions:
- Ensure that the conditions under which the email notification is triggered are the same as those under which the UI policies apply. For example, if the email notification can be triggered in contexts where the UI policy is not applied, you may need additional checks in your email script.
Example of a Conditional Email Notification
You can create a more dynamic email notification that checks the context (like whether it’s an order guide) and decides which fields to display. Here’s a simplified approach:
Create a Notification: Go to System Notification > Email > Notifications and create or edit the notification.
In the "What it will contain" section, use something like:
<p><b>Request Details</b></p>
<p><b>Job Title:</b> ${job_title}</p>
<p><b>Start Date:</b> ${start_date}</p>
# Only show name, surname, and manager if it’s from an order guide
# Email script to control the visibility
${email.script}
- Use the Email Script:
if (current.variables.order_guide) {
email.body += '<p><b>Name:</b> ' + current.variables.name + '</p>';
email.body += '<p><b>Surname:</b> ' + current.variables.surname + '</p>';
email.body += '<p><b>Manager:</b> ' + current.variables.manager + '</p>';
}
✔️ If this solves your issue, please mark it as Correct.
✔️ If you found it helpful, please mark it as Helpful.
—
Shubham Jain
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-01-2024 09:39 AM
@New user1212 Can you please add the notification as well? As mentioned above by @Shubham_Jain you can create a email script and do the show/hide of the desired content.