Populate Memo field value of applied transaction of Bill Payment into check printout

Create new custom field on the bill payment record to store the memo value of transactions applied on it. This custom field is available only in view and print mode.

/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */
define(["N/search", "N/ui/serverWidget"], (search, serverWidget) => {
  /**
   * Defines the function definition that is executed before record is loaded.
   * @param {Object} scriptContext
   * @param {Record} scriptContext.newRecord - New record
   * @param {string} scriptContext.type - Trigger type; use values from the context.UserEventType enum
   * @param {Form} scriptContext.form - Current form
   * @param {ServletRequest} scriptContext.request - HTTP request information sent from the browser for a client action only.
   * @since 2015.2
   */
  const beforeLoad = (scriptContext) => {
    try {
      if (scriptContext.type == "view" || scriptContext.type == "print") {
        let customRec = scriptContext.newRecord;
        let appliedTranArr = getAppliedTransactionDetails(customRec.id);
        let customForm = scriptContext.form;
        log.debug("customForm", customForm);
        let primaryContact = customForm
          .addField({
            id: "custpage_jj_applied_trasactions",
            type: serverWidget.FieldType.TEXTAREA,
            label: "Transaction Memo",
          })
          .updateDisplayType({ displayType: "DISABLED" })
          .setHelpText(
            "This field contains transaction memo and document number details. " +
              "The values are automatically populated based on applied transactions."
          );


        primaryContact.defaultValue = JSON.stringify(appliedTranArr);
        log.debug("field added...");
        //scriptContext.form.clientScriptFileId = 5593035;
      }
    } catch (error) {
      log.error("Error @ beforeLoad", error);
    }
  };


  /**
   * @description Fetch the applied transactions details.
   * @param {number} recId
   * @returns {array} appliedTranArray
   */
  function getAppliedTransactionDetails(recId) {
    try {
      let vendorpaymentSearchObj = search.create({
        type: "vendorpayment",
        settings: [{ name: "consolidationtype", value: "ACCTTYPE" }],
        filters: [
          ["type", "anyof", "VendPymt"],
          "AND",
          ["internalid", "anyof", recId],
          "AND",
          ["mainline", "is", "F"],
          "AND",
          ["appliedtotransaction.mainline", "is", "T"],
        ],
        columns: [
          search.createColumn({
            name: "internalid",
            summary: "GROUP",
            join: "appliedToTransaction",
            sort: search.Sort.ASC,
            label: "Internal ID",
          }),
          search.createColumn({
            name: "tranid",
            summary: "GROUP",
            join: "appliedToTransaction",
            label: "Document Number",
          }),
          search.createColumn({
            name: "memomain",
            summary: "GROUP",
            join: "appliedToTransaction",
            label: "Memo (Main)",
          }),
        ],
      });
      let searchResultCount = vendorpaymentSearchObj.runPaged().count;
      log.debug("vendorpaymentSearchObj result count", searchResultCount);
      let appliedTranArray = [];
      vendorpaymentSearchObj.run().each(function (result) {
        let recObj = {};
        let tranInternalId = result.getValue(vendorpaymentSearchObj.columns[0]);
        let tranMemo = result.getValue(vendorpaymentSearchObj.columns[2]);
        recObj.tranInternalId = tranInternalId;
        recObj.tranMemo = tranMemo;
        appliedTranArray.push(recObj);
        return true;
      });
      return appliedTranArray;
    } catch (error) {
      log.error("Error @ getAppliedTransactionDetails", error);
    }
  }
  return { beforeLoad };
});

Leave a comment

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