Accessing Image URLs from Custom Child Records in PDF Templates Using Suitelet in NetSuite

Suitelet Function: renderTemplateFile

The renderTemplateFile function uses the SuiteScript render module to create a PDF. It sets the template ID, adds the custom record data, and includes the image URLs from the child records. The resulting PDF is then returned.

Fetching Image URLs

The script fetches image URLs from the child records (‘customrecord_vr_pps_procdet’) using the NetSuite SuiteScript API. The getValue function retrieves the internal ID of the image field (‘custrecord_vr_pps_procdet_i1’). The internal ID is then used to search for the corresponding file record, and the URL is obtained.

 function onRequest(context) {

      try {

        var response = context.response;

       

        var newRecId = context.request.parameters.customRecordId;

        var itemArr = [];

        var ppsRecord = record.load({

          type: “customrecord_vr_pps”,

          id: newRecId

        });

        let sublistLineCount = ppsRecord.getLineCount({

          sublistId: “recmachcustrecord_vr_pps_procdet_pps”

        });

   

         

        for (var line = 0; line < sublistLineCount; line++) {

          var imageId1;

          let pudId = ppsRecord.getSublistValue({

            sublistId: “recmachcustrecord_vr_pps_procdet_pps”,

            fieldId: “id”,

            line: line

          });

         

        

         log.debug(“pudId”,pudId)

          // Load the PUD record

          var pudRecord = record.load({

            type: ‘customrecord_vr_pps_procdet’,

            id: pudId,

            isDynamic: true,

          });

          var itemObj = {}

        

        var img1 = pudRecord.getValue({

          fieldId: “custrecord_vr_pps_procdet_i1”

        });

        

       

       if(img1) { let UrlSearch = search.create({

        type: ‘file’,

        filters: [“internalid”,”is”,img1],

        columns: [“url”]

        

        });

        

        var searchResults = UrlSearch.run();

      searchResults.each(function(result) {

         imageId1 = “https://6714807-sb1.app.netsuite.com”+ result.getValue({

          name: ‘url’

        }).replace(/&/g, ‘&amp;’);

        return true; 

      }); }

      

        

       

       itemObj.img1 = imageId1;

       

        itemArr.push(itemObj);

         

         

        }

        log.debug(‘item Array…’, itemArr)

       

        var itemdetails = itemArr;

        // log.debug(“itemdetails”, itemdetails)

         var pdfFiles = renderTemplateFile(itemdetails, newRecId)

        response.writeFile(pdfFiles, true)

      }catch(e){

        log.debug(“error”,e)

      }

   

    }

    function renderTemplateFile(itemLineDetails,newRecId) {

      try{

      var templateRenderer = render.create();

      //template id 107

      templateRenderer.setTemplateById(127);

      // log.debug(“testing id”)

      templateRenderer.addRecord(‘record’, record.load({

        type: “customrecord_vr_pps”,

        id: newRecId

      }));

      templateRenderer.addCustomDataSource({

        format: render.DataSource.OBJECT,

        alias: “itemLineDetails”,

        data: {

          itemLineDetails: itemLineDetails

        }

      });

      var pdf = templateRenderer.renderAsPdf();

      pdf.name = “PPS ” + newRecId + “.pdf”;

      return pdf;

      // log.debug(“end”, scriptInstance.getRemainingUsage());

    } catch(e){

        log.debug(“error”,e)

      }

   

    }

Leave a comment

Your email address will not be published. Required fields are marked *