Wednesday, December 23, 2009

Logging into Netsuite with ColdFusion or Java - Code Sample

Every now and then, I get a request about sample code for logging into Netsuite. I often forget that I started working with Netsuite in CF before I commonly used native java objects. It took me some tinkering to figure out, but when dealing with Netsuite specific datatypes and objects in ColdFusion, sometimes CF will often correctly translate an array, integer, string, etc to an equivalent compatible with the SOAP request, however often I just skip the middle man and eliminate risk by casting the variable or creating a native instance of the java stub class.

Here is the full java equivalent (I haven't tried to compile this, so you may need to make some tweaks)

//java pseudo-ish code (meaning I haven't actually tried to compile this)
import java.net.URL;
import com.netsuite.webservices.platform_2009_1.NetSuitePortType;
import com.netsuite.webservices.platform_2009_1.NetSuiteServiceLocator;
import com.netsuite.webservices.platform.core_2009_1.Passport;

String YourNetsuiteAccountNumber = "123456789";

URL endpoint = new URL("https://webservices.netsuite.com/services/NetSuitePort_2009_1?c=" + YourNetsuiteAccountNumber);
NetSuitePortType nsws = NetSuiteServiceLocator.getNetSuitePort(endpoint);
nsws.setMaintainSession(true); // maintain this session
nsws.setTimeout(1000 * 60 * 60 * 2); // set 2 hour timeout period
Passport passport = new Passport();
passport.setEmail("youremail@blahblah.com"); // email address used to log into netsuite (must be set up as web service user)
passport.setPassword("password1234");
passport.setAccount(YourNetsuiteAccountNumber);
nsws.login(passport);

No comments: