- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-06-2025 02:34 AM
Hi all,
I'm trying to retrieve values as a comma-separated list from a field of type List in a Document Template Script. However, I'm encountering an "undefined" or "null" error only when generating the document in the Workspace. It works fine in the Core UI.
Here is the code snippet I'm using:
var dependents = target.u_dependents; grContacts.addQuery('sys_id', 'IN', dependents);
I've tried several alternatives, including:
var dependents = target.getValue('u_dependents'); var dependents = target.getElement('u_dependents'); var dependents = target.u_dependents.toString();
Does anyone have any suggestions on how to resolve this issue?
Thank you in advance,
Victoria
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-06-2025 04:09 AM
did you add logs and see what came in the target.u_dependents?
try this and query and then get
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
05-06-2025 02:55 AM
share your complete script here along with config screenshots.
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
05-06-2025 03:57 AM
Hi @Ankur Bawiskar ,
This is the script:
(function runTemplateScript(target /*GlideRecord for target task*/, docTemplate /*GlideRecord for doc template*/) { var html = ''; var templateLang = docTemplate.getValue('language'); var templateDateFormat = docTemplate.getValue('template_date_format'); var dependents = target.u_dependents.toString(); // Get dependents details that need visa var contactsBlock = []; var grContacts = new GlideRecord('sn_hr_core_contact'); grContacts.addQuery('sys_id', 'IN', dependents); grContacts.query(); while (grContacts.next()) { var contactDetails = { name: grContacts.first_name.toString() + ' ' + grContacts.last_name.toString(), nationality: grContacts.u_nationality.getDisplayValue(), relation: grContacts.relation_to_employee.getDisplayValue() }; contactsBlock.push(contactDetails); } // --- Table for the dependents that need visa ----- html += '<table style="width: 100%; border-collapse: collapse; margin-top: 20px;">'; html += '<thead><tr><th style="border: 1px solid #000; padding: 8px;">Name</th><th style="border: 1px solid #000; padding: 8px;">Nationality</th><th style="border: 1px solid #000; padding: 8px;">Relation</th></tr></thead>'; html += '<tbody>'; // Adding dependents that need visa rows for (var i = 0; i < contactsBlock.length; i++) { html += '<tr>'; html += '<td style="border: 1px solid #000; padding: 8px;">' + contactsBlock[i].name + '</td>'; html += '<td style="border: 1px solid #000; padding: 8px;">' + contactsBlock[i].nationality + '</td>'; html += '<td style="border: 1px solid #000; padding: 8px;">' + contactsBlock[i].relation + '</td>'; html += '</tr>'; } html += '</tbody></table>'; // --- Closing the table ----- html += '<p> </p>'; return html; })(target, docTemplate);
The u_dependents is like the watch_list field.
Which configuration to share?
Thank you in advance and best regards,
Victoria
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-06-2025 02:57 AM
are you sure the user you are using has access to that reference table being referred by that list field?
Did you check with admin and it's working fine?
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
05-06-2025 03:59 AM
Hi @Ankur Bawiskar ,
I am testing with admin user. It works on the Core UI and when testing in the Document Template, but not in the Workspace.
Best regards,
Victoria