Issue with Query Business rule condition
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2024 11:34 PM
Could anyone help me to fix the following before query BR.
I am having the following requirement.
if the user is from xyz group, the ticket contains a company field. If the company reg id is abc123. then the user was able to see the record else he don't need to see those record.
var newQuery;
var userId = gs.getUserID();
var myGroups = gs.getMyGroups();
if((gs.getUser().isMemberOf('1054f30f1ba2b91484aa85d2604bcba9') {
if(current.company.reg_id =="abc123"){ //current record company reg_id, the reg_id is in company table
newQuery = current.addQuery("company.reg_id", 'abc123');
}
else if(current.company.reg_id =="xyz123"){ //current record company reg_id, the reg_id is in company table
newQuery = current.addQuery("company.reg_id", 'xyz123');
}
else {
newQuery = current.addQuery("sys_id", '-1');
}
}
else {
newQuery = current.addQuery("sys_id", '-1');
}
I written the above before queryBr but the nested if condition and else if conditions are not working. can anyone help me on this.
thanks.
// I only need query BR not ACL
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2024 11:39 PM
- The first if condition is missing a closing parenthesis.
- You're using current without initializing it. You should use current only if you're writing a Business Rule on the table where the current record belongs to. If you're writing a Business Rule on a different table, you need to use new GlideRecord('table_name') to initialize a new GlideRecord object.
(function executeRule(current, previous /*null when async*/) {
var newQuery;
var userId = gs.getUserID();
var myGroups = gs.getMyGroups();
if (gs.getUser().isMemberOf('1054f30f1ba2b91484aa85d2604bcba9')) { // Added missing closing parenthesis
var companyGR = new GlideRecord('company');
companyGR.addQuery('reg_id', current.getValue('company')); // Assuming 'company' is the reference field to the company table
companyGR.query();
if (companyGR.next()) {
var regId = companyGR.getValue('reg_id');
if (regId == 'abc123' || regId == 'xyz123') {
newQuery = ''; // Do nothing, allow the query to proceed
} else {
newQuery = 'sys_id=-1'; // Exclude the record
}
} else {
newQuery = 'sys_id=-1'; // Exclude the record if company record is not found
}
} else {
newQuery = 'sys_id=-1'; // Exclude the record if user is not a member of the specified group
}
current.addEncodedQuery(newQuery);
})(current, previous);
Please Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2024 11:39 PM
Hi @Ak8977
There are some syntax level issues in this existing script.
(function executeRule(current, previous /*null when async*/) {
var newQuery;
var userId = gs.getUserID();
var myGroups = gs.getMyGroups();
if (gs.getUser().isMemberOf('1054f30f1ba2b91484aa85d2604bcba9')) {
if (current.getReference('company').reg_id == "abc123") {
newQuery = current.addQuery("company.reg_id", 'abc123');
} else if (current.getReference('company').reg_id == "xyz123") {
newQuery = current.addQuery("company.reg_id", 'xyz123');
} else {
newQuery = current.addQuery("sys_id", '-1');
}
} else {
newQuery = current.addQuery("sys_id", '-1');
}
})(current, previous);
Please check this once.
Regards,
Amit