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 generate and attach an Asset Handover PDF from after acknowledgment through portal

RajeshS45028713
Tera Contributor

Hi Everyone,

I have created a custom widget on the Service Portal where the user can acknowledge an asset (for example, confirming asset handover).
Now I want to generate a PDF of that acknowledgment form (Asset Handover document) automatically once the user clicks the “Acknowledge” button.

The requirements are:

  1. Generate a PDF with the same layout and fields as our current Asset Handover form.

  2. Attach that generated PDF to the related record (like Asset / Task / Custom table).

  3. Include the same PDF as an attachment in an email notification sent to the user.

Could someone please suggest the best approach or example for:

  • Using sn_pdfgeneratorutils or GlidePDFGenerationAPI to generate PDFs dynamically, or

  • Any Flow Designer / Script Include setup to achieve this?

Any example script or configuration guidance would be really helpful.

Thanks in advance!
Rajesh Singh

3 REPLIES 3

Muhammad Salar
Giga Sage

Hello @RajeshS45028713 

This is an example, you can modify this for your need

Widget:
Html:

<div class="panel panel-default">
  <div class="panel-heading">
    <h3>User Information</h3>
    <button class="btn btn-primary pull-right" ng-click="generatePDF();">Generate PDF</button>
  </div>

  <div class="panel-body">
    <table class="table table-striped">
      <tr><th>Name</th><td>{{::data.user.name}}</td></tr>
      <tr><th>Email</th><td>{{::data.user.email}}</td></tr>
      <tr><th>Username</th><td>{{::data.user.user_name}}</td></tr>
      <tr><th>Manager</th><td>{{::data.user.manager}}</td></tr>
      <tr><th>Company</th><td>{{::data.user.company}}</td></tr>
    </table>
  </div>
</div>

Server:

(function() {
    var gr = new GlideRecord('sys_user');
    gr.addQuery('sys_id', gs.getUserID());
    gr.query();

    while (gr.next()) {
        data.user = {
            name: gr.name.toString(),
            email: gr.email.toString(),
            user_name: gr.user_name.toString(),
            company: gr.company.name.toString(),
            manager: gr.manager.name.toString(),
            sysId: gr.getUniqueValue()
        };
    }
})();

Client:

api.controller = function($scope) {
    /* widget controller */
    var c = this;

    $scope.generatePDF = function() {
        var ga = new GlideAjax('global.UserPDFGenerator');
        ga.addParam('sysparm_name', 'generateUserPDF');
        ga.addParam('sysparm_input', JSON.stringify(c.data.user));

        ga.getXMLAnswer(function(response) {
            try {
                var result = JSON.parse(response);
                if (result.id) {
                    var pdfSysId = result.id;
                    window.open('/sys_attachment.do?sys_id=' + pdfSysId, '_blank');
                } else {
                    alert('Failed to generate PDF.');
                }
            } catch (e) {
                alert('Error: ' + e.message);
            }
        });
    };
};


Script Include:

var UserPDFGenerator = Class.create();
UserPDFGenerator.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    
    generateUserPDF: function() {
        var input = this.getParameter('sysparm_input');
        var user = JSON.parse(input);

		var hfInfo = new Object();
        hfInfo["FooterText"] = "Footer";
        hfInfo["PageSize"] = "A4";
        hfInfo["GeneratePageNumber"] = "true";
        hfInfo["TopOrBottomMargin"] = "60";
        hfInfo["LeftOrRightMargin"] = "42";
        hfInfo["PageOrientation"] = "PORTRAIT";

        var html = "<h2>User Information</h2>" +
            "<p><strong>Name:</strong> " + user.name + "</p>" +
            "<p><strong>Email:</strong> " + user.email + "</p>" +
            "<p><strong>Username:</strong> " + user.user_name + "</p>" +
            "<p><strong>Manager:</strong> " + user.manager + "</p>" +
            "<p><strong>Company:</strong> " + user.company + "</p>";

        var pdf = new sn_pdfgeneratorutils.PDFGenerationAPI;
        var pdfOutput = pdf.convertToPDFWithHeaderFooter(html, 'sys_user', user.sysId, user.name, hfInfo);

        var result = {};
        if (pdfOutput) {
            result.id = pdfOutput.attachment_id;
        }

        return JSON.stringify(result);
    },

	type: 'UserPDFGenerator'

});


This will attach the pdf to record and download it

Hi @RajeshS45028713 
Did you try this?

ben_hollifield
Tera Guru

Hi @RajeshS45028713  

 

If you need a turn-key solution with a great deal of flexibility, Yansa Labs' PDF Generator app (on the ServiceNow Store) can do quite a lot. You can check the links below for more information. Feel free to reach out to Yansa Labs directly with any questions!