Hi All,
I am trying to build a workflow action script via a button that would generate a advanced PDF of fields from the current record and also to pull in data from a different set of custom records via a search, this seems to possible by utilising ‘.addsearchresults’ and ‘.addrecord’ with ’render.create’.
I have built the code following the guidelines and the .addrecord part works fine but the .addsearchresults doesn’t seem to be working as the field data isn’t shown in the created PDF just the fieldnames.
Workflow script below
http:///**
* @NApiVersion 2.0
* @NScriptType WorkflowActionScript
*/
// This sample shows how to render an invoice into a PDF file using an XML template in the file cabinet.
// Note that this example requires the Advanced PDF/HTML Templates feature.
define(['N/render', 'N/file', 'N/record', 'N/search','N/runtime'],
function(render, file, record, search, runtime) {
function onAction(context,response) {
var response = context.response;
log.debug({
title: 'Start Script'
});
log.debug({
title: 'Start Usage',
details: runtime.getCurrentScript().getRemainingUsage()
});
var CurrentRecord = context.newRecord;
var CurrentRecordID = CurrentRecord.id;
var CurrentRecordTYPE = CurrentRecord.type;
log.debug({
title: 'Record ID',
details: CurrentRecordID
});
var ParentSA = CurrentRecord.getValue({
fieldId: 'custrecord_sat_sa_parent'
});
var renderer = render.create();
renderer.setTemplateById(121);
renderer.addRecord('record', record.load({
type: CurrentRecordTYPE,
id: CurrentRecordID
}));
log.debug({
title: 'Current Record',
details: CurrentRecord
});
//new serach build
var mySearch = search.load({
id: 'customsearch2833'
});
log.debug({
title: 'mySearch',
details: mySearch,
});
var filters = mySearch.filters; //reference Search.filters object to a new variable
var filterOne = search.createFilter({ //create new filter
name: 'custrecord_sat_sa_parent',
operator: search.Operator.ANYOF,
values: ParentSA
});
filters.push(filterOne); //add the filter using .push() method
var srchRes = mySearch.run() //run script
var results = srchRes.getRange(0, 1000);
log.debug({
title: 'results.length',
details: results.length,
});
log.debug({
title: 'results',
details: results,
});
renderer.addSearchResults({
templateName: 'results',
searchResult: results,
});
log.debug({
title: 'renderer',
details: renderer,
});
var newfile = renderer.renderAsPdf();
//response.writeFile({ newfile: pdf, isInline: false })
newfile.folder = 1477066;
newfile.name = 'ben_test' + '.pdf';
var fid = newfile.save();
var attachitem = record.attach({
record: { type: 'file', id: fid },
to: { type:
CurrentRecordTYPE, id: CurrentRecordID }
});
// return renderer.renderAsPdf();
// render.PrintMode.PDF
// return context.response.writeFile({ file: pdfFile });
log.debug({
title: 'newfile',
details: newfile
});
}
return {
onAction: onAction
}
});
Advanced PDF code below
<table style="border-collapse: collapse; width: 100%; height: 33.5764px; border: 1px solid rgb(0, 0, 0);" border="1"><#list results as result><#if result_index ==0>
<colgroup>
<col style="width: 25%;" /><col style="width: 25%;" /><col style="width: 25%;" /><col style="width: 25%;" /></colgroup>
<tr style="height: 16.7882px; background-color: rgb(126, 140, 141);">
<td style="height: 16.7882px; border-color: rgb(0, 0, 0);">Date</td>
<td style="height: 16.7882px; border-color: rgb(0, 0, 0);">Details</td>
<td style="height: 16.7882px; border-color: rgb(0, 0, 0);">Hours</td>
<td style="height: 16.7882px; border-color: rgb(0, 0, 0);">Amount</td>
</tr></#if>
<tr style="height: 16.7882px;">
<td style="height: 16.7882px; border-color: rgb(0, 0, 0);">$(result.custrecord_sat_delivery_date)</td>
<td style="height: 16.7882px; border-color: rgb(0, 0, 0);">$(result.custrecord_sat_work_details)</td>
<td style="height: 16.7882px; border-color: rgb(0, 0, 0);">$(result.custrecord_sat_work_actual_time_spent)</td>
<td style="height: 16.7882px; border-color: rgb(0, 0, 0);">$(result.custrecord_sat_work_actual_amount_spent)</td>
</tr></#list></table>