Recurring POs Using Workflow Action script.

To include the Parent PO record on the copied Record when the user creates a PO using Make a Copy and reduce the recurrence count by one whenever a user creates a Copy PO record by Make a Copy. please reduce this integer by 1 every time a user makes a copy of the PO in the case below, the value is 12. after the user saves the copy, pls change the number to 11.

Solution:

We need to create workflow for recurring PO

In the workflow , add custom action using workflow action script in state1.

Source PO action: we need to give the trigger type is before record load and copy event.

/**
 * @NApiVersion 2.1
 * @NScriptType WorkflowActionScript
 */
/*******************************************************************************
 * CLIENT NAME:QIMA GROUP
 * QG-273
 * QG Project
 *******************************************************************************
 *
 * Author: Jobin & Jismi IT Services LLP
 * Script Description :
 * To apply internal control and the script that fills in the following fields when the user triggers the Make Copy
 * Date created :19-MAy-2022
 * Created by: VAIRAMUTHU, Jobin & Jismi IT Services LLP
 * REVISION HISTORY
 * Revision 2.0
 *
 ******************************************************************************/
define(['N/record'],
    /**
     * @param{record} record
     */
    (record) => {
        /**
         * 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
            {
                if (scriptContext.type === 'copy')
                {
                    const newRecord =scriptContext.newRecord;
                    let a = newRecord.getValue({fieldId:'entryformquerystring'});
                    let b = a.split('&');
                    let parentRecordId = b[0].split('=');
                    log.debug({title:'parentRecordId' ,details: parentRecordId[1]});
                    newRecord.setValue({fieldId: "custbody_source_po",value: parentRecordId[1]});
                }
            }
            catch(error)
            {
                log.debug({title:"Error in beforeLoad",details: error});
            }
        }

        return {onAction};
    });

Recurrence No action : we need to give the trigger type is after record submit and create event.

/**
 * @NApiVersion 2.1
 * @NScriptType WorkflowActionScript
 */
/*******************************************************************************
 * CLIENT NAME:QIMA GROUP
 * QG-273
 * QG Project
 *******************************************************************************
 *
 * Author: Jobin & Jismi IT Services LLP
 * Script Description :
 * To apply internal control and the script that fills in the following fields when the user triggers the Make Copy
 * Date created :19-MAy-2022
 * Created by: VAIRAMUTHU, Jobin & Jismi IT Services LLP
 * REVISION HISTORY
 * Revision 2.0
 *
 ******************************************************************************/
define(['N/record'],
    /**
 * @param{record} record
 */
    (record) => {
        /**
         * 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 copyRecord = scriptContext.newRecord;
                let parentRecordId = copyRecord.getValue({fieldId: 'custbody_source_po'});
                log.debug({title: 'parentRecordId', details: parentRecordId});
                let parentRecord = record.load({type: record.Type.PURCHASE_ORDER, id: parentRecordId, isDynamic: true,});
                let recurringNumber = parentRecord.getValue({fieldId:'custbody_recurrence_num'})
                log.debug({title: 'recurringNumber', details: recurringNumber});
                if (recurringNumber > 0)
                {
                    recurringNumber = recurringNumber - 1;
                    log.debug({title: "Recurrence Number of Parent PO Reduced by 1", details: recurringNumber});
                    record.submitFields({
                        type: record.Type.PURCHASE_ORDER,
                        id: parentRecordId,
                        values: {custbody_recurrence_num: recurringNumber},
                        options: {enableSourcing: false, ignoreMandatoryFields: false}
                    });
                }
                copyRecord.setValue({fieldId: "custbody_recurrence_num", value: 0});
            }
            catch (error)
            {
                log.debug({title:"Error in afterSubmit",details: error});
            }
        }
        return {onAction};
    });

Leave a comment

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