Creating any type of record with the details fetched from the newly created custom form using suitelet script

In Suitelet script the code is given below, when the custom form is filled the customer record is successfully created, any kind of records or custom record also we can able to create using this logic by editing the code as per the required fields.

/**

 * @NApiVersion 2.1

 * @NScriptType Suitelet

 */

define([‘N/record’, ‘N/search’, ‘N/ui/serverWidget’],

    /**

 * @param{record} record

 * @param{search} search

 * @param{serverWidget} serverWidget

 */

    (record, search, 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{

                if(scriptContext.request.method === ‘GET’){

                    //Creating form

                    let form = serverWidget.createForm({

                        title : “Customer Information Form”

                    });

                    //Adding fields

                    let fieldGroup = form.addFieldGroup({

                        id : ‘custpage_fieldgroup’,

                        label : “Primary Information”

                    });

                    let name = form.addField({

                        id : ‘custpage_name’,

                        type : serverWidget.FieldType.TEXT,

                        label : “Name : “,

                        container : ‘custpage_fieldgroup’

                    });

                    name.isMandatory = true;

                    let email = form.addField({

                        id : ‘custpage_email’,

                        type : serverWidget.FieldType.TEXT,

                        label : “Email : “,

                        container : ‘custpage_fieldgroup’

                    });

                    let phone = form.addField({

                        id : ‘custpage_phone’,

                        type : serverWidget.FieldType.TEXT,

                        label : “Phone Number : “,

                        container : ‘custpage_fieldgroup’

                    });

                    let salesRep = form.addField({

                        id : ‘custpage_salesrep’,

                        type : serverWidget.FieldType.SELECT,

                        label : “Sales Rep : “,

                        container : ‘custpage_fieldgroup’

                    });

                    salesRep.addSelectOption({

                        value : ,

                        text :

                    });

                    //Creating Search for sales rep

                    let salesrepSearch = search.create({

                        type : search.Type.EMPLOYEE,

                        filters : [‘salesrep’,‘is’,‘T’],

                        columns : [‘entityid’]

                    });

                    //Run search

                    let runSearch = salesrepSearch.run();

                    //Iterate

                    runSearch.each(function(result){

                        let salesRepName = result.getValue({name : ‘entityid’});

                        salesRep.addSelectOption({

                            value : salesRepName,

                            text : salesRepName

                        });

                        return true;

                    })

                    let subsidiary = form.addField({

                        id : ‘custpage_subsidiary’,

                        type : serverWidget.FieldType.SELECT,

                        label : “Subsidiary : “,

                        container : ‘custpage_fieldgroup’

                    });

                    subsidiary.isMandatory = true;

                    subsidiary.addSelectOption({

                        value : ,

                        text :

                    })

                    //Creating search

                    let subsidiarySearch = search.create({

                        type : search.Type.SUBSIDIARY,

                        columns : [‘name’]

                    });

                    //Run the search

                    let searchRun = subsidiarySearch.run();

                    //iterate

                    searchRun.each(function(result){

                        let subsname = result.getValue({name : ‘name’});

                        subsidiary.addSelectOption({

                            value : subsname,

                            text : subsname

                        });

                        return true;

                    })

                    form.addSubmitButton({

                        label : ‘Submit’

                    })

                    scriptContext.response.writePage(form);

                }

                else{

                    // Getting the details in POST server

                    let name = scriptContext.request.parameters.custpage_name;

                    let email = scriptContext.request.parameters.custpage_email;

                    let phone = scriptContext.request.parameters.custpage_phone;

                    let salesRep = scriptContext.request.parameters.custpage_salesrep;

                    let subsidiary = scriptContext.request.parameters.custpage_subsidiary;

                   // Create Customer record

                    let customerRecord = record.create({

                        type : record.Type.CUSTOMER,

                        idDynamic : true

                    });

                    customerRecord.setValue(‘companyname’,name);

                    customerRecord.setValue(’email’,email);

                    customerRecord.setValue(‘phone’,phone);

                    customerRecord.setText(‘salesrep’,salesRep);

                    customerRecord.setText(‘subsidiary’,subsidiary);

                    let saveRec = customerRecord.save({

                        enableSourcing: true,

                        ignoreMandatoryFields: true

                });

               

                    //Displaying

                    let outputMessage = `Customer Information Submitted:<br>`;

                    outputMessage += `companyname : ${name}<br>`;

                    outputMessage += `email : ${email}<br>`;

                    outputMessage += `phone : ${phone}<br>`;

                    outputMessage += `salesrep : ${salesRep}<br>`;

                    outputMessage += `subsidiary : ${subsidiary}<br>`

                    outputMessage += `internal Id : ${saveRec} `;

                    scriptContext.response.write(outputMessage);

                }

            }catch(e){

                log.error(“Form not created :”,e.message);

            }

        }

       

        return {onRequest}

    });

Leave a comment

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