Friday, September 21, 2018

Call NetSuite Web Services methods on a Suitelet


Here's a sample suitelet code that contains the SOAP requests being passed to the WSDL.

 
function suiteletSoap(request, response) {
    //credentials on the other account
    var stLoginEmailAddress =
'login@account.com';
    var stLoginPassword = 'password';
 var stNSAccount = 'ACCT001';
    var stNSRole = '3';
    //var stPageSize = '1000';
    var stMemoFldValue = 'WS call via SuiteScript';
    var stSOSOAPRequest = '<soapenv:Envelope xmlns:soapenv="
http://schemas.xmlsoap.org/soap/envelope/">';
    stSOSOAPRequest = stSOSOAPRequest + '<soapenv:Header>';
 stSOSOAPRequest = stSOSOAPRequest + '<platformMsgs:passport ';
    stSOSOAPRequest = stSOSOAPRequest + 'xmlns:platformCore="urn:core_2010_1.platform.webservices.netsuite.com" ';
    stSOSOAPRequest = stSOSOAPRequest + 'xmlns:platformMsgs="urn:messages_2010_1.platform.webservices.netsuite.com" ';
    stSOSOAPRequest = stSOSOAPRequest + 'xmlns:soapenc="
http://schemas.xmlsoap.org/soap/encoding/" ';
    stSOSOAPRequest = stSOSOAPRequest + 'xmlns:xsd="
http://www.w3.org/2001/XMLSchema" ';
    stSOSOAPRequest = stSOSOAPRequest + 'xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance">';
    stSOSOAPRequest = stSOSOAPRequest + '<email>' + stLoginEmailAddress + '</email>';
    stSOSOAPRequest = stSOSOAPRequest + '<password>' + stLoginPassword + '</password>';
    stSOSOAPRequest = stSOSOAPRequest + '<account>' + stNSAccount + '</account>';
    stSOSOAPRequest = stSOSOAPRequest + '<role internalId="3"/>';
    stSOSOAPRequest = stSOSOAPRequest + '</platformMsgs:passport>';
    stSOSOAPRequest = stSOSOAPRequest + '<platformMsgs:applicationInfo ';
    stSOSOAPRequest = stSOSOAPRequest + 'xmlns:platformMsgs="urn:messages_2010_1.platform.webservices.netsuite.com" ';
    stSOSOAPRequest = stSOSOAPRequest + 'xmlns:soapenc="
http://schemas.xmlsoap.org/soap/encoding/" ';
    stSOSOAPRequest = stSOSOAPRequest + 'xmlns:xsd="
http://www.w3.org/2001/XMLSchema" ';
    stSOSOAPRequest = stSOSOAPRequest + 'xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance" />';
    stSOSOAPRequest = stSOSOAPRequest + '<platformMsgs:partnerInfo ';
    stSOSOAPRequest = stSOSOAPRequest + 'xmlns:platformMsgs="urn:messages_2010_1.platform.webservices.netsuite.com" ';
    stSOSOAPRequest = stSOSOAPRequest + 'xmlns:soapenc="
http://schemas.xmlsoap.org/soap/encoding/" ';
    stSOSOAPRequest = stSOSOAPRequest + 'xmlns:xsd="
http://www.w3.org/2001/XMLSchema" ';
    stSOSOAPRequest = stSOSOAPRequest + 'xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance" />';
    stSOSOAPRequest = stSOSOAPRequest + '<platformMsgs:preferences ';
    stSOSOAPRequest = stSOSOAPRequest + 'xmlns:platformMsgs="urn:messages_2010_1.platform.webservices.netsuite.com" ';
    stSOSOAPRequest = stSOSOAPRequest + 'xmlns:soapenc="
http://schemas.xmlsoap.org/soap/encoding/" ';
    stSOSOAPRequest = stSOSOAPRequest + 'xmlns:xsd="
http://www.w3.org/2001/XMLSchema" ';
    stSOSOAPRequest = stSOSOAPRequest + 'xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"> ';
    stSOSOAPRequest = stSOSOAPRequest + '<platformMsgs:warningAsError>false</platformMsgs:warningAsError>';
    stSOSOAPRequest = stSOSOAPRequest + '</platformMsgs:preferences>';
    stSOSOAPRequest = stSOSOAPRequest + '</soapenv:Header>';
    stSOSOAPRequest = stSOSOAPRequest + '<soapenv:Body>';
    stSOSOAPRequest = stSOSOAPRequest + '<platformMsgs:update ';
    stSOSOAPRequest = stSOSOAPRequest + 'xmlns:platformMsgs="urn:messages_2010_1.platform.webservices.netsuite.com" ';
    stSOSOAPRequest = stSOSOAPRequest + 'xmlns:soapenc="
http://schemas.xmlsoap.org/soap/encoding/" ';
    stSOSOAPRequest = stSOSOAPRequest + 'xmlns:xsd="
http://www.w3.org/2001/XMLSchema" ';
    stSOSOAPRequest = stSOSOAPRequest + 'xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance" ';
    stSOSOAPRequest = stSOSOAPRequest + 'xmlns:s0="urn:sales_2010_1.transactions.webservices.netsuite.com">';
    stSOSOAPRequest = stSOSOAPRequest + '<platformMsgs:record xsi:type="s0:SalesOrder" internalId="73">';
    stSOSOAPRequest = stSOSOAPRequest + '<s0:memo>' + stMemoFldValue + '</s0:memo>';
    stSOSOAPRequest = stSOSOAPRequest + '</platformMsgs:record>';
    stSOSOAPRequest = stSOSOAPRequest + '</platformMsgs:update>';
    stSOSOAPRequest = stSOSOAPRequest + '</soapenv:Body>';
 stSOSOAPRequest = stSOSOAPRequest + '</soapenv:Envelope>';
    var salesUpdateResponse = callNetsuiteWebService(stSOSOAPRequest, 'update');
 var stsalesUpdateResponse = salesUpdateResponse.getBody();
 var xmlsalesUpdateResponse = nlapiStringToXML(stsalesUpdateResponse);
 response.write(stSOSOAPRequest);
}

 
function callNetsuiteWebService(stSoapRequest, stSoapAction, stSessionCookie, stCookieInfo){
    var stWebServiceConsumer = 'https://webservices.netsuite.com/services/NetSuitePort_2010_1';
    if (isEmpty(stSoapRequest) || isEmpty(stSoapAction)) {
  throw nlapiCreateError('10014', 'SOAP Request and Action should not be empty.');
 }
    var stFinalCookie = stCookieInfo;
    if (isEmpty(stFinalCookie)) {
  stFinalCookie = 'NS_VER=2010.1.0';
 }
 var soapHeaders = new Array();
 soapHeaders['User-Agent-x'] = 'SuiteScript-Call';
 soapHeaders['SOAPAction'] = stSoapAction;
 soapHeaders['Host'] = "webservices.netsuite.com";
 if (!isEmpty(stSessionCookie)) {
  soapHeaders['Cookie'] = '$Version=0; ' + stFinalCookie + '; ' + stSessionCookie;
 }
 var soapResponse = nlapiRequestURL(stWebServiceConsumer, stSoapRequest, soapHeaders);
 return soapResponse;
}

function isEmpty( inputStr ) {
 if ( null == inputStr || "" == inputStr )
 { return true; }
 return false;
}

No comments:

Post a Comment