Friday, September 21, 2018

Post XML Data to Externally Available Suitelet


Customer wants to POST XML Data to an Externally Available Suitelet using an application (Java, .Net etc.).

The sample code below demonstrates how to do this in Java. Notice that the XML contains some Chinese characters. This code also demonstrates how to deal with it.

import java.net.*;
import java.io.*;
import javax.net.*;
import javax.net.ssl.HttpsURLConnection;

public class Main {

public static void main(String[] args) {

try {
//Build the XML String
StringBuilder XML = new StringBuilder();
XML.append("<Customer>");
XML.append("<InternalId>106</InternalId>");
XML.append("<Phone>+8621 8028840805</Phone>");
XML.append("<Fax>+8621 8058840806</Fax>");
XML.append("<Email>czhang@exc.com.cn</Email>");
XML.append("<Comments>大公司制作,在美国首演</Comments>");
XML.append("</Customer>");

//Convert the StringBuilder object to a String object
String XMLString = XML.toString();
System.out.println(XMLString);

//Convert the String object to bytes and encode it in UTF-8
byte[] EncodedXMLBytes = XMLString.getBytes("UTF-8");

//Connect to the Suitelet
URL url = new URL("https://forms.netsuite.com/app/site/hosting/scriptlet.nl?script=159&deploy=1&compid=TSTDRV501653&h=178b45e757bb5f2b2976");
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();

connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);

//Set the Headers correctly
connection.setRequestMethod("POST");
connection.setRequestProperty("User-Agent", "Mozilla/5.0");
connection.setRequestProperty("Content-Type", "application/xml;charset=UTF-8");

//Submit the Request
DataOutputStream output = new DataOutputStream( connection.getOutputStream());
output.write(EncodedXMLBytes);
output.flush();
output.close();

//Get the output
System.out.println("Response Code: " + connection.getResponseCode());
System.out.println("Response Message: " + connection.getResponseMessage());

connection.disconnect();
}
catch(Exception e){
System.out.println("Error has occurred");
e.printStackTrace();
}
}
}


Below is the code for the suitelet that parses the XML Data.

function postSuitelet(request,response) {
nlapiLogExecution('DEBUG', 'Method is: ', request.getMethod());
try {

//Get the XML String from the Request
var XMLRequest = request.getBody();
nlapiLogExecution('DEBUG', 'Name is: ', XMLRequest);
if(request.getMethod() == 'POST' && (XMLRequest != null || XMLRequest != '')) {
var xmlDoc = nlapiStringToXML(XMLRequest); //Conver the XML String to XML Document\

//Parse the XML Data
var customerNode = nlapiSelectNode(xmlDoc, "//*[name()='Customer']");
var customerInternalId = nlapiSelectValue(customerNode, "//*[name()='InternalId']");
var phoneNum = nlapiSelectValue(customerNode, "//*[name()='Phone']");
var faxNum = nlapiSelectValue(customerNode, "//*[name()='Fax']");
var email = nlapiSelectValue(customerNode, "//*[name()='Email']");
var comments = nlapiSelectValue(customerNode, "//*[name()='Comments']");

//Update the Record
var customerRec = nlapiLoadRecord('customer', customerInternalId);
customerRec.setFieldValue('phone', phoneNum);
customerRec.setFieldValue('fax', faxNum);
customerRec.setFieldValue('email', email);
customerRec.setFieldValue('comments', comments);

//Submit the record
nlapiSubmitRecord(customerRec, true, true);
}
}
catch (e){
throw nlapiCreateError('SUITELET_ERROR', e.name + ' ' + e.message, false);
}
 


And here is the result:

26 comments:

  1. The business investigation and insight segment has a boundless openings for work and acquiring potential.ExcelR Data Science Courses

    ReplyDelete
  2. At that point somebody alters their perspective and you get the chance to do it once more.
    ExcelR Data Science Courses

    ReplyDelete
  3. This comment has been removed by a blog administrator.

    ReplyDelete
  4. I like viewing web sites which comprehend the price of delivering the excellent useful resource free of charge. I truly adored reading your posting. Thank you!

    Correlation vs Covariance

    ReplyDelete
  5. I have to search sites with relevant information on given topic and provide them to teacher our opinion and the article.

    Simple Linear Regression

    Correlation vs Covariance

    ReplyDelete
  6. Wow, What a Excellent post. I really found this to much informatics. It is what i was searching for.I would like to suggest you that please keep sharing such type of info.Thanks survey data entry

    ReplyDelete
  7. Really nice and interesting post. I was looking for this kind of information and enjoyed reading this one.
    data science course in guwahati

    ReplyDelete
  8. Your writing is fine and gives food for thought. I hope that I’ll have more time to read your articles . Regards. I wish you that you frequently publish new texts and invite you to greet me data entry bookkeeping

    ReplyDelete
  9. Such a very useful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. data science training in coimbatore

    ReplyDelete
  10. Such a very useful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. data science training in coimbatore

    ReplyDelete
  11. I genuinely believed you would probably have something useful to say. All I hear is a bunch of whining about something that you can fix if you were not too busy looking for attention. After all, I know it was my choice to read.. bookkeeping data entry

    ReplyDelete
  12. A portion of the Data Entry programs I investigated were from Clickbank's Commercial center.data entry virtual assistant

    ReplyDelete
  13. Thanks for the blog loaded with so many information. Stopping by your blog helped me to get what I was looking for. hikedatabase.com/united-states/hiking-in-rhode-island/

    ReplyDelete
  14. There are actually a variety of particulars like that to take into consideration. That could be a nice level to convey up. I provide the ideas above as normal inspiration but clearly there are questions just like the one you deliver up where a very powerful thing shall be working in sincere good faith. I don?t know if best practices have emerged around issues like that, however I am positive that your job is clearly recognized as a good game. Both girls and boys really feel the impact of only a second’s pleasure, for the remainder of their lives.url

    ReplyDelete
  15. I am looking for and I love to post a comment that "The content of your post is awesome" Great work!

    Simple Linear Regression

    Correlation vs covariance

    KNN Algorithm

    Logistic Regression explained

    ReplyDelete
  16. Its as if you had a great grasp on the subject matter, but you forgot to include your readers. Perhaps you should think about this from more than one angle. inventory data entry

    ReplyDelete
  17. Really nice and interesting post. I was looking for this kind of information and enjoyed reading this one. Keep posting. Thanks for sharing.

    data science interview questions

    ReplyDelete
  18. I really enjoy simply reading all of your weblogs. Simply wanted to inform you that you have people like me who appreciate your work. Definitely a great post. Hats off to you! The information that you have provided is very helpful. crm data entry

    ReplyDelete
  19. There is no dearth of Data Science course syllabus or resources. Learn the advanced data science course concepts and get your skills upgraded from the pioneers in Data Science.
    data science course bangalore
    data science course syllabus
    data science training in marathahalli

    ReplyDelete
  20. I am looking for and I love to post a comment that "The content of your post is awesome" Great work!

    Simple Linear Regression

    Correlation vs Covariance

    ReplyDelete
  21. Thank you for taking the time to publish this information very useful! Multi-User CRM

    ReplyDelete
  22. Good artcile, but it would be better if in future you can share more about this subject. Keep posting. Legacy Data Archiving System

    ReplyDelete
  23. Attractive, post. I just stumbled upon your weblog and wanted to say that I have liked browsing your blog posts. After all, I will surely subscribe to your feed, and I hope you will write again soon! dotcomsecrets

    ReplyDelete