Adding Planogram Button in customer Record

Requirement

We need to create a Planogram button in customer record.

If the customer record doesn’t have a planogram custom record entry yet, we need to set show a button .If not hide the button.

On button click, we need to show a planogram and create a page in a new tab with the customer name auto populated. After saving that custom record entry page, then navigate to the customer record. Button to be shown only in View mode of customer record.

Solution

Userevent script to Add a button in view mode in custome rrecord

/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */
define(['N/currentRecord', 'N/record', 'N/search'],
    /**
 * @param{currentRecord} currentRecord
 * @param{record} record
     * @param{search} search
 */
    (currentRecord, record,search) => {
        /**
         * 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
         */
        const beforeLoad = (scriptContext) => {
            try{
                if(scriptContext.type==="view"){
                    scriptContext.form.clientScriptFileId=24056

                    var customerRec = scriptContext.newRecord;
                    log.debug('customer Record',customerRec)
                    log.debug('customer Record id',customerRec.id)
                    let custRecId=customerRec.id
                    var checkPlanogramRecExist=checkForPlanogramRec(custRecId)
                    log.debug('checkPlanogramRecExist',checkPlanogramRecExist)
                   if(!checkPlanogramRecExist){
                        scriptContext.form.addButton({
                            id :'custpage_addplanogram',
                            label : 'Planogram',
                            functionName:'addPlanogramRec("'+custRecId+'")'
                        })
                  }

                }

            }
            catch (e) {
                log.debug('Error@ BeforeLoad',e)
            }

        }

        /**
         * Defines the function definition that is executed before record is submitted.
         * @param {Object} scriptContext
         * @param {Record} scriptContext.newRecord - New record
         * @param {Record} scriptContext.oldRecord - Old record
         * @param {string} scriptContext.type - Trigger type; use values from the context.UserEventType enum
         * @since 2015.2
         */
        const beforeSubmit = (scriptContext) => {

        }

        /**
         * Defines the function definition that is executed after record is submitted.
         * @param {Object} scriptContext
         * @param {Record} scriptContext.newRecord - New record
         * @param {Record} scriptContext.oldRecord - Old record
         * @param {string} scriptContext.type - Trigger type; use values from the context.UserEventType enum
         * @since 2015.2
         */
        const afterSubmit = (scriptContext) => {

        }

        function checkForPlanogramRec(customerRecId){
          try{
              var customerSearchObj = search.create({
                  type: "customer",
                  filters:
                      [
                          ["internalid","anyof",customerRecId],
                          "AND",
                          ["custrecord_jj_cust_plngrm_parent.idtext","isnotempty",""]
                      ],
                  columns:
                      [
                          search.createColumn({
                              name: "name",
                              join: "CUSTRECORD_JJ_CUST_PLNGRM_PARENT",
                              label: "ID"
                          })
                      ]
              });
              var searchResultCount = customerSearchObj.runPaged().count;
              var res=customerSearchObj.run()
              log.debug('res',res)
              log.debug('searchResultCount',searchResultCount)
              if(searchResultCount>0){
                  return true
              }
              else
              {
                  return false
              }
          }
          catch (e) {
              log.debug('Error@checkForPlanogramRec',e)
          }
        }

        return {beforeLoad}

    });

Clientscript to open a new custom record in new tab

/**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */

define(['N/currentRecord', 'N/record', 'N/search'],
    /**
     * @param{currentRecord} currentRecord
     * @param{record} record
     * @param{search} search
     */

    function (currentRecord, record, search) {


        function addPlanogramRec(custId) {
            try {

                var newPlanoUrl = "https://6687177-sb1.app.netsuite.com/app/common/custom/custrecordentry.nl?rectype=1182";   //Custom function to get new standard Sales Order URL

                var finalUrl = newPlanoUrl + '&custId=' + custId;

                window.open(finalUrl, "_blank");
                return true;
            } catch (e) {
                log.error("Error @addPlanogramRec", e);

            }
        }



        function pageInit(scriptContext) {
            try {

                var currentContext = scriptContext.mode
                
                //Get current record
                var currentRec = scriptContext.currentRecord
                var params = window.location.href

                //Get parent soid from the URL
                var getCId = params.split('&custId=')
                var custId = getCId[1]
                
                if (currentContext === 'create' && checkForParameter(custId)) {
                    currentRec.setValue({fieldId: "custrecord_jj_cust_plngrm_parent", value: custId});
                }


            } catch (e) {
                console.log('Error@pageInit', e)
            }

        }
        function saveRecord(scriptContext) {
            try{
                var params = window.location.href
           
                var getCId = params.split('&custId=')
             
                var currRec=scriptContext.currentRecord


                if(checkForParameter(getCId[1])){
   

                 var id= currRec.setValue({
                        fieldId: 'custrecord_jj_check_planogramrec',
                        value: true,
                        ignoreFieldChange: true,
                        forceSyncSourcing: true
                    });
log.debug('id',id)

                }
                return true
            }
            catch (e) {
                log.debug('Error@saveRecord',e)
            }
        }


        function checkForParameter(parameter) {

            if (parameter !== "" && parameter !== null && parameter !== undefined && parameter !== false && parameter !== "null" && parameter !== "undefined" && parameter !== " " && parameter !== 'false' && parameter !== '- None -') {

                return true;

            }

        }


        return {
            pageInit: pageInit,
            addPlanogramRec: addPlanogramRec,
            saveRecord:saveRecord
        };

    }
)

Userevent script to close the new custom record after its save and return back to customer record

/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */
define(['N/currentRecord', 'N/record', 'N/runtime','N/ui/serverWidget'],
    /**
 * @param{currentRecord} currentRecord
 * @param{record} record
 * @param{runtime} runtime
     * @param{serverWidget} serverWidget
 */
    (currentRecord, record, runtime,serverWidget) => {
        /**
         * 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
         */
        const beforeLoad = (scriptContext) => {

            try{
                var rec=scriptContext.newRecord
                var planoRec=record.load({
                    type:'customrecordplgrm_jj_hgl',
                    id: rec.id,
                    isDynamic: false,
                });

                let checkValue=rec.getValue({
                    fieldId:'custrecord_jj_check_planogramrec'
                })
               if (runtime.executionContext === runtime.ContextType.USER_INTERFACE){
                        if(checkValue===true){
                   
   let closeWindow = '<html><body><script type="text/javascript">window.close();</script></body></html>'
                        var fieldValue = scriptContext.form.addField({
                            id: 'custpage_hiddenfield',
                            type: serverWidget.FieldType.INLINEHTML,
                            label: 'Text'
                        });
                        let id=planoRec.setValue({
                            fieldId:'custrecord_jj_check_planogramrec',
                            value:false
                        })
                            planoRec.save()
                            log.debug('id',id)
                        fieldValue.defaultValue = closeWindow;

                    }
                }

            }
            catch (e) {
                log.debug('Error@beforeLoad',e)
            }
        }

        
        return {beforeLoad}

    });

Leave a comment

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