When you can’t add details in every fields in Suitelet

Suitelet scripts can be executed with any client scripts. Sometimes in such situations, the suitlet page(interface) mayn’t be working perfectly. There will be some conditions where some fields may not be working or sometime you can’t enter values to all (necessary) fields. In such situations, output also won’t be perfect.

In the client script, almost every entry point expect a return value. Ie; either true or false. So if you add a ‘return true’ statement at the end of every entry point, then this issue can be resolved.

Source Code:

jj_sl_blood_donation_sample.js

/**
 * @NApiVersion 2.1
 * @NScriptType Suitelet
 */
define(['N/ui/serverWidget'],
    /**
 * @param{serverWidget} serverWidget
 */
    (serverWidget) => {
        /**
         * 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{
                var form = serverWidget.createForm({
                    title: 'Blood Donar Details'
                });
                form.clientScriptFileId = 2810;
                if(scriptContext.request.method === 'GET'){

                    var firstName = form.addField({
                        id:'firstname',
                        label:'First Name',
                        type:serverWidget.FieldType.TEXT
                    });

                    var lastName = form.addField({
                        id:'lastname',
                        label:'Last Name',
                        type:serverWidget.FieldType.TEXT
                    });
                    var genderLabel = form.addField({
                        id: 'genderlabel',
                        label:'Gender',
                        type: serverWidget.FieldType.LABEL
                    });

                    var gender1 = form.addField({
                        id: 'gender',
                        type: serverWidget.FieldType.RADIO,
                        label: 'Male',
                        source: 'male'
                    });
                    var gender2 = form.addField({
                        id: 'gender',
                        type: serverWidget.FieldType.RADIO,
                        label: 'Female',
                        source: 'female'
                    });
                    var gender3 = form.addField({
                        id: 'gender',
                        type: serverWidget.FieldType.RADIO,
                        label: 'Others',
                        source: 'others'
                    });
                    var phone = form.addField({
                        id:'phonenum',
                        label:'Contact Number',
                        type: serverWidget.FieldType.PHONE
                    });
                    var bloodGroup = form.addField({
                        id: 'bloodgrp',
                        type: serverWidget.FieldType.SELECT,
                        label: 'Blood Group',
                        source: 'customlist_blood_group'
                    });

                    bloodGroup.defaultValue = 1;
                    var lastDonationdate = form.addField({
                        id:'lastdonationdate',
                        type: serverWidget.FieldType.DATE,
                        label: 'Last Donation Date'
                    });
                    var btn = form.addSubmitButton({
                        label: 'SUBMIT'
                    });

                    scriptContext.response.writePage(form);
                }
                else if(scriptContext.request.method === 'POST') {
                    var fName = scriptContext.request.parameters.firstname;
                    var lName = scriptContext.request.parameters.lastname;
                    var fullName = fName + ' ' + lName;
                    var gender = scriptContext.request.parameters.gender;
                    var phoneNum = scriptContext.request.parameters.phonenum;
                    var blood = scriptContext.request.parameters.inpt_bloodgrp;
                    var donationDate = scriptContext.request.parameters.lastdonationdate;

                    form.addField({
                        id: 'namelabel',
                        type: serverWidget.FieldType.LABEL,
                        label: 'Name'
                    });
                    form.addField({
                        id: 'genderlabel',
                        type: serverWidget.FieldType.LABEL,
                        label: 'Gender'
                    });
                    form.addField({
                        id: 'phonelabel',
                        type: serverWidget.FieldType.LABEL,
                        label: 'Contact Number'
                    });
                    form.addField({
                        id: 'bloodgrplabel',
                        type: serverWidget.FieldType.LABEL,
                        label: 'Blood Group'
                    });
                    form.addField({
                        id: 'donationlabel',
                        type: serverWidget.FieldType.LABEL,
                        label: 'Last Donated Date'
                    });

                    form.addField({
                        id: 'fullname',
                        type: serverWidget.FieldType.LABEL,
                        label: fullName
                    });

                    form.addField({
                        id: 'gender',
                        type: serverWidget.FieldType.LABEL,
                        label: gender
                    });

                    form.addField({
                        id: 'contact',
                        type: serverWidget.FieldType.LABEL,
                        label: phoneNum
                    });

                    form.addField({
                        id: 'blood',
                        type: serverWidget.FieldType.LABEL,
                        label: blood
                    });

                    form.addField({
                        id: 'donationdate',
                        type: serverWidget.FieldType.LABEL,
                        label: donationDate
                    });
                    var btn2 = form.addButton({
                        id: 'cust_btn1',
                        label: 'Alert',
                        functionName: 'showAlert'
                    });
                    scriptContext.response.writePage(form);
                }

                else{
                    log.debug("Other Method");
                }
            }
            catch (e) {
                log.debug("ERROR: ",e.message);
            }
        }

        return {onRequest}

    });

client_script_sample.js

/**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */
define([],

function() {
    
    /**
     * Function to be executed after page is initialized.
     *
     * @param {Object} scriptContext
     * @param {Record} scriptContext.currentRecord - Current form record
     * @param {string} scriptContext.mode - The mode in which the record is being accessed (create, copy, or edit)
     *
     * @since 2015.2
     */
    function pageInit(scriptContext) {
        console.log("Page Loading.......");
        return true;
    }

    /**
     * Validation function to be executed when record is saved.
     *
     * @param {Object} scriptContext
     * @param {Record} scriptContext.currentRecord - Current form record
     * @returns {boolean} Return true if record is valid
     *
     * @since 2015.2
     */
    function saveRecord(scriptContext) {
        console.log("RECORD SAVED");
        return true;
    }

    function showAlert(){
        alert("Saved Successfully");
    }

    return {
        pageInit: pageInit,
        saveRecord: saveRecord,
        showAlert: showAlert
    };
    
});

Leave a comment

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