Wednesday, October 24, 2018

Send a POST request from a Suitelet to a Restlet

Thisexample creates a Suitelet that submits data into a RESTlet to create acustomer record. You could use several JavaScript packages available onthe internet to convert JavaScript objects into JSON string easily.




function requestFromForm(request,response){

    if(request.getMethod() == 'GET'){

        //create a form where userscould enter data

        var form =nlapiCreateForm('Simple Form');

        var companyname =form.addField('companyname','text','Company:');





        //when form is submittedcreate and send the request to the Restlet


            //URL ofRestlet

            var url ="";


           //Authorization header

            varauthorization = "NLAuthnlauth_account=TSTDRV12345,,nlauth_signature=*******,nlauth_role=3";


            //Createthe request headers

            varheaders = new Array();

           headers['Content-Type'] = 'application/json';

           headers['Authorization'] = authorization;

           headers['User-Agent-x'] = 'SuiteScript Call';


            //obtainthe values entered by the user

            varcompanyname = request.getParameter('companyname');

            varisperson = false;


            //formatthe data to be submitted; for this instance I used JSON

            varjsonString = "{\"companyname\":\"" + companyname +"\",\"isperson\":" + isperson + "}";


            //sendthe request to the restlet and retrieve the response

            varoutput = nlapiRequestURL(url,jsonString,headers, null, "POST");



        catch(e){ thrownlapiCreateError('SUITELET_ERROR', + ' ' + e.message, false); }







function RestletPost(data){

    //create customer record

    var customer = nlapiCreateRecord('customer');


    //set values of the record depending on thevalues submitted to the Restlet

    if(data.isperson == false){customer.setFieldValue('isperson','F'); }

    else { customer.setFieldValue('isperson','T');}




    //submit record to NetSuite

    var id = nlapiSubmitRecord(customer);


    //create an object that would contain theresponse to be sent

    var output = new Object(); = id;


    //send response back

    return output;




DISCLAIMER: The sample code described herein isprovided on an "as is" basis, without warranty of any kind, to thefullest extent permitted by law. Netsuite Inc. does not warrant or guaranteethe individual success developers may have in implementing the sample code ontheir development platforms or in using their own Web server configurations.



NetsuiteInc. does not warrant, guarantee or make any representations regarding the use,results of use, accuracy, timeliness or completeness of any data or informationrelating to the sample code. Netsuite Inc. disclaims all warranties, express orimplied, and in particular, disclaims all warranties of merchantability,fitness for a particular purpose, and warranties related to the code, or anyservice or software related thereto.



NetsuiteInc. shall not be liable for any direct, indirect or consequential damages orcosts of any type arising out of any action taken by you or others related tothe sample code.


No comments:

Post a Comment