Hide Body Fields and Sublist Columns

When a transaction loads in the UI (Create/ Edit/ View), the following sample can:

For the complete tutorial, see Hide Body Fields and Sublist Columns on Transaction Forms.

Important:

This sample uses SuiteScript 2.1. For more information, see SuiteScript 2.1.

          /**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */
define(['N/runtime'], (runtime) => {
  /*
   * Parameter constraints accommodated:
   * - Labels <= 25 chars (you choose labels in UI; suggestions below)
   * - IDs <= 30 chars (the IDs below are <= 30)
   *
   * Create these PARAMETERS on the Script record (then set per deployment):
   *
   * 1) Roles (optional)
   *    ID: custscript_hf_roles
   *    Example: 1019,1050
   *
   * 2) Forms (optional)
   *    ID: custscript_hf_forms
   *    Example: 123,456
   *
   * 3) Body Flds (optional)
   *    ID: custscript_hf_body
   *    Example: memo,custbody_myfield
   *
   * 4) Sublists (optional)
   *    ID: custscript_hf_sublists
   *    Example: item,expense
   *
   * 5) Sublist Flds (optional)
   *    ID: custscript_hf_subflds
   *    Example: taxdetailsreference,custcol_mycol
   */

  const P_ROLES   = 'custscript_hf_roles';
  const P_FORMS   = 'custscript_hf_forms';
  const P_BODY    = 'custscript_hf_body';
  const P_SUBLSTS = 'custscript_hf_sublists';
  const P_SUBFLDS = 'custscript_hf_subflds';

  const DEFAULT_SUBLISTS = ['item', 'expense', 'line', 'apply', 'credit', 'payment', 'billing'];

  const parseCsv = (val) =>
    String(val || '')
      .split(',')
      .map(s => s.trim())
      .filter(Boolean);

  const parseCsvNums = (val) =>
    parseCsv(val)
      .map(v => Number(v))
      .filter(n => Number.isFinite(n));

  const beforeLoad = (context) => {
    // UI only
    const allowedTypes = [context.UserEventType.CREATE, context.UserEventType.EDIT, context.UserEventType.VIEW];
    if (!allowedTypes.includes(context.type)) return;

    const script = runtime.getCurrentScript();

    // Role filter (optional)
    const allowedRoles = parseCsvNums(script.getParameter({ name: P_ROLES }));
    if (allowedRoles.length) {
      const currentRole = Number(runtime.getCurrentUser().role);
      if (!allowedRoles.includes(currentRole)) return;
    }

    // Form filter (optional) - transactions typically use "customform"
    const allowedForms = parseCsvNums(script.getParameter({ name: P_FORMS }));
    if (allowedForms.length) {
      const formId = Number(context.newRecord.getValue({ fieldId: 'customform' }));
      if (!allowedForms.includes(formId)) return;
    }

    const form = context.form;

    // Hide body fields
    const bodyFieldIds = parseCsv(script.getParameter({ name: P_BODY }));
    bodyFieldIds.forEach((fieldId) => {
      try {
        const fld = form.getField({ id: fieldId });
        if (fld) fld.updateDisplayType({ displayType: 'HIDDEN' });
      } catch (e) {
        // ignore
      }
    });

    // Hide sublist columns
    const sublistFieldIds = parseCsv(script.getParameter({ name: P_SUBFLDS }));
    if (!sublistFieldIds.length) return;

    const configuredSublists = parseCsv(script.getParameter({ name: P_SUBLSTS }));
    const sublistsToCheck = configuredSublists.length ? configuredSublists : DEFAULT_SUBLISTS;

    sublistsToCheck.forEach((sublistId) => {
      let sublist;
      try {
        sublist = form.getSublist({ id: sublistId });
      } catch (e) {
        return;
      }
      if (!sublist) return;

      sublistFieldIds.forEach((fieldId) => {
        try {
          const fld = sublist.getField({ id: fieldId });
          if (fld) fld.updateDisplayType({ displayType: 'HIDDEN' });
        } catch (e) {
          // ignore
        }
      });
    });
  };

  return { beforeLoad };
}); 

        

General Notices