Approve custom record using suitescrip

In NetSuite, the N/action module’s action.find function is used to locate a specific action (e.g., a custom button, workflow action, or standard action) on a record. This is particularly useful in Client Scripts or Suitelets when you need to dynamically check if an action exists or retrieve its details before performing operations, such as enabling/disabling a button or triggering an action programmatically.

The action.find function helps you verify if a specific action (e.g., a custom button with an ID like custpage_approve_button) exists on the form. This is useful for:

  • Checking if the “Approve” button is available before manipulating it.
  • Ensuring the script doesn’t throw errors if the action is missing (e.g., due to role permissions or form customization).
  • Conditionally enabling/disabling the button based on the record’s state.

Below is an updated Client Script that uses action.find to locate the “Approve Record” button and handle the approval process for a custom record.

Sample Client Script with action.find

/**

 * @NApiVersion 2.x

 * @NScriptType ClientScript

 * @NModuleScope SameAccount

 */

define([‘N/currentRecord’, ‘N/ui/message’, ‘N/action’], function(currentRecord, message, action) {

  // Function to handle approval logic

  function approveRecord() {

    try {

      var rec = currentRecord.get();

      // Update approval status

      rec.setValue({

        fieldId: ‘custrecord_approval_status’,

        value: ‘Approved’ // Replace with your status value or internal ID

      });

      rec.setValue({

        fieldId: ‘custrecord_approval_date’,

        value: new Date()

      });

      rec.setValue({

        fieldId: ‘custrecord_approver’,

        value: rec.getValue(‘owner’) // Or current user ID

      });

      // Save the record

      rec.save({

        enableSourcing: true,

        ignoreMandatoryFields: false

      });

      // Show success message

      message.create({

        title: ‘Success’,

        message: ‘Record approved successfully!’,

        type: message.Type.CONFIRMATION

      }).show({ duration: 5000 });

      // Disable the Approve button after approval

      var approveButton = action.find({

        id: ‘custpage_approve_button’,

        recordType: rec.type

      });

      if (approveButton) {

        document.getElementById(‘custpage_approve_button’).disabled = true;

      }

    } catch (e) {

      // Show error message

      message.create({

        title: ‘Error’,

        message: ‘Error approving record: ‘ + e.message,

        type: message.Type.ERROR

      }).show({ duration: 5000 });

    }

  }

  // Page initialization to add and validate the button

  function pageInit(context) {

    var rec = context.currentRecord;

    var form = context.form;

    // Add custom Approve button

    form.addButton({

      id: ‘custpage_approve_button’,

      label: ‘Approve Record’,

      functionName: ‘approveRecord’

    });

    // Use action.find to check if the button exists

    var approveButton = action.find({

      id: ‘custpage_approve_button’,

      recordType: rec.type

    });

    if (approveButton) {

      // Disable button if record is already approved

      if (rec.getValue(‘custrecord_approval_status’) === ‘Approved’) {

        document.getElementById(‘custpage_approve_button’).disabled = true;

      }

    } else {

      // Log or handle case where button is not found

      message.create({

        title: ‘Warning’,

        message: ‘Approve button not found. Contact your administrator.’,

        type: message.Type.WARNING

      }).show({ duration: 5000 });

    }

  }

  return {

    pageInit: pageInit,

    approveRecord: approveRecord

  };

});

Leave a comment

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