Use PDIs? Take our 5-minute survey to help shape the PDI roadmap.

How to generate page number in the pdf through "Generate PDF" button on Impact analysis table(BCM)

sunil maddheshi
Tera Guru

Hey everyone, We have OOB UI action "Generate PDF" on Impact analysis table(sn_bia_analysis), which generates pdf but there is no pdf number on generated pdf, Can anyone please help me , what to do to make page number appear, screenshots attached

sunilmaddheshi_0-1785689678401.pngsunilmaddheshi_1-1785689776629.png

 

15 REPLIES 15

@sunil maddheshi 

1st you need to know which API that OOTB script include is using

did you check that?

generateDocument method within script include GenerateBiaPdfUtil?

 

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

@Ankur Bawiskar Below is the generateDocument method

 generateDocument: function(biaName, docTemplateName) {
        var templateId;
        var docCategory;
        var templateGr = this._getDocTemplate(
            this.documentTemplateTable,
            docTemplateName
        );
        if (templateGr && templateGr.isValid()) {
            templateId = templateGr.getValue('sys_id');
            docCategory = templateGr.getDisplayValue('category');
        }

        var docName = docCategory ? docCategory + '-' + biaName : biaName;
        var docNameWithExt = docName + this.FILE_EXTENSION;
        this.deleteExistingDocument(docNameWithExt);

        var attachmentId = new sn_doc.GenerateDocumentAPI().generateDocumentForTask(
            this.biaSysId,
            templateId,
            docName
        );
        var pdfDownloadLink = attachmentId ?
            this._getPdfDownloadLink(attachmentId, biaName) :
            null;

        return {
            attachmentId: attachmentId,
            downloadLink: pdfDownloadLink ? pdfDownloadLink : biaName,
        };
    },

And this method "generateDocumentForTask" is below

	initiateDocumentTasks: function(taskGr, htmlBody, docTemplateId, generatedPdfName, documentId) {
		var flowInputs = {};
		flowInputs['task_record'] = taskGr;
		flowInputs['template_id'] = docTemplateId;
		flowInputs['generated_pdf_name'] = generatedPdfName;
		flowInputs['document_id'] = documentId;
		if (!gs.nil(htmlBody) && taskGr.document_template.sys_class_name.toString() == "sn_doc_html_template")
			flowInputs['html_body'] = htmlBody;
		var result = sn_fd.Subflow.startAsync('sn_doc.automatic_document_task_creation', flowInputs);
		if (gs.nil(result) || gs.nil(result.contextId)) {
			gs.error('Automatic Document Task Creation flow failed to start. Template ID: ' + docTemplateId +',Task ID: '+ taskGr.getValue('sys_id'));
			return false;
		}
		var gr = new GlideRecord('sn_doc_task_execution');
		gr.initialize();
		gr.setValue('task',taskGr.sys_id);
		gr.setValue('document_template',docTemplateId);
		gr.setValue('flow_context',result.contextId);
		gr.setValue('status','initiated');
		gr.insert();
		return true;
	},

	isDocumentTasksExecutionActive: function(taskGr, docTemplateId) {
		var gr = new GlideRecord('sn_doc_task_execution');
		gr.addQuery('task',taskGr.sys_id);
		gr.addQuery('document_template',docTemplateId);
		gr.addQuery('status','!=','closed');
		gr.query();
		return gr.hasNext();
	},

    generateDocumentForTask: function(recordId, documentTemplateId, pdfName) {
        var isValid = this._validateUserAccess(documentTemplateId, recordId);
        if (!isValid) { 
            gs.error("User do not access to the record");
            return null;
        }

        var documentTemplateGr = this._getGlideRecord("sn_doc_template", documentTemplateId)();
        var templateType = new DocumentTemplateUtils().getTemplateType(documentTemplateGr);

        if (templateType == "sn_doc_pdf_template")
            return new PdfTemplateUtils().generate(recordId, documentTemplateId, pdfName);
        else if (templateType == "sn_doc_html_template")
            return new HtmlTemplateUtils().generate(recordId, documentTemplateId, pdfName);
    },
	
    /**
     * Description: Method to prefill pdf document with mapped values returns document as
     * base64 content
     * @param {SysId} recordId 
     * @param {SysId} documentTemplateId 
     * @return {{status: string, message: string, pdf_name:string, document_base64: string}}
     */
    getPreFilledDocumentAsBase64: function(recordId, documentTemplateId) {
        var response = {
            "status": "error",
            "message": "Error while generating pre-filled document"
        };			
        var isValid = this._validateUserAccess(documentTemplateId, recordId);
        if (!isValid) { 
            response.message = "Data is incorrect or User does not have access to the record";
            return response;
        }
        try {
            response = new PdfTemplateUtils().getPreFilledDocumentAsBase64(recordId, documentTemplateId);
        }
        catch(exception){
            gs.error("Error generating the prefilled document as base64"+exception);
        }
        return response;
    },	

    // Document template table should be same as recordTableName.
    _validateUserAccess: function(documentTemplateId, recordId) {
        if (gs.nil(documentTemplateId) || gs.nil(recordId))
            return false;

        var gRecord = new GlideRecord('sn_doc_template');
        if (gRecord.get(documentTemplateId)) {
            var tableName = gRecord.getValue('table');
            if (gs.nil(tableName))
                return false;
            var record = new GlideRecord(tableName);
            return record.get(recordId) && record.canRead();
        }

        return false;
    },

    _getGlideRecord: function(table, sysId) {
        var gr = new GlideRecord(table);
        return function(query) {
            if (sysId)
                gr.get(sysId);
            else {
                gr.addActiveQuery();
                Object.keys(query).map(function(q) {
                    gr.addQuery(q, query[q]);
                });
                gr.query();
            }
            return gr;
        };
    },

    type: 'GenerateDocumentAPISNC'
};

@sunil maddheshi 

it looks like it's using OOTB GenerateDocumentAPI

see if that supports page number

if not then have your PDF Generation logic

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

Tanushree Maiti
Tera Patron

Hi @sunil maddheshi 

 

Read this Article for adding page number : Document Template: Dynamic Header and Footers

 

Also you can check :Generating Custom PDFs - using the new PDFGenerationAPI

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti

@Tanushree Maiti The article you shared uses PDFGenerationAPI, while BCM code uses GenerateDocumentAPI. Those are different APIs.