How to add a if and else condition for Contains and doesn't contain a string in flow designer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2024 12:20 AM - edited 03-04-2024 12:23 AM
Hi All,
I have a encoded query on User table, which is to be validated in If-Else condition.
Below is the query to be searched inside User Table, please suggest if we have any option to do so.
To check if a user Department doesn't contain YYY, but contains XXX. if user's Department contains XXX & not contains YYY return assigment group as '9910a5771bbd7c509bae5530604bcb09'.
Else return assigment group as '8810a5771bbd7c509bae5530604bcb99'.
EncodedQuery('department.nameNOT LIKEThomson Reuters^department.nameLIKEReuters^active!=false');
Script :
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2024 12:27 AM
Here's a way to implement this in ServiceNow using GlideRecord. Your encoded query can be translated into separate queries using the addQuery() method. Here's a possible script:
javascript
Copy Code
var requestor = fd_data.trigger.current.u_requestor;
var user = new GlideRecord('sys_user');
user.addQuery('sys_id', requestor);
user.query();
if (user.next()) {
if (user.department.name.indexOf('YYY') === -1 && user.department.name.indexOf('XXX') !== -1 && user.active == true) {
return 'Assignment Group for XXX and not YYY';
} else {
return 'Other Assignment Group';
}
}
Please replace 'Assignment Group for XXX and not YYY' and 'Other Assignment Group' with the actual names of your assignment groups.
This script will first check if the user's department doesn't contain 'YYY' but does contain 'XXX', and if the user is active. If this condition is true, it will return the assignment group for users in department 'XXX' and not 'YYY'. Otherwise, it will return the other assignment group.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2024 12:29 AM
Here's a way to implement this in ServiceNow using GlideRecord. Your encoded query can be translated into separate queries using the addQuery() method. Here's a possible script:
javascript
Copy Code
var requestor = fd_data.trigger.current.u_requestor;
var user = new GlideRecord('sys_user');
user.addQuery('sys_id', requestor);
user.query();
if (user.next()) {
if (user.department.name.indexOf('YYY') === -1 && user.department.name.indexOf('XXX') !== -1 && user.active == true) {
return 'Assignment Group for XXX and not YYY';
} else {
return 'Other Assignment Group';
}
}
This script will first check if the user's department doesn't contain 'YYY' but does contain 'XXX', and if the user is active. If this condition is true, it will return the assignment group for users in department 'XXX' and not 'YYY'. Otherwise, it will return the other assignment group.
Please note that JavaScript's indexOf() function will return -1 if the substring is not found, and a non-negative integer (the index of the first occurrence of the substring) if the substring is found. Also, in JavaScript, the comparison == true is not necessary, you can use user.active.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2024 12:37 AM
i have tried to improve your code let me know if this helps.
// Get the requester's ID
var requesterID = fd_data.trigger.current.u_requestor;
// Use GlideRecord to search for the user in the User table
var userRecord = new GlideRecord('sys_user');
userRecord.addQuery('sys_id', requesterID);
// Apply a query to find users not in Thomson Reuters department and those in XXX department
userRecord.addQuery('NOT department.name', 'Thomson Reuters'); // Doesn't belong to Thomson Reuters
userRecord.addOrQuery('department.name', 'XXX'); // Belongs to XXX department
// Check if the user exists
if (userRecord.query()) {
// If user found, return the appropriate assignment group based on department
return userRecord.department.contains('XXX') ? '9910a5771bbd7c509bae5530604bcb09' : '8810a5771bbd7c509bae5530604bcb99';
} else {
// If user not found (you can handle this based on your needs)
gs.error('User with sys_id ' + requesterID + ' not found.');
return ''; // Replace with relevant action for when user is not found
}