Call Suitelet from Workflow and set Values.

The workflow calls the Suitelet script and this script set values in the record by fetching the record type and record id.

/**
 * @NApiVersion 2.1
 * @NScriptType Suitelet
 */
define(['N/record', 'N/ui/dialog', 'N/ui/message', 'N/ui/serverWidget','N/http','N/search', 'N/url','N/runtime','N/email'],
    /**
     * @param{record} record
     * @param{dialog} dialog
     * @param{message} message
     * @param{serverWidget} serverWidget
     * @param{http} http
     * @param{search} search
     * @param{url} url
     * @param{email} email
     */
    (record, dialog, message, serverWidget,http,search,url,runtime, email) => {



        function getLocation(href) {
            var match = href.match(/^(https?\:)\/\/(([^:\/?#]*)(?:\:([0-9]+))?)([\/]{0,1}[^?#]*)(\?[^#]*|)(#.*|)$/);
            return match && {
                href: href,
                protocol: match[1],
                host: match[2],
                hostname: match[3],
                port: match[4],
                pathname: match[5],
                search: match[6],
                hash: match[7]
            }
        }

        function getAllUrlParams(url) {
            //https://stackoverflow.com/a/21553982
            // get query string from url (optional) or window
            var queryString = url ? url.split('?')[1] : window?.location?.search?.slice(1);

            // we'll store the parameters here
            var obj = {};

            // if query string exists
            if (queryString) {

                // stuff after # is not part of query string, so get rid of it
                queryString = queryString.split('#')[0];

                // split our query string into its component parts
                var arr = queryString.split('&');

                for (var i = 0; i < arr.length; i++) {
                    // separate the keys and the values
                    var a = arr[i].split('=');

                    // set parameter name and value (use 'true' if empty)
                    var paramName = a[0];
                    var paramValue = typeof (a[1]) === 'undefined' ? true : a[1];

                    // (optional) keep case consistent
                    paramName = paramName.toLowerCase();
                    if (typeof paramValue === 'string') paramValue = paramValue.toLowerCase();

                    // if the paramName ends with square brackets, e.g. colors[] or colors[2]
                    if (paramName.match(/\[(\d+)?\]$/)) {

                        // create key if it doesn't exist
                        var key = paramName.replace(/\[(\d+)?\]/, '');
                        if (!obj[key]) obj[key] = [];

                        // if it's an indexed array e.g. colors[2]
                        if (paramName.match(/\[\d+\]$/)) {
                            // get the index value and add the entry at the appropriate position
                            var index = /\[(\d+)\]/.exec(paramName)[1];
                            obj[key][index] = paramValue;
                        } else {
                            // otherwise add the value to the end of the array
                            obj[key].push(paramValue);
                        }
                    } else {
                        // we're dealing with a string
                        if (!obj[paramName]) {
                            // if it doesn't exist, create property
                            obj[paramName] = paramValue;
                        } else if (obj[paramName] && typeof obj[paramName] === 'string'){
                            // if property does exist and it's a string, convert it to an array
                            obj[paramName] = [obj[paramName]];
                            obj[paramName].push(paramValue);
                        } else {
                            // otherwise add the property
                            obj[paramName].push(paramValue);
                        }
                    }
                }
            }

            return obj;
        }





        /**
         * Defines the Suitelet script trigger point.
         * @param {Object} scriptContext
         * @param {ServerRequest} scriptContext.request - Incoming request
         * @param {ServerResponse} scriptContext.response - Suitelet response
         * @since 2015.2
         */
        const onRequest = (scriptContext) => {
            try {
                let form = serverWidget.createForm({
                    title: 'Reject Reason',
                });

                //todo
                if (scriptContext.request.method == 'POST') {

                    log.debug("Param in post",scriptContext.request.parameters)
                    let recID= scriptContext.request.parameters.custpage_recordid;
                    log.debug("Rec Id in Post",recID);
                    let reason=scriptContext.request.parameters.custpage_rejectreason;
                    log.debug("Reject Reason",reason);
                    let type= scriptContext.request.parameters.custpage_rectype;
                    log.debug("Type",type)

                    let submitToField = record.submitFields({
                        type: 'customrecord_jj_cust_item_rec',
                        id: recID,
                        values: {
                            custrecord_jj_rejection_reason: reason,
                            custrecord_jj_approvalstatus: 3
                        }
                    });

                } else {
                    let currentRecordid = scriptContext.request.parameters;
                    let header = scriptContext.request.headers
                    log.debug("header", header)

                    let url_string = header["referer"]
                    log.debug("url_string", url_string)
                    let refererURL = getAllUrlParams(url_string);
                    log.debug('refererURL',refererURL);
                    let recID = refererURL.id;

                    let urlBreakDown = getLocation(url_string);
                    log.debug('urlBreakDown',urlBreakDown);
                    let pathName = urlBreakDown.pathname;
                    log.debug("pathName",pathName)
                    //let recordType = pathName.split('/app/accounting/transactions/')[1];
                    let recordType =  pathName.toString().trim().split('/');
                    recordType = recordType[recordType.length-1];
                    log.debug("recordType",recordType)

                    form.addFieldGroup({
                        id: 'head',
                        label: 'Bassic Details'
                    });
                    let message= form.addField({
                        id: 'custpage_rejectreason',
                        label: 'Reject Reason',
                        type: serverWidget.FieldType.TEXTAREA,
                        container: 'head'
                    });
                    message.isMandatory=true;
                    let recordID = form.addField({
                        id: 'custpage_recordid',
                        label: 'Record ID',
                        type: serverWidget.FieldType.INLINEHTML,
                        container: 'head',
                    });
                    let recType=form.addField({
                        id: 'custpage_rectype',
                        label: 'Record Type',
                        type: serverWidget.FieldType.TEXT,
                        container: 'head'
                    });
                    recType.updateDisplayType({
                        displayType : serverWidget.FieldDisplayType.HIDDEN
                    })
                    recordID.updateDisplayType({
                        displayType : serverWidget.FieldDisplayType.HIDDEN
                    })
                    recType.defaultValue=recordType;
                    recordID.defaultValue=recID;
                    form.addSubmitButton({
                        label: 'Submit'
                    });

                    scriptContext.response.writePage(form);
                }


            } catch (e) {
                log.error({
                    title: e.name,
                    details: e
                });
            }

        }

        return {onRequest}

    });

Leave a comment

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