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.
SUITEletCode:
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:');
form.addSubmitButton('Submit');
response.writePage(form);
}
else{
//when form is submittedcreate and send the request to the Restlet
try{
//URL ofRestlet
var url ="https://rest.netsuite.com/app/site/hosting/restlet.nl?script=108&deploy=1";
//Authorization header
varauthorization = "NLAuthnlauth_account=TSTDRV12345,nlauth_email=email@netsuite.com,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");
response.write(output.getBody());
}
catch(e){ thrownlapiCreateError('SUITELET_ERROR',e.name + ' ' + e.message, false); }
}
}
RESTletCode:
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');}
customer.setFieldValue('companyname',data.companyname);
customer.setFieldValue('billpay','F');
//submit record to NetSuite
var id = nlapiSubmitRecord(customer);
//create an object that would contain theresponse to be sent
var output = new Object();
output.id = 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