netsuite integration

Ketan Jetty
enthusiasm for technology

netsuite integration

Netsuite integration code in java. You can jar this code and use this from coldfusion to communicate with netsuite.

Netsuite integration code in Java
import java.util.*;
import java.util.HashMap;

import org.apache.axis.transport.http.HTTPConstants;
import com.netsuite.webservices.lists.relationships_2_5.Customer;
import com.netsuite.webservices.lists.relationships_2_5.CustomerAddressbook;
import com.netsuite.webservices.lists.relationships_2_5.CustomerAddressbookList;
import com.netsuite.webservices.lists.relationships_2_5.CustomerSearch;
import com.netsuite.webservices.platform.common_2_5.CustomerSearchBasic;
import com.netsuite.webservices.platform.core_2_5.CustomFieldList;
import com.netsuite.webservices.platform.core_2_5.CustomFieldRef;
import com.netsuite.webservices.platform.core_2_5.Passport;
import com.netsuite.webservices.platform.core_2_5.Record;
import com.netsuite.webservices.platform.core_2_5.RecordList;
import com.netsuite.webservices.platform.core_2_5.RecordRef;
import com.netsuite.webservices.platform.core_2_5.SearchResult;
import com.netsuite.webservices.platform.core_2_5.SearchStringField;
import com.netsuite.webservices.platform.core_2_5.Status;
import com.netsuite.webservices.platform.core_2_5.StatusDetail;
import com.netsuite.webservices.platform.core_2_5.StringCustomFieldRef;
import com.netsuite.webservices.platform.core_2_5.types.RecordType;
import com.netsuite.webservices.platform.core_2_5.types.SearchStringFieldOperator;
import com.netsuite.webservices.platform.messages_2_5.WriteResponse;
import com.netsuite.webservices.platform_2_5.NetSuiteBindingStub;
import com.netsuite.webservices.platform_2_5.NetSuitePortType;
import com.netsuite.webservices.platform_2_5.NetSuiteServiceLocator;
import com.netsuite.webservices.platform.messages_2_5.*;
import com.netsuite.webservices.activities.scheduling_2_5.Task;
import com.netsuite.webservices.lists.relationships_2_5.ContactSearch;
import com.netsuite.webservices.platform.common_2_5.ContactSearchBasic;
import com.netsuite.webservices.platform.core_2_5.*;
import com.netsuite.webservices.platform.core_2_5.types.*;
import com.netsuite.webservices.setup.customization_2_5.*;
import com.netsuite.webservices.lists.relationships_2_5.Contact;

public class NetSuiteAPIKJ {
	public NetSuiteAPIKJ() {
	}

	private NetSuiteServiceLocator service = null;
	private NetSuitePortType port = null;
	private Passport passport = null;
	private StatusDetail[] statusDetail = null;
	private Status status = null;
	private Task task = null;
	private Customer customer = null;
	private Contact contact = null;
	private SearchResult sResult = null;
	private boolean loggedIn = false;
	private boolean isDebug = false;
	private HashMap hm = new HashMap();
	private HashMap hmLeadSource = new HashMap();
	private HashMap hmCustomEntityFields = new HashMap();
	private String _dataKeyValues = "";

	public static void main(String[] args) {
		Timer t = new Timer();
		t.start();

		NetSuiteAPIKJ nsObj = new NetSuiteAPIKJ();
		nsObj.setDebug("yes");

		// login to netsutie account
		nsObj.login("ketan.jetty@ketanjetty.com", "asdfpoiu", "TSTDRV13579", "3");

		 nsObj.getsetAllLeadSources();
		 nsObj.getsetAllCustomFields();
			 
		// nsObj.setDataKeyValues("firstName=sept07_001 first name|lastName=sept07_001 last name|email=sept07_013@test.com|phone=1234567890|companyName=sept07_013 company|customerID=sept07_013 url679|leadSource=test|ENTITYSTATUS=leadNew|");
		// String tempValue = "";
		// tempValue = nsObj.getCustomer();
		// tempValue = nsObj.getContact();

		// logout from netsuite
		nsObj.logout();

		if (nsObj.isDebug) {
			System.out.println("----- process complete -----");
		}

		t.end();
		System.out.println("Execution time: " + t.duration() + " ms");
	}

	public String doSPECIFIC2Process() {
		String retVal = "";
		try {
			// CUSTOMER
			String customerInternalID = addCustomerDuplicates();
			if (customerInternalID != null && customerInternalID.toUpperCase().indexOf("ERROR") > -1) {
				retVal = retVal + "Error [Netsuite]: Unable to create a customer. " + customerInternalID;
			} else {
				retVal = retVal + "customerInternalID=" + customerInternalID + ", ";

				// CONTACT
				String contactInternalID = addContactDuplicates();
				if (contactInternalID != null && contactInternalID.toUpperCase().indexOf("ERROR") > -1) {
					retVal = retVal + "Error [Netsuite]: Unable to create a contact. " + contactInternalID;
				} else {
					retVal = retVal + "contactInternalID=" + contactInternalID + ", ";

					// Attach CONTACT to CUSTOMER
					String connectionID = attachCustomerToContact(customerInternalID, contactInternalID);
					if (connectionID != null && connectionID.toUpperCase().indexOf("ERROR") > -1) {
						retVal = retVal + "Error [Netsuite]: Unable to attach customer to contact. " + connectionID;
					} else {
						retVal = retVal + "connectionID=" + connectionID + ", ";
					}
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			retVal = retVal + "Error [NetSuite]: " + retVal + ex.toString();
		}

		if (isDebug) {
			System.out.println(retVal);
		}

		return retVal;
	}

	public String doSpecificProcess() {
		String retVal = "";
		try {
			// CUSTOMER
			String customerInternalID = addCustomerDuplicates();
			if (customerInternalID != null && customerInternalID.toUpperCase().indexOf("ERROR") > -1) {
				retVal = retVal + "Error [Netsuite]: Unable to create a customer. " + customerInternalID;
			} else {
				retVal = retVal + "customerInternalID=" + customerInternalID + ", ";

				// CONTACT
				String contactInternalID = addContactDuplicates();
				if (contactInternalID != null && contactInternalID.toUpperCase().indexOf("ERROR") > -1) {
					retVal = retVal + "Error [Netsuite]: Unable to create a contact. " + contactInternalID;
				} else {
					retVal = retVal + "contactInternalID=" + contactInternalID + ", ";

					// Attach CONTACT to CUSTOMER
					String connectionID = attachCustomerToContact(customerInternalID, contactInternalID);
					if (connectionID != null && connectionID.toUpperCase().indexOf("ERROR") > -1) {
						retVal = retVal + "Error [Netsuite]: Unable to attach customer to contact. " + connectionID;
					} else {
						retVal = retVal + "connectionID=" + connectionID + ", ";
					}
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			retVal = retVal + "Error [NetSuite]: " + retVal + ex.toString();
		}

		if (isDebug) {
			System.out.println(retVal);
		}

		return retVal;
	}

	public String attachCustomerToContact(String customerInternalID, String contactInternalID) {
		String retVal = "";

		try {
			RecordRef lead = new RecordRef();
			lead.setInternalId(customerInternalID);
			lead.setType(RecordType.customer);

			RecordRef cont = new RecordRef();
			cont.setInternalId(contactInternalID);
			cont.setType(RecordType.contact);

			AttachContactReference att = new AttachContactReference();
			att.setContact(cont);
			att.setAttachTo(lead);

			retVal = getResult(port.attach(att));
		} catch (Exception ex) {
			ex.printStackTrace();
			retVal = "Error [NetSuite]: " + ex.toString();
		}

		if (isDebug) {
			System.out.println(retVal);
		}

		return retVal;
	}

	public String addContactDuplicates() {
		String retVal = "";
		String contID = "";

		if (hm.containsKey("FIRSTNAME") && hm.get("FIRSTNAME") != null && hm.get("FIRSTNAME").toString().trim().length() > 0 && hm.containsKey("LASTNAME") && hm.get("LASTNAME") != null && hm.get("LASTNAME").toString().trim().length() > 0) {
			String mainFirstName = hm.get("FIRSTNAME").toString().trim();

			int loopCounter = 0;
			while (!contID.equalsIgnoreCase("0")) {
				if (loopCounter > 0) {
					hm.put("FIRSTNAME", mainFirstName + loopCounter);
				}

				contID = getContact();

				if (loopCounter > 100) {
					break;
				}

				loopCounter++;
			}
		}

		if (contID.equalsIgnoreCase("0")) {
			retVal = addContact();
		}

		if (isDebug) {
			System.out.println(retVal);
		}

		return retVal;
	}

	public String addContact() {
		String retVal = "";

		try {
			if (loggedIn) {
				contact = new Contact();

				if (hm.containsKey("FIRSTNAME") && hm.get("FIRSTNAME") != null && hm.get("FIRSTNAME").toString().trim().length() > 0) {
					contact.setFirstName(hm.get("FIRSTNAME").toString().trim());
				}
				// mandatory field for INDIVIDUAL
				if (hm.containsKey("LASTNAME") && hm.get("LASTNAME") != null && hm.get("LASTNAME").toString().trim().length() > 0) {
					contact.setLastName(hm.get("LASTNAME").toString().trim());
				}

				retVal = getResult(port.add(contact));
			}

		} catch (Exception ex) {
			ex.printStackTrace();
			retVal = "Error [NetSuite]: " + ex.toString();
		}

		if (isDebug) {
			System.out.println(retVal);
		}

		return retVal;
	}

	public String addCustomerDuplicates() {
		String retVal = "";
		String custID = "";

		if (hm.containsKey("CUSTOMERID") && hm.get("CUSTOMERID") != null && hm.get("CUSTOMERID").toString().trim().length() > 0) {
			String mainValue = hm.get("CUSTOMERID").toString().trim();

			int loopCounter = 0;
			while (!custID.equalsIgnoreCase("0")) {
				if (loopCounter > 0) {
					hm.put("CUSTOMERID", mainValue + loopCounter);
				}

				custID = getCustomer();

				if (loopCounter > 100) {
					break;
				}

				loopCounter++;
			}
		}

		if (custID.equalsIgnoreCase("0")) {
			retVal = addCustomer();
		}

		if (isDebug) {
			System.out.println(retVal);
		}

		return retVal;
	}

	public String addCustomer() {
		String retVal = "";

		try {
			if (loggedIn) {
				customer = new Customer();

				// mandatory field
				if (hm.containsKey("CUSTOMERID") && hm.get("CUSTOMERID") != null && hm.get("CUSTOMERID").toString().trim().length() > 0) {
					customer.setEntityId(hm.get("CUSTOMERID").toString().trim());
				}
				// mandatory field for COMPANY
				if (hm.containsKey("COMPANYNAME") && hm.get("COMPANYNAME") != null && hm.get("COMPANYNAME").toString().trim().length() > 0) {
					customer.setCompanyName(hm.get("COMPANYNAME").toString().trim());
				}
				// SALES REPRESENTATIVE :: SALESREP
				if (hm.containsKey("SALESREP") && hm.get("SALESREP") != null && hm.get("SALESREP").toString().trim().length() > 0) {
					RecordRef rr = new RecordRef();
					rr.setInternalId(hm.get("SALESREP").toString().trim());

					customer.setSalesRep(rr);
				}
				// SALES TERRITORY :: SALESTERR
				if (hm.containsKey("SALESTERR") && hm.get("SALESTERR") != null && hm.get("SALESTERR").toString().trim().length() > 0) {
					RecordRef territory = new RecordRef();
					territory.setInternalId(hm.get("SALESTERR").toString().trim());

					customer.setTerritory(territory);
				}

				// customer is an INDIVIDUAL
				if (hm.containsKey("ISPERSON") && hm.get("ISPERSON") != null && hm.get("ISPERSON").toString().trim().length() > 0 && (hm.get("ISPERSON").toString().equalsIgnoreCase("true") || hm.get("ISPERSON").toString().equalsIgnoreCase("yes") || hm.get("ISPERSON").toString().equalsIgnoreCase("1"))) {
					customer.setIsPerson(Boolean.TRUE);
					// mandatory field for INDIVIDUAL
					if (hm.containsKey("FIRSTNAME") && hm.get("FIRSTNAME") != null && hm.get("FIRSTNAME").toString().trim().length() > 0) {
						customer.setFirstName(hm.get("FIRSTNAME").toString().trim());
					}
					// mandatory field for INDIVIDUAL
					if (hm.containsKey("LASTNAME") && hm.get("LASTNAME") != null && hm.get("LASTNAME").toString().trim().length() > 0) {
						customer.setLastName(hm.get("LASTNAME").toString().trim());
					}
					if (hm.containsKey("MIDDLENAME") && hm.get("MIDDLENAME") != null && hm.get("MIDDLENAME").toString().trim().length() > 0) {
						customer.setMiddleName(hm.get("MIDDLENAME").toString().trim());
					}
					if (hm.containsKey("JOBTITLE") && hm.get("JOBTITLE") != null && hm.get("JOBTITLE").toString().trim().length() > 0) {
						customer.setTitle(hm.get("JOBTITLE").toString().trim());
					}
					if (hm.containsKey("SALUTATION") && hm.get("SALUTATION") != null && hm.get("SALUTATION").toString().trim().length() > 0) {
						customer.setSalutation(hm.get("SALUTATION").toString().trim());
					}
					if (hm.containsKey("ALTEMAIL") && hm.get("ALTEMAIL") != null && hm.get("ALTEMAIL").toString().trim().length() > 0) {
						customer.setAltEmail(hm.get("ALTEMAIL").toString().trim());
					}
					if (hm.containsKey("HOMEPHONE") && hm.get("HOMEPHONE") != null && hm.get("HOMEPHONE").toString().trim().length() > 0) {
						customer.setHomePhone(hm.get("HOMEPHONE").toString().trim());
					}
					if (hm.containsKey("MOBILEPHONE") && hm.get("MOBILEPHONE") != null && hm.get("MOBILEPHONE").toString().trim().length() > 0) {
						customer.setMobilePhone(hm.get("MOBILEPHONE").toString().trim());
					}
				} else {
					if (hm.containsKey("URL") && hm.get("URL") != null && hm.get("URL").toString().trim().length() > 0) {
						customer.setUrl("http://" + hm.get("URL").toString().trim());
					}
				}

				if (hm.containsKey("PHONE") && hm.get("PHONE") != null && hm.get("PHONE").toString().trim().length() > 0) {
					customer.setPhone(hm.get("PHONE").toString().trim());
				}
				if (hm.containsKey("FAX") && hm.get("FAX") != null && hm.get("FAX").toString().trim().length() > 0) {
					customer.setFax(hm.get("FAX").toString().trim());
				}
				if (hm.containsKey("EMAIL") && hm.get("EMAIL") != null && hm.get("EMAIL").toString().trim().length() > 0) {
					customer.setEmail(hm.get("EMAIL").toString().trim());
				}
				if (hm.containsKey("ALTPHONE") && hm.get("ALTPHONE") != null && hm.get("ALTPHONE").toString().trim().length() > 0) {
					customer.setAltPhone(hm.get("ALTPHONE").toString().trim());
				}
				if (hm.containsKey("COMMENTS") && hm.get("COMMENTS") != null && hm.get("COMMENTS").toString().trim().length() > 0) {
					customer.setComments(hm.get("COMMENTS").toString().trim());
				}

				// /// Customer Status /////
				/**
				 * 17 lead - dead 6 lead - new 18 lead - qualified 21 lead -
				 * unqualified 14 prospect - closed lost 9 prospect - identified
				 * decision makers 8 prospect - in discussion 11 prospect - in
				 * negotiation 7 prospect - opportunity identified 10 prospect -
				 * proposal 12 prospect - purchasing 13 customer - closed won ::
				 * DEFAULT 16 customer - lost customer 15 customer - renewal
				 */

				if (hm.containsKey("ENTITYSTATUS") && hm.get("ENTITYSTATUS") != null && hm.get("ENTITYSTATUS").toString().trim().length() > 0) {
					RecordRef entityStatus = new RecordRef();

					if (hm.get("ENTITYSTATUS").toString().trim().equalsIgnoreCase("leadNew")) {
						entityStatus.setInternalId("6");
					} else if (hm.get("ENTITYSTATUS").toString().trim().equalsIgnoreCase("leadUnqualified")) {
						entityStatus.setInternalId("21");
					} else if (hm.get("ENTITYSTATUS").toString().trim().equalsIgnoreCase("leadQualified")) {
						entityStatus.setInternalId("18");
					} else if (hm.get("ENTITYSTATUS").toString().trim().equalsIgnoreCase("leadDead")) {
						entityStatus.setInternalId("17");
					} else if (hm.get("ENTITYSTATUS").toString().trim().equalsIgnoreCase("prospectPurchasing")) {
						entityStatus.setInternalId("12");
					} else if (hm.get("ENTITYSTATUS").toString().trim().equalsIgnoreCase("prospectClosedLost")) {
						entityStatus.setInternalId("14");
					} else if (hm.get("ENTITYSTATUS").toString().trim().equalsIgnoreCase("prospectOpportunity")) {
						entityStatus.setInternalId("7");
					} else if (hm.get("ENTITYSTATUS").toString().trim().equalsIgnoreCase("prospectProposal")) {
						entityStatus.setInternalId("10");
					} else if (hm.get("ENTITYSTATUS").toString().trim().equalsIgnoreCase("customerLost")) {
						entityStatus.setInternalId("16");
					} else if (hm.get("ENTITYSTATUS").toString().trim().equalsIgnoreCase("customerRenewal")) {
						entityStatus.setInternalId("15");
					} else {
						entityStatus.setInternalId("13");
					}

					customer.setEntityStatus(entityStatus);
				}

				// LeadSource
				if (hm.containsKey("LEADSOURCE") && hm.get("LEADSOURCE") != null && hm.get("LEADSOURCE").toString().trim().length() > 0) {
					if (hmLeadSource.containsKey(hm.get("LEADSOURCE").toString().toUpperCase().trim())) {
						RecordRef leadSource = new RecordRef();
						leadSource.setType(RecordType.leadSource);
						leadSource.setInternalId(hmLeadSource.get(hm.get("LEADSOURCE").toString().toUpperCase().trim()).toString().trim());
						customer.setLeadSource(leadSource);
					}
				}

				try {
					/**
					 * addressDefaultShipping=1| addressDefaultBilling=1|
					 * addressLabel= label| addressAddressee= adressee|
					 * addressAttention= attention| addressAddr1= addr1|
					 * addressCity= city| addressState= state| addressZip= zip|
					 * addressCountry= county|
					 */

					CustomerAddressbook address = new CustomerAddressbook();

					if (hm.containsKey("ADDRESSDEFAULTSHIPPING") && hm.get("ADDRESSDEFAULTSHIPPING") != null && hm.get("ADDRESSDEFAULTSHIPPING").toString().trim().length() > 0 && (hm.get("ADDRESSDEFAULTSHIPPING").toString().equalsIgnoreCase("true") || hm.get("ADDRESSDEFAULTSHIPPING").toString().equalsIgnoreCase("yes") || hm.get("ADDRESSDEFAULTSHIPPING").toString().equalsIgnoreCase("1"))) {
						address.setDefaultShipping(Boolean.TRUE);
					}

					if (hm.containsKey("ADDRESSDEFAULTBILLING") && hm.get("ADDRESSDEFAULTBILLING") != null && hm.get("ADDRESSDEFAULTBILLING").toString().trim().length() > 0 && (hm.get("ADDRESSDEFAULTBILLING").toString().equalsIgnoreCase("true") || hm.get("ADDRESSDEFAULTBILLING").toString().equalsIgnoreCase("yes") || hm.get("ADDRESSDEFAULTBILLING").toString().equalsIgnoreCase("1"))) {
						address.setDefaultBilling(Boolean.TRUE);
					}

					if (hm.containsKey("ADDRESSLABEL") && hm.get("ADDRESSLABEL") != null && hm.get("ADDRESSLABEL").toString().trim().length() > 0) {
						address.setLabel(hm.get("ADDRESSLABEL").toString().trim());
					}

					if (hm.containsKey("ADDRESSADDRESSEE") && hm.get("ADDRESSADDRESSEE") != null && hm.get("ADDRESSADDRESSEE").toString().trim().length() > 0) {
						address.setAddressee(hm.get("ADDRESSADDRESSEE").toString().trim());
					}

					if (hm.containsKey("ADDRESSATTENTION") && hm.get("ADDRESSATTENTION") != null && hm.get("ADDRESSATTENTION").toString().trim().length() > 0) {
						address.setAttention(hm.get("ADDRESSATTENTION").toString().trim());
					}

					if (hm.containsKey("ADDRESSADDR1") && hm.get("ADDRESSADDR1") != null && hm.get("ADDRESSADDR1").toString().trim().length() > 0) {
						address.setAddr1(hm.get("ADDRESSADDR1").toString().trim());
					}

					if (hm.containsKey("ADDRESSCITY") && hm.get("ADDRESSCITY") != null && hm.get("ADDRESSCITY").toString().trim().length() > 0) {
						address.setCity(hm.get("ADDRESSCITY").toString().trim());
					}

					if (hm.containsKey("ADDRESSZIP") && hm.get("ADDRESSZIP") != null && hm.get("ADDRESSZIP").toString().trim().length() > 0) {
						address.setZip(hm.get("ADDRESSZIP").toString().trim());
					}

					address.setState("_" + hm.get("ADDRESSSTATE").toString().trim());
					NSCountry nsc = new NSCountry();
					address.setCountry(nsc.getHBCountry(hm.get("ADDRESSCOUNTRY").toString().trim()));

					// Attach the CustomerAddressbookList to the customer
					CustomerAddressbookList addressList = new CustomerAddressbookList();
					CustomerAddressbook[] addresses = new CustomerAddressbook[1];
					addresses[0] = address;
					addressList.setAddressbook(addresses);

					customer.setAddressbookList(addressList);
				} catch (Exception exAddr) {
					// do nothing
					exAddr.printStackTrace();
				}

				// CustomEntityFields
				setCustomEntityFields();

				retVal = getResult(port.add(customer));
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			retVal = "Error [NetSuite]: " + retVal + ex.toString();
		}

		if (isDebug) {
			System.out.println(retVal);
		}

		return retVal;
	}

	private void setCustomEntityFields() {
		// custom entity fields
		String[] noOfCustEntity = _dataKeyValues.split("CUSTENTITY");

		if (noOfCustEntity.length > 1) {
			// figure out the CustomFieldRef[] array length
			int CustomFieldRefLength = 0;

			Iterator iter = hm.keySet().iterator();
			while (iter.hasNext()) {
				Object key = iter.next();
				String keyName = key.toString().trim();

				if (keyName.startsWith("CUSTENTITY")) {
					String keyValue = hm.get(key).toString();

					if (keyValue.trim().length() > 0 && hmCustomEntityFields.containsValue(keyName)) {
						CustomFieldRefLength++;
					}
				}
			}

			// create CustomFieldList and CustomFieldRef[]
			CustomFieldList customFieldList = new CustomFieldList();
			CustomFieldRef[] customFieldRef = new CustomFieldRef[CustomFieldRefLength];

			int customFieldNo = 0;

			iter = hm.keySet().iterator();
			while (iter.hasNext()) {
				Object key = iter.next();
				String keyName = key.toString().trim();

				if (keyName.startsWith("CUSTENTITY")) {
					String keyValue = hm.get(key).toString();

					if (keyValue.trim().length() > 0 && hmCustomEntityFields.containsValue(keyName)) {
						StringCustomFieldRef stringCustomFieldRef = new StringCustomFieldRef();
						stringCustomFieldRef.setInternalId(keyName);
						stringCustomFieldRef.setValue(keyValue);

						customFieldRef[customFieldNo] = stringCustomFieldRef;
						customFieldList.setCustomField(customFieldRef);

						customFieldNo++;
					}
				}
			}

			customer.setCustomFieldList(customFieldList);
		}
	}

	public String addTask() {
		String retVal = "";

		try {
			if (loggedIn) {
				task = new Task();

				if (hm.containsKey("TITLE") && hm.get("TITLE") != null && hm.get("TITLE").toString().trim().length() > 0) {
					task.setTitle(hm.get("TITLE").toString().trim());
				}
				if (hm.containsKey("MESSAGE") && hm.get("MESSAGE") != null && hm.get("MESSAGE").toString().trim().length() > 0) {
					task.setMessage(hm.get("MESSAGE").toString().trim());
				}
				if (hm.containsKey("ASSIGNEDTO") && hm.get("ASSIGNEDTO") != null && hm.get("ASSIGNEDTO").toString().trim().length() > 0) {
					/**
					 * String contactInternalId = getContact("contactID=" +
					 * hm.get("ASSIGNEDTO").toString().trim());
					 * 
					 * if (contactInternalId != null &&
					 * contactInternalId.trim().length() > 0 &&
					 * !contactInternalId.startsWith("Error")) { RecordRef
					 * assigned = new RecordRef();
					 * assigned.setInternalId(contactInternalId);
					 * task.setAssigned(assigned); }
					 */
				}

				retVal = getResult(port.add(task));
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			retVal = "Error [NetSuite]: " + ex.toString();
		}

		if (isDebug) {
			System.out.println(retVal);
		}

		return retVal;
	}

	public String getContact() {
		String retVal = "";

		try {
			if (loggedIn) {
				ContactSearch cs = new ContactSearch();
				ContactSearchBasic csb = new ContactSearchBasic();
				SearchStringFieldOperator ssfo = null;

				if (hm.containsKey("FIRSTNAME") && hm.get("FIRSTNAME") != null && hm.get("FIRSTNAME").toString().trim().length() > 0) {
					String searchstring = hm.get("FIRSTNAME").toString().trim();
					SearchStringField ssf = new SearchStringField();

					if (searchstring.startsWith("%") && (searchstring.endsWith("%"))) {
						ssfo = SearchStringFieldOperator.contains;
						ssf.setSearchValue(searchstring.substring(1, searchstring.length() - 1));
					} else if (searchstring.startsWith("%")) {
						ssfo = SearchStringFieldOperator.startsWith;
						ssf.setSearchValue(searchstring.substring(1));
					} else {
						ssfo = SearchStringFieldOperator.is;
						ssf.setSearchValue(searchstring);
					}

					ssf.setOperator(ssfo);

					csb.setFirstName(ssf);
				}

				if (hm.containsKey("LASTNAME") && hm.get("LASTNAME") != null && hm.get("LASTNAME").toString().trim().length() > 0) {
					String searchstring = hm.get("LASTNAME").toString().trim();
					SearchStringField ssf = new SearchStringField();

					if (searchstring.startsWith("%") && (searchstring.endsWith("%"))) {
						ssfo = SearchStringFieldOperator.contains;
						ssf.setSearchValue(searchstring.substring(1, searchstring.length() - 1));
					} else if (searchstring.startsWith("%")) {
						ssfo = SearchStringFieldOperator.startsWith;
						ssf.setSearchValue(searchstring.substring(1));
					} else {
						ssfo = SearchStringFieldOperator.is;
						ssf.setSearchValue(searchstring);
					}

					ssf.setOperator(ssfo);

					csb.setLastName(ssf);
				}

				cs.setBasic(csb);

				sResult = port.search(cs);

				RecordList recList = sResult.getRecordList();
				Record[] rec = recList.getRecord();

				if (rec != null) {
					for (int i = 0; i < rec.length; i++) {
						contact = (Contact) rec[i];
						retVal = contact.getInternalId();
					}
				} else {
					retVal = "0";
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			retVal = "Error [NetSuite]: " + ex.toString();
		}

		if (isDebug) {
			System.out.println(retVal);
		}

		return retVal;
	}

	public String getCustomer() {
		// ///// SEARCH COMBINATIONS ///////
		// is customerID=May22_006
		// starts with customerID=M%
		// contains customerID=%May%
		String retVal = "";

		try {
			if (loggedIn) {
				CustomerSearch cs = new CustomerSearch();
				CustomerSearchBasic csb = new CustomerSearchBasic();
				SearchStringField ssf = new SearchStringField();
				SearchStringFieldOperator ssfo = null;

				if (hm.containsKey("CUSTOMERID") && hm.get("CUSTOMERID") != null && hm.get("CUSTOMERID").toString().trim().length() > 0) {
					String searchstring = hm.get("CUSTOMERID").toString().trim();

					ssf = new SearchStringField();

					if (searchstring.startsWith("%") && (searchstring.endsWith("%"))) {
						ssfo = SearchStringFieldOperator.contains;
						ssf.setSearchValue(searchstring.substring(1, searchstring.length() - 1));
					} else if (searchstring.startsWith("%")) {
						ssfo = SearchStringFieldOperator.startsWith;
						ssf.setSearchValue(searchstring.substring(1));
					} else {
						ssfo = SearchStringFieldOperator.is;
						ssf.setSearchValue(searchstring);
					}

					ssf.setOperator(ssfo);
					csb.setEntityId(ssf);
					cs.setBasic(csb);

					sResult = port.search(cs);
					RecordList recList = sResult.getRecordList();
					Record[] rec = recList.getRecord();

					if (rec != null) {
						for (int i = 0; i < rec.length; i++) {
							customer = (Customer) rec[i];
							retVal = customer.getInternalId();
						}
					} else {
						retVal = "0";
					}
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			retVal = "Error [NetSuite]: " + ex.toString();
		}

		if (isDebug) {
			System.out.println(retVal);
		}

		// ///// WORKING EXAMPLE - DO NOT DELETE - kept for reference ///////
		/**
		 * CustomerSearch cs = new CustomerSearch(); CustomerSearchBasic csb =
		 * new CustomerSearchBasic(); SearchStringField ssf = new
		 * SearchStringField(); SearchStringFieldOperator ssfo = null;
		 * SearchStringField ssf = new SearchStringField();
		 * ssf.setSearchValue("M"); SearchStringFieldOperator ssfo =
		 * SearchStringFieldOperator.startsWith; ssf.setOperator(ssfo);
		 * csb.setEntityId(ssf); cs.setBasic(csb);
		 */

		return retVal;
	}

	public void setDataKeyValues(String dataKeyValues) {
		_dataKeyValues = dataKeyValues;
		hm = getMappedData(dataKeyValues);
	}

	public void getsetAllCustomFields() {
		try {
			hmCustomEntityFields.clear();

			GetCustomizationResult res = port.getCustomization(new CustomizationType(GetCustomizationType.entityCustomField));

			if (res.getStatus().isIsSuccess()) {
				RecordList recList = res.getRecordList();
				Record[] recArray = recList.getRecord();

				for (int i = 0; i < recArray.length; i++) {
					EntityCustomField entityCustomField = (EntityCustomField) recArray[i];

					String tempValue = cleanString(entityCustomField.getLabel());

					if (!hmCustomEntityFields.containsKey(tempValue)) {
						hmCustomEntityFields.put(tempValue, entityCustomField.getInternalId().trim());
					}
				}
			}

			if (isDebug) {
				System.out.println("hmCustomEntityFields =" + hmCustomEntityFields);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public void setAllCustomFields(String dataKeyValues) {
		try {
			hmCustomEntityFields.clear();
			hmCustomEntityFields = getMappedData(dataKeyValues);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public void getsetAllLeadSources() {
		try {
			hmLeadSource.clear();

			GetSelectValueField f = new GetSelectValueField();
			f.setFieldType(GetSelectValueType.customer_leadSource);
			GetSelectValueResult res = port.getSelectValue(f);

			if (res.getStatus().isIsSuccess()) {
				RecordRefList recRefList = res.getRecordRefList();

				for (int i = 0; i < recRefList.getRecordRef().length; i++) {
					String tempValue = cleanString(recRefList.getRecordRef(i).getName().toUpperCase());

					if (!hmLeadSource.containsKey(tempValue)) {
						hmLeadSource.put(tempValue, recRefList.getRecordRef(i).getInternalId().trim());
					}
				}
			}

			if (isDebug) {
				System.out.println("hmLeadSource =" + hmLeadSource);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public void setAllLeadSources(String dataKeyValues) {
		try {
			hmLeadSource.clear();
			hmLeadSource = getMappedData(dataKeyValues);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	private String cleanString(String in) {
		String retVal = in;

		// replace all   with white space
		retVal = retVal.replaceAll("\u00a0", " ").trim();

		// replace all double spaces with single space
		while (retVal.indexOf("  ") > 0) {
			retVal = retVal.replaceAll("  ", " ").trim();
		}

		return retVal;
	}

	private HashMap getMappedData(String dataKeyValues) {
		HashMap retMap = new HashMap();
		String key = "";
		String value = "";

		dataKeyValues = cleanString(dataKeyValues);

		try {
			if (dataKeyValues != null && dataKeyValues.length() > 0) {
				String[] aryKeyValues = dataKeyValues.split("\\|");

				for (int j = 0; j < aryKeyValues.length; j++) {
					if (aryKeyValues[j].length() > 0 && aryKeyValues[j].indexOf("=", 0) > 0) {
						String[] aryKeyValue = aryKeyValues[j].split("\\=");
						if (aryKeyValue != null) {
							if (aryKeyValue[0] != null && aryKeyValue[0].trim().length() > 0) {
								key = aryKeyValue[0].trim().toUpperCase();
								value = "";

								if (aryKeyValue.length == 2 && aryKeyValue[1] != null && aryKeyValue[1].trim().length() > 0) {
									value = aryKeyValue[1].trim();
								}
								retMap.put(key, value);
							}
						}
					}
				}
			}

			if (isDebug) {
				System.out.println(retMap);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			System.err.println(retMap);
		}

		return retMap;
	}

	public String logout() {
		String retVal = "";

		try {
			if (loggedIn) {
				retVal = getResult(port.logout());

				if (retVal != null && retVal.trim().length() == 0) {
					loggedIn = false;
					retVal = "Logout is successfull";
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			retVal = "Error [NetSuite]: " + retVal + ex.toString();
		}

		if (isDebug) {
			System.out.println(retVal);
		}

		return retVal;
	}

	public String login(String un, String pwd, String acctid, String rid) {
		String retVal = "";
		String tempRoleID = "3"; // 3 is default administrator role

		try {
			if (rid != null && rid.trim().length() > 0) {
				tempRoleID = rid;
			}
			// login is false
			loggedIn = false;
			// In order to use SSL forwarding for SOAP messages.
			System.setProperty("axis.socketSecureFactory", "org.apache.axis.components.net.SunFakeTrustSocketFactory");
			// Locate the NetSuite web service.
			service = new NetSuiteServiceLocator();
			// Enable client cookie management. This is required.
			service.setMaintainSession(true);
			// Get the service port
			port = service.getNetSuitePort();
			// Setting client timeout to 2 hour for long running operations
			((NetSuiteBindingStub) port).setTimeout(1000 * 60 * 60 * 2);
			// set the cookie
			((javax.xml.rpc.Stub) port)._setProperty(HTTPConstants.HEADER_COOKIE, new String[] { "ACCOUNT=" + acctid.trim() });

			RecordRef role = new RecordRef();
			role.setInternalId(tempRoleID);

			passport = new Passport();
			passport.setEmail(un.trim());
			passport.setPassword(pwd.trim());
			passport.setRole(role);
			passport.setAccount(acctid.trim());

			retVal = getResult(port.login(passport));

			if (retVal != null && retVal.trim().length() == 0) {
				loggedIn = true;
				retVal = "Login is successfull";
			}
		} catch (Exception ex) {
			loggedIn = false;
			ex.printStackTrace();
			retVal = "Error [NetSuite]: " + retVal + ex.toString();
		}

		if (isDebug) {
			System.out.println(retVal);
		}

		return retVal;
	}

	public void setDebug(String val) {
		isDebug = false;
		if (val != null) {
			if (val.trim().equalsIgnoreCase("1") || val.trim().equalsIgnoreCase("yes") || val.trim().equalsIgnoreCase("true")) {
				isDebug = true;
			}
		}
	}

	private String getResult(WriteResponse writeResponse) {
		String retVal = "";
		StringBuffer sb = new StringBuffer();

		try {
			status = writeResponse.getStatus();

			if (status != null && status.isIsSuccess()) {
				BaseRef br = writeResponse.getBaseRef();

				if (br != null) {
					RecordRef ii = (RecordRef) writeResponse.getBaseRef();
					retVal = ii.getInternalId();
				}
			} else {
				statusDetail = status.getStatusDetail();

				if (statusDetail != null) {
					for (int i = 0; i < statusDetail.length; i++) {
						sb.append(statusDetail[i].getType().getValue() + ":" + statusDetail[i].getMessage());
					}

					retVal = sb.toString();
				}
			}

			if (isDebug) {
				System.out.println(retVal);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			retVal = "Error [NetSuite]: " + retVal + ex.toString();
		}

		return retVal;
	}

	private String getResult(SessionResponse sessionResponse) {
		String retVal = "";
		StringBuffer sb = new StringBuffer();

		try {
			status = sessionResponse.getStatus();

			if (status != null && status.isIsSuccess()) {
				statusDetail = status.getStatusDetail();

				if (statusDetail != null) {
					for (int i = 0; i < statusDetail.length; i++) {
						sb.append(statusDetail[i].getType().getValue() + ":" + statusDetail[i].getMessage());
					}

					retVal = sb.toString();
				}
			} else {
				retVal = "Error [NetSuite]: Status is not available.";
			}

		} catch (Exception ex) {
			ex.printStackTrace();
			retVal = "Error [NetSuite]: " + retVal + ex.toString();
		}

		if (isDebug) {
			System.out.println(retVal);
		}

		return retVal;
	}
}

coldfusion


CF Quick Reference


Ginger CMS
the future of cms, a simple, easy and intutive content management system ... more


CFTurbine
cf prototyping engine, generates boilerplate code and ... more


Jrun monitor
monitor and timely auto-restart to avoid Jrun hang ... more


Inheritance Config.
uses OOPs inheritance to create configuration file ... more


Real Estate App.
complete real estate application using data from MLS ... more


Search Engine Lite
create your own search engine for your web site ... more