Marty Zigman

Conversations with Marty Zigman

Certified Administrator • ERP • SuiteCloud

Pattern to Make SuiteScript Call to NetSuite SuiteTalk Web Services

NetSuite Technical

Tags: , , ,

This article is relevant if you are trying to make an NetSuite Web Services call and you are in SuiteScript, not an external application.

Background

When most people talk about NetSuite’s Web Services technologies, they are referencing how external applications can interact with the NetSuite platform. However, consider the case where you need to make a NetSuite SuiteScript call to NetSuite’s Web Services.  We see this use case come up where there is a NetSuite function that is supported in the SuiteTalk technologies but is not supported in the SuiteScript area.  In these situations, while we wish we wouldn’t have to, we need to call out to Web Services to take care of the business problem.

Technically Not Best Practice but Gets the Job Done

NetSuite is clear that they do not want NetSuite authentication credentials stored in custom fields to make calls to the Web Services tier.  Instead, you should use a Single Sign On approach to configure your application to allow for access to NetSuite’s Web Services.   Single Sign on has overhead and is a discussion in itself.   However, not withstanding the credential consideration, you may be wondering how to make a SuiteScript call out to a NetSuite SuiteTalk Web Services.

Example SuiteScript to SuiteTalk Functions

The following custom functions can help you see the code pattern.  The first function builds the XML SOAP message.  Notice the credential information is stored in message body.  The next function formats the request to to include the necessary HTTPS headers as well as indicate the reference method that will be called.  Once you get the result back from your call, you will need to parse the XML response to look for the information you need.

function xml_ws(sWS_URL, sWS_Acct, sWS_User, sWS_Pw, sWS_Role, period_id) {

	return '<soap:Envelope xmlns:xsd="https://www.w3.org/2001/XMLSchema" xmlns:soap="https://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance">' +
    '<soap:Header>'+
        '<passport xmlns="urn:messages_2013_1.platform.webservices.netsuite.com">'+
            '<email xmlns="urn:core_2013_1.platform.webservices.netsuite.com">'+sWS_User+'</email>'+
            '<password xmlns="urn:core_2013_1.platform.webservices.netsuite.com">'+sWS_Pw+'</password>'+
            '<account xmlns="urn:core_2013_1.platform.webservices.netsuite.com">'+sWS_Acct+'</account>'+
            '<role internalId="'+sWS_Role+'" xmlns="urn:core_2013_1.platform.webservices.netsuite.com"/>'+
        '</passport>'+
        '<preferences xmlns="urn:messages_2013_1.platform.webservices.netsuite.com">'+
            '<warningAsError>false</warningAsError>'+
        '</preferences>'+
    '</soap:Header>'+
    '<soap:Body>'+
		'<getConsolidatedExchangeRate>'+
	        '<consolidatedExchangeRateFilter>'+
	           '<period internalId="'+period_id+'" />'+
	           '<toSubsidiary internalId="1" />'+
	        '</consolidatedExchangeRateFilter>'+
         '</getConsolidatedExchangeRate>'+
      '</soap:Body>'+
   '</soap:Envelope>';
}

//sample soap_action
var soap_action = 'getPostingTransactionSummary';

function invoke_ws(xml, soap_action, sUrl){

    //Set up Headers
    var headers = new Array();
    headers['User-Agent-x'] = 'SuiteScript-Call';
    headers['Content-Type'] = 'text/xml';
    headers['SOAPAction'] = soap_action;

    var resp = null;
    if (xml)  {
    	// blindly re-try upon error
    	try{
    		resp = nlapiRequestURL( sUrl, xml , headers );
    	}catch(e){
    		resp = nlapiRequestURL( sUrl, xml , headers );
    	}
    } else {
    	resp = nlapiRequestURL( sUrl, null , headers );
    }
    return resp.getBody();
}

These functions can be excuted in NetSuite UserEvents (remember, they may be slow), SuiteLets, or ideally, in scheduled scripts.

Get Assistance

Because of its ability to be adapted and programmed, the NetSuite platform is excellent for taking care of business problems.  Contact us if you are looking to get more out of your NetSuite account.

 

Marty Zigman LinkedIn

Marty Zigman

Holding three official certifications, Marty is widely recognized as a top NetSuite expert and leads a team of senior professionals at Prolecto Resources, Inc. A former Deloitte & Touche CPA and technology executive with CTO roles, he brings over 35 years of leadership in ERP, CRM, and eCommerce business systems. Contact Marty to engage directly.

BiographyYouTubeLinkedInX (Twitter)

10 thoughts on “Pattern to Make SuiteScript Call to NetSuite SuiteTalk Web Services

  1. Hi sir…Thnx for the All U R doing…Please Let me know this….Can I call External Url (..Out side of Netsuite and it is Awaiting Request..) from User event script and send Json Text of Sales order fields with nlapiRequestURl API through user event script.. We are storing these salesorder details outside Netsuite.

    Reply
  2. Hi sir,
    How to change the focus to besides field rather than the below field when tab key pressed on the key board?

    Please clarify me sir

    Reply
  3. Hi,

    I want to call a external url from user event using nalpirequesturl. but it shows the following error
    “Authorization Required”
    This server could not verify that you are authorized to access the document requested. Either you supplied the wrong credentials (e.g., bad password), or your browser doesn’t understand how to supply the credentials required.

    var url=url;
    //build request header and auth token
    var username =’username’ ;
    var password = ‘password’;
    var cn=08384;
    var cred = ‘username’ +’:’+ ‘password’;
    var token= nlapiEncrypt(cred, ‘base64’);
    var headers = {‘Content-Type’ : ‘application/json’,
    ‘Authorization’ : token
    };

    var response = nlapiRequestURL(url, null, headers);

    Could you please share your thoughts?

    Reply
  4. i am new in netsuite. form where can i get the set of rest services to connect with netsuite and can i test if from postman

    Reply
  5. Hello Marty,

    I am Trying to call External URL its throwing 406 Error.

    var user_name = ‘username’;
    var password = ‘password’;
    var url=”Url”;
    var auth = nlapiEncrypt(user_name+’:’+password,’base64′);

    var headers = new Array();
    headers[“Content-Type”] = “application/json”;
    headers[“Authorization”] = ‘Basic ‘+auth+”;
    headers[“Token”] = ‘abcdddd-djfjjff-djd/dkdkd’;

    var token_res=nlapiRequestURL(url, null,headers);
    var token_response_XML = token_res.getBody();
    nlapiLogExecution(‘DEBUG’, ‘token_response_XML’, token_response_XML);

    Can You Suggest Please?

    Thanks in Advance.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *