Friday 5 July 2019

How to create a custom page (form) using suitelets ?


-          ->suitelets are extensions of the SuiteScript API that allow you to build custom NetSuite pages and backend logic.

-          ->Suitelets are server-side scripts that operate in a request-response model, and are invoked by HTTP GET or POST requests to system generated URLs.
-          Using suitelets we can create custom pages like forms, assistant pages , linked pages.
Custom Page Components:
We need to know below 4 main components before we create custom NetSuite Pages:
1.       Page Type: This determines how the information should display on the screen.
2.       Object Members: these are the elements those needs to be added to the screen.
3.       Member Properties: Each member object can be modified by changing it’s properties like hiding or making mandatory.
4.       Page Layout: This controls how each of the member objects arranged on the page.

Here is an example of creating a custom netsuite form using suitelets:

I have created an employee on boarding page, where I’m capturing some information of employee and with that I’m creating employee record, and after clicking on the submit button navigating to the employee record whose employee details are captured in on boarding kit.

Below is the sample code :

/**

 * @NApiVersion 2.x
 * @NScriptType Suitelet
 * @NModuleScope SameAccount
 */
define(['N/email', 'N/record', 'N/redirect', 'N/ui/serverWidget'],
/**
 * @param {email} email
 * @param {record} record
 * @param {redirect} redirect
 * @param {serverWidget} serverWidget
 */
function(email, record, redirect, serverWidget) {
   
    /**
     * Definition of the Suitelet script trigger point.
     *
     * @param {Object} context
     * @param {ServerRequest} context.request - Encapsulation of the incoming request
     * @param {ServerResponse} context.response - Encapsulation of the Suitelet response
     * @Since 2015.2
     */
    function onRequest(context) {
   
    var request = context.request;
    var response  = context.response;
   
    if (request.method == 'GET'  ) {
   
    // title of the page
        var form = serverWidget.createForm ({
       
        title : 'Employee On-Boarding'
        });
       
        // field Groups in the page
        var empInfo = form.addFieldGroup ({
       
        id : 'custpage_empinfo',
        label : 'Employee Information'
       
        });
       
        var meetwSup =  form.addFieldGroup ({
       
        id : 'custpage_meetwsup',
        label : 'Meeting with Supervisor'
       
        });
       
        var welEmail = form.addFieldGroup ({
       
        id : 'custpage_wecmem',
        label : 'Welcome Email',
       
        });
       
        var firstName = form.addField({
       
        id : 'custpage_firstname',
        label : 'First Name',
        type : serverWidget.FieldType.TEXT,
        container : 'custpage_empinfo'
       
        });
       
        firstName.isMandatory = true;
       
        var midlName = form.addField({
       
        id : 'custpage_middlename',
        label : 'Middle Name',
        type : serverWidget.FieldType.TEXT,
        container : 'custpage_empinfo'
       
        });
       
        var lastName = form.addField({
       
        id : 'custpage_lastname',
        label : 'Last Name',
        type : serverWidget.FieldType.TEXT,
        container : 'custpage_empinfo'
       
        });
       
        lastName.isMandatory = true;
       
        var email = form.addField({
       
        id : 'custpage_email',
        label : 'Email',
        type : serverWidget.FieldType.EMAIL,
        container : 'custpage_empinfo'
       
        });
        
        var supervisor = form.addField({
       
        id : 'custpage_supervisor',
        label : 'Supervisor',
        type : serverWidget.FieldType.SELECT,
        container : 'custpage_empinfo',
        source : 'employee'
       
        });
       
        supervisor.isMandatory = true;
       
        var subs = form.addField({
       
        id : 'custpage_subsidiary',
        label : 'Subsidiary',
        type : serverWidget.FieldType.SELECT,
        source : 'subsidiary',
        container : 'custpage_empinfo'
       
        });
       
        subs.isMandatory = true;
       
        var fldTitle = form.addField({
       
        id : 'custpage_title',
        label : 'Title',
        type : serverWidget.FieldType.TEXT,
        container : 'custpage_meetwsup'
       
        });
       
       
        fldTitle.isMandatory = true;
       
        var fldMessage = form.addField({
       
        id : 'custpage_message',
        label : 'Message',
        type : serverWidget.FieldType.TEXTAREA,
        container : 'custpage_meetwsup'
       
        });
       
        fldMessage.isMandatory = true;
       
        var fldWelTitle = form.addField({
       
        id : 'custpage_weltitle',
        label : 'Title',
        type : serverWidget.FieldType.TEXT,
        container : 'custpage_wecmem'
       
        });
       
        fldWelTitle.isMandatory = true;
       
        var fldWelMessage = form.addField({
       
        id : 'custpage_welmessage',
        label : 'Message',
        type : serverWidget.FieldType.TEXTAREA,
        container : 'custpage_wecmem'
       
        });
       
        fldWelMessage.isMandatory = true;
       
        // update the page layout
        firstName.updateBreakType({
        breakType : serverWidget.FieldBreakType.STARTCOL
        });
       
        midlName.updateBreakType({
        breakType : serverWidget.FieldBreakType.STARTROW
        });
       
        lastName.updateBreakType({
        breakType : serverWidget.FieldBreakType.STARTROW
    });
       
//update the field display type
        midlName.updateDisplaySize({
        height : 0,
        width : 10
        });
       
        fldMessage.updateDisplaySize({
        height : 12,
        width : 60
        });
       
        fldWelMessage.updateDisplaySize({
        height : 12,
        width : 60
        });
           
        email.updateBreakType({
        breakType : serverWidget.FieldBreakType.STARTCOL
        });
       

        fldTitle.defaultValue = 'Welcome meeting with your Supervisor';
        fldMessage.defaultValue = 'Meet and Greet your supervisor and Team';
       
        fldWelTitle.defaultValue = 'Welcome to smartERP!';
        fldWelMessage.defaultValue = "Hi,"+'\n'+'\n'+"We'd like to welcome you to smartERP. Please feel to reachout us to any questions."+'\n'+'\n'+'Best Regards,'+'\n'+'smartERP HR';

        var button = form.addSubmitButton({
        label : 'Process Employee Details'
        });
       
        context.response.writePage(form);
    } else { // POST method
    // get the field details and store on variables
    var fName = request.parameters.custpage_firstname;
    var mName = request.parameters.custpage_middlename;
    var lName = request.parameters.custpage_lastname;
    var empEmail = request.parameters.custpage_email;
    var sprvsr = request.parameters.custpage_supervisor;
    var subs = request.parameters.custpage_subsidiary;
   
// Create the employee record using the above parameters
    var employeeObj = record.create({
   
    type : record.Type.EMPLOYEE,
    isDynamic : true,
   
    });
   
    employeeObj.setValue('firstname', fName);
    employeeObj.setValue('lastname', lName);
    employeeObj.setValue('email', empEmail);
    employeeObj.setValue('supervisor', sprvsr);
    employeeObj.setValue('subsidiary', subs);
   
// save the employee record
    var empID = employeeObj.save();
   
//go to the employee record which is created.
    redirect.toRecord ({
   
    type : record.Type.EMPLOYEE,
    id   : empID,
    isEditMode : true
    });
    }
   
    }

    return {
        onRequest: onRequest
    };
    
});

Before Submitting :
:


No comments:

Post a Comment