- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2025 01:42 AM
There is an issue when query before BR runs, the encoded query works fine when checked as admin but for a custom roleA for which it should run, only returns few record compare to checking the encoded query with admin role.
I have checked ACL, there is no such issue, and also there is no security constraints message for that roleA. not sure what is missing here
i have checked logs, the encoded query which is returned looks fine but not sure what is happening. the difference in number of records show in admin to role is A huge. from 2000 - 18000 records
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2025 02:08 AM
your closing bracket for while should not be at the end
Did you try with hard-coded query in query BR?
things to check
1) field level READ ACL
2) domain separation
3) extra comma in encodedQuery
4) test the queries using list view
5) remove any empty strings from your arrays
try this
// Get the current user's sys_id
var userid = gs.getUserID();
// Prepare arrays to store company and segment values
var companyList = [];
var segmentlist = [];
// Query the custom table for relevant records
var companyListGr = new GlideRecord('x_custom_table');
companyListGr.addEncodedQuery("active!=false^ORactive=NULL^requested_for=" + userid);
companyListGr.query();
while (companyListGr.next()) {
// Collect company sys_ids (assuming comma-separated)
var companyArray = companyListGr.company_sys_id.toString().split(',');
companyList = companyList.concat(companyArray);
// Collect segment values (assuming comma-separated display values)
var segmentValue = companyListGr.getDisplayValue('segment').toString();
if (segmentValue) {
var segment1 = segmentValue.split(',');
segmentlist = segmentlist.concat(segment1);
}
}
// Clean up arrays: remove empty/whitespace values and duplicates
function cleanArray(arr) {
return arr.filter(function(x, i, self) {
return x && x.trim() && self.indexOf(x) === i;
});
}
companyList = cleanArray(companyList);
segmentlist = cleanArray(segmentlist);
// Build the encoded query only if there are values to use
if (companyList.length > 0 || segmentlist.length > 0) {
var queryParts = [];
if (companyList.length > 0) {
queryParts.push('company_name_of_customer.sys_idIN' + companyList.join(','));
queryParts.push('company_name_of_the_service_provider.sys_idIN' + companyList.join(','));
}
if (segmentlist.length > 0) {
queryParts.push('segment_of_customerIN' + segmentlist.join(','));
queryParts.push('segment_of_service_providerIN' + segmentlist.join(','));
}
// Add your static conditions
queryParts.push('state!=delete');
queryParts.push('contract_type!=intra_company_cost_shift');
queryParts.push('segment_of_customerISNOTEMPTY');
queryParts.push('segment_of_service_providerISNOTEMPTY');
// Join with ^OR as needed
var newQry = queryParts.join('^OR');
// Apply the encoded query to the current GlideRecord
current.addEncodedQuery(newQry);
// Optional: log the query for debugging
// gs.info("Final Encoded Query: " + newQry);
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2025 02:08 AM
your closing bracket for while should not be at the end
Did you try with hard-coded query in query BR?
things to check
1) field level READ ACL
2) domain separation
3) extra comma in encodedQuery
4) test the queries using list view
5) remove any empty strings from your arrays
try this
// Get the current user's sys_id
var userid = gs.getUserID();
// Prepare arrays to store company and segment values
var companyList = [];
var segmentlist = [];
// Query the custom table for relevant records
var companyListGr = new GlideRecord('x_custom_table');
companyListGr.addEncodedQuery("active!=false^ORactive=NULL^requested_for=" + userid);
companyListGr.query();
while (companyListGr.next()) {
// Collect company sys_ids (assuming comma-separated)
var companyArray = companyListGr.company_sys_id.toString().split(',');
companyList = companyList.concat(companyArray);
// Collect segment values (assuming comma-separated display values)
var segmentValue = companyListGr.getDisplayValue('segment').toString();
if (segmentValue) {
var segment1 = segmentValue.split(',');
segmentlist = segmentlist.concat(segment1);
}
}
// Clean up arrays: remove empty/whitespace values and duplicates
function cleanArray(arr) {
return arr.filter(function(x, i, self) {
return x && x.trim() && self.indexOf(x) === i;
});
}
companyList = cleanArray(companyList);
segmentlist = cleanArray(segmentlist);
// Build the encoded query only if there are values to use
if (companyList.length > 0 || segmentlist.length > 0) {
var queryParts = [];
if (companyList.length > 0) {
queryParts.push('company_name_of_customer.sys_idIN' + companyList.join(','));
queryParts.push('company_name_of_the_service_provider.sys_idIN' + companyList.join(','));
}
if (segmentlist.length > 0) {
queryParts.push('segment_of_customerIN' + segmentlist.join(','));
queryParts.push('segment_of_service_providerIN' + segmentlist.join(','));
}
// Add your static conditions
queryParts.push('state!=delete');
queryParts.push('contract_type!=intra_company_cost_shift');
queryParts.push('segment_of_customerISNOTEMPTY');
queryParts.push('segment_of_service_providerISNOTEMPTY');
// Join with ^OR as needed
var newQry = queryParts.join('^OR');
// Apply the encoded query to the current GlideRecord
current.addEncodedQuery(newQry);
// Optional: log the query for debugging
// gs.info("Final Encoded Query: " + newQry);
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-11-2025 04:55 AM
Thank you @Ankur Bawiskar . the issue was with while loop closing brackets. also thanks for sharing rest of the code as that helped to fine tune my BR