Hide the Edit button in Sales order if a custom (shipment) record exist in SO.

Check to see if shipment record exists in Sales order and if it is present, hide edit button on SO form for ALL roles EXCEPT

  • Administrator
  • VVS CSR Manager
  • VVS Pharmacy

Solution: Create a workflow and use workflow action script and lock record action to hide the edit button in SO if there exist a shipment record (custom record) in SO

Storing a Return Value from a Custom Action Script in a Workflow Field

  1. Create a new Workflow Action script.

Workflow action Script

/**
 * @NApiVersion 2.1
 * @NScriptType WorkflowActionScript
 */

define(['N/action', 'N/record', 'N/runtime', 'N/search'],
    /**
     * @param{action} action
     * @param{record} record
     * @param{runtime} runtime
     * @param{search} search
     */
    (action, record, runtime, search) => {
        /**
         * Defines the WorkflowAction script trigger point.
         * @param {Object} scriptContext
         * @param {Record} scriptContext.newRecord - New record
         * @param {Record} scriptContext.oldRecord - Old record
         * @param {string} scriptContext.workflowId - Internal ID of workflow which triggered this action
         * @param {string} scriptContext.type - Event type
         * @param {Form} scriptContext.form - Current form that the script uses to interact with the record
         * @since 2016.1
         */
        const onAction = (scriptContext) => {
            try {
                const action = checkIfCustomRecordExists(scriptContext);
                log.debug("action", action);
                return action;

            } catch (e) {
                log.debug("err@onAction", e.message)
            }
        }


        function checkIfCustomRecordExists(scriptContext) {
            if (scriptContext.type === 'view') {
                const currentRecord = scriptContext.newRecord;
                const recordType = currentRecord.type;
                // Check if it's a Sales Order
                if (recordType === 'salesorder') {
                    const salesOrderId = currentRecord.id;
                    const customRecordSearch = search.create({
                        type: 'customrecord_vv_shipment',
                        filters: [
                            ['custrecord_vv_shipment_order', 'anyof', salesOrderId]
                        ],
                        columns: ['internalid']
                    });

                    // Run the search and retrieve the results
                    const searchResults = customRecordSearch.run().getRange({
                        start: 0,
                        end: 1
                    });

                    // Check if any results are returned
                    if (searchResults && searchResults.length > 0) {
                        return 1;

                    } else {
                        return 0;
                    }
                }
            }
        }


        return { onAction };
    });

2. Make sure that the script returns a value. You can specify this on the Parameters tab of the Script record page.

3. In SuiteFlow, create a workflow field. The field should be the of the same type as the return parameter of the Workflow Action script.

4. Within a state, add the custom action (this is the Workflow Action script).

5. The return value from the Workflow Action script can be stored in the Store Result In field.

6. Add the condition in the lock record action in WF.

Leave a comment

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