Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

how to get all the fields of custom table for PDF Generator.

sreevidya Kota
Giga Contributor

Dear All, 

I have requirement in PDF Generator for custom table. I am able to generate PDF for Custom table using UI action but I need to hard code the each and every field value to get inserted in PDF.  I need any other solution to get display the field values based on one  filed. When i have hard coded the values it is showing all the values whatever their in table. But i need to show few fields based on other field. Written code as below in UI action.

var v = new sn_pdfgeneratorutils.PDFGenerationAPI;

var boldltr1 = FinalString1.bold();

var field= 'Number: '+current.number + '<br>' + '<br>'+ 'Template Name: '+current.template_name +'<br>' + '<br>'+ 'Alt Hz: '+current.u_althz + '<br>'+ '<br>'+'CL Society: '+current.cl_society + '<br>'+ '<br>' +'Alt P (kWe): '+current.u_altpkwe ;

var recsysid = current.getUniqueValue();

var result = v.convertToPDF(field, "custom_table_name",recsysid, current.number);

Kindly provide your helpful inputs.

1 ACCEPTED SOLUTION

Community Alums
Not applicable

Hi SreeVidya,

please refer below code

var v = new sn_pdfgeneratorutils.PDFGenerationAPI();

var tableName = current.getTableName();
var columLabels = ['number']; //As number field is derived from extended table

var grDict = new GlideRecord('sys_dictionary');
grDict.addEncodedQuery('name=' + tableName + '^internal_type!=collection^internal_typeISNOTEMPTY');
grDict.query();

while(grDict.next()) {
  columLabels.push(grDict.element + '');
}

var fields = '';
for (f in columLabels) {
    if (!columLabels[f].startsWith('sys_')) { //to avoid sys fields
        if (fields.length > 0) //if value exists in fields
            fields += '<br><br>';

        fields += current[columLabels[f]].getLabel() + ' : ' + current[columLabels[f]].toString();
    }
}

var result = v.convertToPDF(fields, tableName, current.getUniqueValue(), current.number);

action.setRedirect(current);

View solution in original post

5 REPLIES 5

Thank You very much SriRam