- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
I have a Widget Server Script
(function() {
data.accounts = [];
data.attachments = []; // To store attachments
var userSysId = gs.getUserID();
// --- Step 1: Find customer_contact for the logged-in user
var contactGR = new GlideRecord('customer_contact');
contactGR.addQuery('sys_id', userSysId);
contactGR.query();
if (contactGR.next()) {
var mainAccountSysId = contactGR.account.toString();
if (mainAccountSysId) {
addAccount(mainAccountSysId);
fetchChildAccounts(mainAccountSysId);
}
}
// --- Step 2: Find additional accounts through contact relationships
var relationshipGR = new GlideRecord('sn_customerservice_contact_relationship');
relationshipGR.addQuery('contact', userSysId);
relationshipGR.query();
while (relationshipGR.next()) {
var companySysId = relationshipGR.company.toString().trim();
if (companySysId) {
addAccount(companySysId);
fetchChildAccounts(companySysId);
}
}
// --- Helper functions ---
function addAccount(sysId) {
var accGR = new GlideRecord('customer_account');
if (accGR.get(sysId)) {
var accountName = accGR.name.toString();
data.accounts.push({
sys_id: accGR.getUniqueValue(),
name: accountName
});
// Fetch attachments for this account
fetchAttachments(sysId, accountName);
}
}
function fetchChildAccounts(parentSysId) {
var childGR = new GlideRecord('customer_account');
childGR.addQuery('account_parent', parentSysId);
childGR.query();
gs.info("SV 50: Number of child accounts for " + parentSysId + " is " + childGR.getRowCount());
while (childGR.next()) {
var childId = childGR.getUniqueValue();
if (!accountExists(childId)) {
addAccount(childId);
fetchChildAccounts(childId);
}
}
}
function accountExists(sysId) {
return data.accounts.some(function(account) {
return account.sys_id === sysId;
});
}
function fetchAttachments(accountSysId, accountName) {
var attGR = new GlideRecord('sys_attachment');
attGR.addQuery('table_sys_id', accountSysId);
attGR.query();
while (attGR.next()) {
data.attachments.push({
sys_id: attGR.getUniqueValue(),
file_name: attGR.file_name.toString(),
account_sys_id: accountSysId,
account_name: accountName
});
}
}
})();
Requirement is to fetch the parent and the corresponding child accounts.
When the parentSysId is fetched from customer_contact (which is in gloabl scope), i am able to get the child accounts.
When the parentSysId is fetched from sn_customerservice_contact_relationship (which is in Customer Service scope), i am not able to get the child accounts.
When I run this part of the script as background script (with the sysid from sn_customerservice_contact_relationship), I can see all the child accounts.
var childGR = new GlideRecord('customer_account');
childGR.addQuery('account_parent', parentSysId);
childGR.query();
gs.info("SV 50: Number of child accounts for " + parentSysId + " is " + childGR.getRowCount());
while (childGR.next()) {
var childId = childGR.getUniqueValue();
if (!accountExists(childId)) {
addAccount(childId);
fetchChildAccounts(childId);
}
}
The widget is in Customer Service scope. How do I resolve this issue?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
I found the issue. It is an ACL issue. The user is not able to access the child account when he is mentioned in Customer User Relationship. This is because of the Business Rule: Account query rules.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
since table and widget are in same scope cross scope issue should not come.
what's the error you are getting?
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
3 weeks ago
This line:
gs.info("SV 50: Number of child accounts for " + parentSysId + " is " + childGR.getRowCount());
In the widget the RowCount is 0
In the background script it is 1
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
you said you are getting cross scope issue?
what's that issue?
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
3 weeks ago
I am not sure if this is cross scope issue, that is my assumption.
When fetchChildAccounts is called after fetching sysId from customer_contact, it gets the child accounts correctly. However when fetchChildAccounts is called after fetching sysId from sn_customerservice_contact_relationship, it returns 0 (even though this account had child accounts).
sn_customerservice_contact_relationship is in Customer Service Scope and customer_contact is in global scope and the widget is in Customer Service Scope
Tried the following script in background an it returns the child accounts correctly.
parentSysId = 'f416f8e23365621097561b989d5c7b4b';
var childGR = new GlideRecord('customer_account');
childGR.addQuery('account_parent', parentSysId);
childGR.query();
gs.info("SV 50:(1): Number of child accounts for " + parentSysId + " is " + childGR.getRowCount());
However the same returns 0 in the widget.
Requirement is to get all the accounts and the child accounts associated to he user. Also should get accounts and related child account if the user is in Customer User Relationships.