generate and download pdf only multiple fields in the form

marrikruthi
Tera Contributor

generate and download pdf , the pdf contain only patient name, doctor name, patient id, date of birth, phone number, email, gender

1 ACCEPTED SOLUTION

Mark Manders
Mega Patron

Please close this one and follow up here, by also adding what solution you already tried and what the results were, making you say 'it didn't work'


https://www.servicenow.com/community/developer-forum/add-two-more-fields-to-the-script/td-p/3013745

 


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

View solution in original post

5 REPLIES 5

Sohail Khilji
Kilo Patron
Kilo Patron

Hi @marrikruthi,

 

You can create a new view on the form called PDF this will contain all the fields like (patient name, doctor name, patient id, date of birth, phone number, email, gender) what ever you need.

 

Create a UI action as below.

function export2PDF() { 
 var sysparm_table = g_form.getTableName(); 
 var sysparm_sys_id = g_form.getUniqueValue().toString(); 
 var instanceName = 'https://my-instance.service-now.com/';
 var url = instanceName+sysparm_table + '.do?PDF&sys_id=' + sysparm_sys_id + "&sysparm_view=PDF"; 
 g_navigation.openPopup(url);
}

 

I hope this helps...


☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect

this script downloading whole form details but I want specific fields in the form

Shaqeel
Mega Sage

hi @marrikruthi 

 

If you want to create a button for this requirement follow these steps:

 

Create a new Script Include:

var PDFGenerator = Class.create();
PDFGenerator.prototype = {
    initialize: function() {},

    generatePDF: function(recordSysId) {
        var gr = new GlideRecord('your_table_name'); // Replace 'your_table_name' with the actual table name
        if (gr.get(recordSysId)) {
            var doc = new sn_pdfgenerator.PDFDocument();
            
            // Add specific fields
            doc.addText('Field1: ' + gr.getValue('field1')); // Replace 'field1' with actual field name
            doc.addText('Field2: ' + gr.getValue('field2')); // Replace 'field2' with actual field name
            doc.addText('Field3: ' + gr.getValue('field3')); // Replace 'field3' with actual field name
            
            // Generate PDF
            var pdfBytes = doc.generate();
            
            // Save the PDF as an attachment to the record
            var attachment = new GlideSysAttachment();
            var attachmentSysId = attachment.write('your_table_name', recordSysId, 'Record PDF', 'application/pdf', pdfBytes);
            
            return attachmentSysId;
        } else {
            return null;
        }
    },

    type: 'PDFGenerator'
};

 

Now create a new UI Action and modify the below code:

// UI Action Script
(function() {
    var recordSysId = g_form.getUniqueValue();
    var pdfGenerator = new PDFGenerator();
    var attachmentSysId = pdfGenerator.generatePDF(recordSysId);
    
    if (attachmentSysId) {
        // Redirect to the attachment to download it
        var url = new GlideURL('sys_attachment.do');
        url.addParam('sys_id', attachmentSysId);
        g_navigation.open(url.getURL(), '_blank');
    } else {
        g_form.addErrorMessage('Failed to generate PDF.');
    }
})();

 

 

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting "Accept as Solution" and " Helpful." This action benefits both the community and me.  

*************************************************************************************************************

 

 

Regards

Shaqeel

 


***********************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting "Accept as Solution" and " Helpful." This action benefits both the community and me.

***********************************************************************************************************************





Regards

Shaqeel

no it is not working

@Shaqeel