首页 > 解决方案 > NetSuite SuiteScript 2.0 如何从 UserEvent 按钮创建银行存款单(suitelet 功能)

问题描述

我是一名非开发人员,对 NetSuite 和 SuiteScript 是全新的,我正在磕磕绊绊。

问题:对于每条存款记录,我需要创建一个可打印的 pdf,其中列出了该记录上的每笔付款(银行存款单)。我想在自定义按钮上调用此 pdf,以显示在可以打印的新窗口中。我不需要将文档保存在文件柜中。

1.现在我有一个 UserEvent 按钮,它在查看和编辑模式下显示在存款上。

define([], function () {
  /**
   * @NApiVersion 2.x
	 * @NScriptType UserEventScript
	 */
	var exports = {};

	function beforeLoad(context) {
		context.form.addButton({
            id: "custpage_printdepositslip",
        	label: "Print Deposit Slip",
			functionName: "onButtonClick"
        });
		context.form.clientScriptModulePath = "SuiteScripts/depositSlips/customDepositSlipButton.js"
	}

	exports.beforeLoad = beforeLoad;
	return exports;

});

2.此按钮从名为“customDepositSlipButton.js”的 ClientScript 调用 clickhandler 函数“onButtonClick”

define(["N/ui/dialog"], function (dialog) {
  /** 
   * @NApiVersion 2.x
	 * @NScriptType ClientScript
	 */
	var exports = {};
  
	function pageInit(context) {
		 // TODO	
	}

  	function onButtonClick() {
     dialog.alert({
            title: "COMING SOON",
        	message: "This feature will eventually create a bank deposit slip"
        });
	}

	exports.onButtonClick = onButtonClick;
	exports.pageInit = pageInit;
	return exports;

});

目前这可行,但仅创建一个用于测试目的的对话框弹出窗口。到目前为止,一切都很好。 测试弹窗

这就是我卡住的地方:我不明白现在如何将它连接到 Suitelet 上的一个函数,该函数应该从 xml 生成一个 pdf 文件。

3.我已经在标题为“depositSlipPDF.js”的同一文件柜位置中设置了一个带有 xml 脚本的 pdf 的 Suitelet,其函数“generatePdfFileFromRawXml”如下所示。

define(['N/render', 'N/record'],
function(render, record) {
/**
	 * @NApiVersion 2.x
	 * @NScriptType Suitelet
	 * @appliedtorecord deposit
	 */
/**
	 * <code>onRequest</code> event handler
   * @gov 0
	 * 
	 * @param request
	 * 			{Object}
	 * @param response
	 * 			{String}
	 * 
	 * @return {void}
	 * 
	 * @static
	 * @function onRequest
	 */

    function generatePdfFileFromRawXml() {
                var xml = '<?xml version="1.0"?>n<!DOCTYPE pdf PUBLIC "-//big.faceless.org//report" "report-1.1.dtd">n<pdf>n<body size="letter-landscape" font-size="10">n';

        xml += '<table width="100%" align="center">n';
        xml += '<tr>n';
        xml += '<td>Deposit Number: ' + depositRecord.getFieldValue('tranid') + '</td>n';
        xml += '</tr>n';
        xml += '<tr>n';
        xml += '<td>Date: ' + depositRecord.getFieldValue('trandate') + '</td>n';
        xml += '</tr>n';
        xml += '<tr>n';
        xml += '<td>Total: ' + depositRecord.getFieldValue('total') + '</td>n';
        xml += '</tr>n';
        xml += '<tr>n';
        xml += '<td>Posting Period: ' + depositRecord.getFieldText('postingperiod') + '</td>n';
        xml += '</tr>n';
        xml += '</table>n';

        xml += '<br /><br />n';

        xml += '<table width="100%" align="center">n';
        xml += '<thead>n';
        xml += '<tr>n';
        xml += '<th>Date</th>n';
        xml += '<th>ID</th>n';
        xml += '<th>Customer</th>n';
        xml += '<th>Payment Amount</th>n';
        xml += '<th>Transaction Amount</th>n';
        xml += '<th>Transaction Type</th>n';
        xml += '<th>Payment Method</th>n';
        xml += '</tr>n';
        xml += '</thead>n';
        xml += '<tbody>n';

        for (var i = 1; i < parseInt(depositRecord.getLineItemCount('payment')) + 1; i++)
        {
        if (depositRecord.getLineItemValue('payment', 'deposit', i) == 'T')
        {
        xml += '<tr>n';
        xml += '<td>' + depositRecord.getLineItemValue('payment', 'docdate', i) + '</td>n';
        xml += '<td>' + depositRecord.getLineItemValue('payment', 'docnumber', i) + '</td>n';
        xml += '<td>' + depositRecord.getLineItemText('payment', 'entity', i) + '</td>n';
        xml += '<td>' + depositRecord.getLineItemValue('payment', 'paymentamount', i) + '</td>n';
        xml += '<td>' + depositRecord.getLineItemValue('payment', 'transactionamount', i) + '</td>n';
        xml += '<td>' + depositRecord.getLineItemValue('payment', 'type', i) + '</td>n';
        xml += '<td>' + depositRecord.getLineItemText('payment', 'paymentmethod', i) + '</td>n';
        xml += '</tr>n';
        }
        }

        xml += '</tbody>n';
        xml += '</table>n';
        xml += '</body>n</pdf>';
      
        var pdfFile = render.xmlToPdf({
            xmlString: xmlStr
        });
    }
  	return {
    	onRequest: generatePdfFileFromRawXml
    }
});

如何调用 generatePdfFileFromRawXml(); 从 onButtonClick();?

取得这一步的功劳归功于Stoic Software,其中 SS2 变得易于理解(谢谢,Eric),并感谢我在这篇文章中获取这些初始 xml 数据拉取代码的团队标签。

标签: netsuitesuitescript2.0

解决方案


使用前面两个答案的输入,这是完整的工作解决方案和结果的屏幕截图:

步骤 1) 为自定义按钮建立点击处理程序的客户端脚本,它获取我当前记录的 id 并将其传递给我的套件,该套件在新窗口中执行

define(['N/url', 'N/currentRecord'], function (url, currentRecord) {
/**
  * 
  * @NApiVersion 2.x
  * @NScriptType ClientScript
  * @appliedtorecord deposit
  */
var exports = {};
/**
  * <code>pageInit</code> event handler
  * 
  * @gov XXX
  * 
  * @param context
  *             {Object}
  * @param context.mode
  *             {String} The access mode of the current record. will be one of
  *                 <ul>
  *                 <li>copy</li>
  *             <li>create</li>
  *             <li>edit</li>
  *             </ul>
  * 
  * @return {void}
  * 
  * @static
  * @function pageInit
  */
 function pageInit(context) {
     // TODO
 }
 function onButtonClick() {
  var suiteletUrl = url.resolveScript({
    scriptId: 'customscript_depositslip_pdf', //the script id of my suitelet
    deploymentId: 'customdeploy_depositslip_pdf', //the deployment id of my suitelet
    returnExternalUrl: false,
    params: {
        custom_id: currentRecord.get().id,
    },
 });
 window.open(suiteletUrl);
 }
 exports.onButtonClick = onButtonClick;
 exports.pageInit = pageInit;
 return exports;
 });

第 2 步)用户事件脚本创建在我的存款记录的编辑和查看模式中可见的自定义按钮,并从我的客户端脚本调用点击处理程序。

define([], function () {
/**
  * 
  * @NApiVersion 2.x
  * @NScriptType UserEventScript
  * @appliedtorecord deposit
  */
 var exports = {};
 /**
  * <code>beforeLoad</code> event handler
  * 
  * @gov 0
  * 
  * @param context
  *             {Object}
  * @param context.newRecord
  *             {record} the new record being loaded
  * @param context.type
  *             {UserEventType} the action that triggered this event
  * @param context.form
  *             {form} The current UI form
  * 
  * @return {void}
  * 
  * @static
  * @function beforeLoad
  */
  function beforeLoad(context) {
    context.form.addButton({
        id: "custpage_printdepositslip",
        label: "Print Deposit Slip",
        functionName: "onButtonClick"
    });
    context.form.clientScriptModulePath = "SuiteScripts/ss2-add-button/customDepositSlipButton.js"
    }
  exports.beforeLoad = beforeLoad;
  return exports;
 });

步骤 3) Suitelet 脚本,用于从 xml 相对于当前存款的记录 id(您单击按钮的位置)创建自定义表单,该表单从存款子列表中提取信息并将其返回到带有标题的 pdf 中的组织表中,并且如果表格需要多页,页脚部分会保留在每页上。

define(['N/render', 'N/record', 'N/xml', 'N/format'],
function(render, record, xml, format) {
 /**
    *@NApiVersion 2.x
    * @NScriptType Suitelet
    * @appliedtorecord deposit
    */
 /**
    * <code>onRequest</code> event handler
    * @gov 0
    * 
    * @param request
    *           {Object}
    * @param response
    *           {String}
    * 
    * @return {void}
    * 
    * @static
    * @function onRequest
    * @function generateXml
    */
function onRequest(context) {
    var id = context.request.parameters.custom_id;
    if (!id) {
        context.response.write('The parameter "custom_id" is required');
        return;
    }
    var xmlString = generateXml(id);
    context.response.renderPdf({ xmlString : xmlString });
}
function generateXml(id) {
      var depositRecord = record.load({ type: record.Type.DEPOSIT, id: id });
      var totes = depositRecord.getValue('total');
      var totally = format.format({value:totes, type:format.Type.CURRENCY});
      var fulldate = depositRecord.getValue('trandate');
      var mmdddate = format.format({value:fulldate, type:format.Type.DATE});
      var xml='<?xml version="1.0" encoding="utf-8"?>\n<!DOCTYPE pdf PUBLIC "-//big.faceless.org//report" "report-1.1.dtd">\n<pdf>\n<head>\n<macrolist>\n<macro id="nlheader">\n';
        xml += '<table width="100%" align="center" style="font-size:11px;">\n';
        xml += '<tr>\n';
        xml += '<td><b>Deposit Number:</b> ' + depositRecord.getValue('tranid') + '</td>\n';
        xml += '</tr>\n';
        xml += '<tr>\n';
        xml += '<td><b>Date:</b> ' + mmdddate + '</td>\n';
        xml += '</tr>\n';
        xml += '<tr>\n';
        xml += '<td><b>Account:</b> ' + depositRecord.getText('account') + '</td>\n';
        xml += '</tr>\n';
        xml += '<tr>\n';
        xml += '<td><b>Total:</b> ' + totally + '</td>\n';
        xml += '</tr>\n';
        xml += '<tr>\n';
        xml += '<td><b>Posting Period:</b> ' + depositRecord.getText('postingperiod') + '</td>\n';
        xml += '</tr>\n';
        xml += '<tr>\n';
        xml += '<td><b>Memo:</b> ' + depositRecord.getText('memo') + '</td>\n';
        xml += '</tr>\n';
        xml += '</table>\n';
        xml += '</macro>\n<macro id="nlfooter">\n<table style="width: 100%;">\n<tr>\n<td align="right" style="padding: 0; font-size:8pt;">\n<p align="right" text-align="right" ><br /><pagenumber/> of <totalpages/></p>\n</td>\n</tr>\n</table>\n</macro>\n</macrolist>\n</head>\n<body header="nlheader" header-height="13%" footer="nlfooter" footer-height="10pt" padding="0.375in 0.5in 0.5in 0.5in"  style="font:10px arial, sans-serif; text-align:left;">\n';
        xml += '<table width="100%" align="center" cellpadding="4" cellspacing="0" style="text-align:left; border-left:1px solid #ccc; border-right:1px solid #ccc;">\n';
        xml += '<thead>\n';
        xml += '<tr style="border:1px solid #ccc; background-color:#efefef;">\n';
        xml += '<th style="border-right:1px solid #ccc;"><b>Date</b></th>\n';
        xml += '<th style="border-right:1px solid #ccc;"><b>ID</b></th>\n';
        xml += '<th style="border-right:1px solid #ccc;"><b>Customer</b></th>\n';
        xml += '<th style="border-right:1px solid #ccc;"><b>Payment Method</b></th>\n';
        xml += '<th style="border-right:1px solid #ccc;"><b>Type</b></th>\n';
        xml += '<th style="border-right:1px solid #ccc;"><b>Ref No.</b></th>\n';
        xml += '<th><b>Amount</b></th>\n';
        xml += '</tr>\n';
        xml += '</thead>\n';
        xml += '<tbody>\n';
        for (var i = 0; i < parseInt(depositRecord.getLineCount({sublistId: 'payment'})); i++)
        {
        var amt = depositRecord.getSublistValue({sublistId: 'payment', fieldId: 'paymentamount', line: i});
        var payamt = format.format({value:amt, type:format.Type.CURRENCY});
        var longdate = depositRecord.getSublistValue({sublistId: 'payment', fieldId: 'docdate', line: i});
        var shortdate = format.format({value:longdate, type:format.Type.DATE});
        if (depositRecord.getSublistText({sublistId: 'payment', fieldId: 'deposit', line: i}) == 'T')
        {
        xml += '<tr style="border-bottom:1px solid #ccc;">\n';
        xml += '<td style="border-right:1px solid #ccc;">' + shortdate  + '</td>\n';
        xml += '<td style="border-right:1px solid #ccc;">' + depositRecord.getSublistValue({sublistId: 'payment', fieldId: 'docnumber', line: i}) + '</td>\n';
        var name= depositRecord.getSublistText({sublistId: 'payment', fieldId: 'entity', line: i})
                var andName = name.match('&')
                if(andName){
                var newName = name.replace("&", "&amp;");
                xml += '<td style="border-right:1px solid #ccc;">' + newName + '</td>\n';
                }
                else 
                    xml += '<td style="border-right:1px solid #ccc;">' + name + '</td>\n';
        xml += '<td style="border-right:1px solid #ccc;">' + depositRecord.getSublistText({sublistId: 'payment', fieldId: 'paymentmethod', line: i}) + '</td>\n';
        xml += '<td style="border-right:1px solid #ccc;">' + depositRecord.getSublistValue({sublistId: 'payment', fieldId: 'type', line: i}) + '</td>\n';
        xml += '<td style="border-right:1px solid #ccc;">' + depositRecord.getSublistValue({sublistId: 'payment', fieldId: 'refnum', line: i}) + '</td>\n';
        xml += '<td align="right" style="text-align:right !important;"><p style="text-align:right !important;">' + payamt + '</p></td>\n';
        xml += '</tr>\n';
        }
        }
        xml += '</tbody>\n';
        xml += '</table>\n';
        xml += '</body>\n</pdf>';
      return xml;
    }
  return {
    generateXml:generateXml,
    onRequest: onRequest
  }
});

我遇到的主要障碍是我们的许多客户的客户名称中都有与号,而 xml 将其解释为运算符,这需要对“实体”子列表字段进行条件与号处理。此外,xml 想要返回包含时间戳的更长形式的日期字段,我正在使用变量和 N/format 模块将日期更改为结果表单上的 mm/dd/yyyy 格式。

所有这些都会在您的存款屏幕上放置一个自定义按钮,标有“打印存款单”或您在用户事件脚本中选择的任何标签。当你点击它时,它会在一个看起来像这样的新窗口中为你提供一个 pdf 表单(请注意,出于本演示的目的,潜在的敏感信息已被像素化):

Suitelet 的 NetSuite 银行存款单

就是这样!我希望这对将来的其他人有所帮助。


推荐阅读