Suitelet script to trigger user event script

Suitelet script is used to trigger user event script.

User Event script

define(['N/record', 'N/url', 'N/https'],
    /**
 * @param{record} record
 */
    (record, url, https) => {
        /**
         * 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
         */
        function afterSubmit(context) {
            if (context.type !== context.UserEventType.EDIT) return;
            try {
                let poRecord = context.newRecord;
                let startLandedCost = poRecord.getValue('custbody_po_startlanded');
                if (!startLandedCost) return;
                let poId = poRecord.id;
                log.debug('Calling Suitelet', `Processing PO ID: ${poId}`);
                let suiteletUrl = url.resolveScript({
                    scriptId: 'customscript_jj_sl_start_lan_cst_kimhn91', // Update with your Suitelet Script ID
                    deploymentId: 'customdeploy_jj_sl_start_lan_cst_kimhn91', // Update with your Suitelet Deployment ID
                    returnExternalUrl: true,
                    params: { poId: poId },
                    
                });
                let response = https.get({ url: suiteletUrl });
                log.debug('Suitelet Response', response.body);
            } catch (error) {
                log.error('Error in afterSubmit', error.message);
            }
        }
        return {
            afterSubmit: afterSubmit
        };
    });

Suitelet script

define(['N/record', 'N/search', 'N/log'], (record, search, log) => {

    function onRequest(context) {
        if (context.request.method !== 'GET') return;
        try {
            let poId = context.request.parameters.poId;
            if (!poId) {
                log.error('Missing PO ID', 'No PO ID provided.');
                return;
            }
            log.debug('Suitelet Processing', `Processing Item Receipts for PO ID: ${poId}`);
            let irSearch = search.create({
                type: 'itemreceipt',
                filters: [
                    ['createdfrom', 'anyof', poId],
                    "AND",
                    ["mainline", "is", true]
                ],
                columns: ['internalid']
            });
            let irIds = [];
            irSearch.run().each(result => {
                irIds.push(result.getValue('internalid'));
                return true;
            });
            if (irIds.length === 0) {
                log.debug('No IRs Found', `No Item Receipts linked to PO ID: ${poId}`);
                return;
            }
            irIds.forEach(irId => {
                try {
                    let irRecord = record.load({
                        type: 'itemreceipt',
                        id: irId,
                        isDynamic: true
                    });
                    let savedId = irRecord.save({
                        enableSourcing: true,
                        ignoreMandatoryFields: true
                    });
                    log.audit('Item Receipt Updated', `Successfully refreshed IR ID: ${savedId}`);
                } catch (error) {
                    log.error('Error Processing IR', `IR ID: ${irId} - ${error.message}`);
                }
            });
        } catch (error) {
            log.error('Suitelet Error', error.message);
        }
    }


    return { onRequest };
});

Leave a comment

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