Document Template Script in Workspace - retrieve list collector values - undefined error

VictoriaSte
Giga Guru

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

1 ACCEPTED SOLUTION

@VictoriaSte 

did you add logs and see what came in the target.u_dependents?

try this and query and then get

(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;
    var gr = new GlideRecord("tableName"); // give your table name here
    gr.addQuery("sys_id", target.sys_id);
    gr.query();
    if (gr.next()) {
        dependents = gr.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>&nbsp;</p>';

    return html;
})(target, docTemplate);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

5 REPLIES 5

@VictoriaSte 

did you add logs and see what came in the target.u_dependents?

try this and query and then get

(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;
    var gr = new GlideRecord("tableName"); // give your table name here
    gr.addQuery("sys_id", target.sys_id);
    gr.query();
    if (gr.next()) {
        dependents = gr.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>&nbsp;</p>';

    return html;
})(target, docTemplate);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader