Current value not returning in Business Rule

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-20-2024 03:19 AM
Please could someone take a look over my script that ive written and help me out?
(function executeRule(current, previous /*null when async*/ ) {
var user = gs.getUserID();
var contact = current.contact;
gs.info('MIIA User ID: ' + user);
gs.info('MIIA Current Contact: ' + contact);
if (contact == user) {
gs.info('MIIA User is the contact.');
return;
} else {
var grUser = new GlideRecord('customer_contact');
if (grUser.get(user)) {
var userHelpCentreProfile = grUser.getValue('u_help_centre_profile');
gs.info('MIIA User Help Centre Profile: ' + userHelpCentreProfile);
if (userHelpCentreProfile == '3') {
current.addQuery('sys_class_name', '!=', 'sn_customerservice_case');
gs.info('MIIA Query added to hide the record.');
}
}
}
})(current, previous);
I am trying to allow a user to view the tickets that they raise and are the contact, but then if not the contact, then to exclude a certain class.
The log returns no value when i log in as the user and create the ticket:
MIIA Current Contact: |
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-20-2024 03:24 AM
Good job adding the logs. What table is this running on, and what are the When to run settings?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-20-2024 03:49 AM
Thanks @Brad Bowman
This runs on the table sn_customerservice_case.
The user is completing a record producer in the service portal but is unable to view it even though they have u_help_centre_profile set to 3.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-20-2024 05:03 AM
Ah - so this is a before Query Business Rule. Those do not have access to a 'current' record as the list is being built from the table, so 'current' can only be used to affect the query. This script, running on the sn_customerservice_case table will only show the case records in a list view that the current user is the contact and u_help_centre_profile = 3.
(function executeRule(current, previous /*null when async*/ ) {
var caseArr = [];
var user = gs.getUserID();
gs.info('MIIA User ID: ' + user);
var caseGr = new GlideRecord('sn_customerservice_case');
caseGr.query();
while (caseGr.next()) {
if (caseGr.contact == user) {
gs.info('MIIA User is the contact.');
var grUser = new GlideRecord('customer_contact');
if (grUser.get(user)) {
var userHelpCentreProfile = grUser.getValue('u_help_centre_profile');
gs.info('MIIA User Help Centre Profile: ' + userHelpCentreProfile);
if (userHelpCentreProfile == '3') {
caseArr.push(caseGr.sys_id.toString()); //user is contact and profile, so show this record
gs.info('MIIA case added to list.' + caseGr.number);
}
}
}
}
current.addQuery('sys_id', 'IN', caseArr.join(','));
})(current, previous);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-20-2024 06:33 AM
@Brad Bowman Thanks and yes, im starting to understand that now.
I should have also been a bit clearer on the overall process I have in place.
So a user of the portal has a profile of either 1, 2 or 3. They can also be a standard user or an admin user. So a standard user can only view their own cases and an admin can view all cases for their company regardless of who logged the case(standard vs admin is controlled via ACL so not in scope for this piece).
Profile 1 is for access to view both direct and shared case classes.
Profile 2 is for access to view only the case class direct.
Profile 3 is for access to view only the case class of shared.
However, one slight caveat which I am trying to achieve is that someone who has a Profile of 3, can view all of the shared cases, but can log a "direct" case in the odd circumstance. So needs to continue to view all of the shared cases, but also the direct cases that the person has created.
So initially, I had 2 BRs in play:
(function executeRule(current, previous /*null when async*/ ) {
var user = gs.getUserID();
var grUser = new GlideRecord('customer_contact');
if (grUser.get(user)) {
var userHelpCentreProfile = grUser.getValue('u_help_centre_profile');
if (userHelpCentreProfile == '2') {
// Add the query to the current object
current.addQuery('sys_class_name', '!=', 'sn_customerservice_direct_service_case');
}
}
})(current, previous);
(function executeRule(current, previous /*null when async*/ ) {
var user = gs.getUserID();
var grUser = new GlideRecord('customer_contact');
if (grUser.get(user)) {
var userHelpCentreProfile = grUser.getValue('u_help_centre_profile');
if (userHelpCentreProfile == '3') {
// Add the query to the current object
current.addQuery('sys_class_name', '!=', 'sn_customerservice_case');
}
}
})(current, previous);
But then I realised that someone with the profile 3, would also need to log the occasional direct case. So as an admin user. would want to continue to view all shared cases, but only the direct cases that they had logged on the portal.
Your above script worked really well, but now affects the users who have a profile of either 1 or 2