Custom field using script to store manufacturer country from item

Script to create a custom field and store the manufacturer country from item record.

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 itemCoo = itemDetails(customRec.id, customRec.type);
        let customForm = scriptContext.form
        let itemData = customForm
          .addField({
            id: "custpage_jj_item_data",
            type: serverWidget.FieldType.TEXTAREA,
            label: "Item Data",
          })
          .updateDisplayType({ displayType: "DISABLED" })
          .setHelpText('This field contains item name and its country of manufacturer details. ' +
            'The values are automatically populated based on items.');
        itemData.defaultValue = JSON.stringify(itemCoo);
      }
    } catch (error) {
      log.error("Error @ beforeLoad", error);
    }
  };
/**
 * @description Function to get manufacture country of item
 * @param {*} recId 
 * @param {*} recType 
 * @returns []
 */
  function itemDetails(recId, recType) {
    try {
      let salesorderSearchObj = search.create({
        type: recType,
        filters:
          [
            ["internalid", "anyof", recId],
            "AND",
            ["taxline", "is", "F"],
            "AND",
            ["shipping", "is", "F"],
            "AND",
            ["mainline", "is", "F"]
          ],
        columns:
          [
            search.createColumn({
              name: "itemid",
              join: "item",
              label: "Name"
            }),
            search.createColumn({
              name: "countryofmanufacture",
              join: "item",
              label: "Manufacturer Country"
            })
          ]
      });
      let itemArray = []
      salesorderSearchObj.run().each(function (result) {
        let recObj = {}
        let itemName = result.getValue({
          name: "itemid",
          join: "item",
          label: "Name"
        });
        let itemOrgn = result.getValue({
          name: "countryofmanufacture",
          join: "item",
          label: "Manufacturer Country"
        });
        recObj.itemName = itemName;
        recObj.itemOrgn = itemOrgn;
        itemArray.push(recObj)
        return true;
      });
      return itemArray;
    }
    catch (error) {
      log.error("Error @ itemDetails", error);
    }
  }
  return { beforeLoad };
});

Leave a comment

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