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="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://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.

 

Be Sociable, Share!

Marty Zigman

Holding all three official certifications, Marty is regarded as the top NetSuite expert and leads a team of senior professionals at Prolecto Resources, Inc. He is a former Deloitte & Touche CPA and has held CTO roles. For over 30 years, Marty has produced leadership in ERP, CRM and eCommerce business systems. Contact Marty to set up a conversation.

More Posts - Website - Twitter - Facebook - LinkedIn - YouTube

About Marty Zigman

Holding all three official certifications, Marty is regarded as the top NetSuite expert and leads a team of senior professionals at Prolecto Resources, Inc. He is a former Deloitte & Touche CPA and has held CTO roles. For over 30 years, Marty has produced leadership in ERP, CRM and eCommerce business systems. Contact Marty to set up a conversation.

Biography • Website • X (Twitter) • Facebook • LinkedIn • YouTube

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

  1. kumar says:

    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.

  2. Marty Zigman says:

    Kumar,

    I would recommend not calling out to anything in a user event that may not respond fast. I suspect you are getting timeout errors. Instead, call out to a scheduled job that can do work for you while returning quickly to the user. These jobs have longer wait times. It may require thinking about your work in a different way.

    Marty

  3. phani kumar says:

    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

  4. Rajitha K S says:

    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?

  5. Marty Zigman says:

    Rajitha,

    Are you able to make your call using tools such as Postman to confirm the remote system will accept? The challenge we have seen in some situations is that remote systems demand certain header information and it takes some effort to get the calls right from within NetSuite outbound.

    Marty

  6. Shreekant says:

    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

  7. Marty Zigman says:

    Hello Shreekant,

    There are no public REST Urls (except for getting data centers). You need to develop your own REST service and then publish it. Review this article to give you guidance: https://blog.prolecto.com/2011/12/18/netsuite-restlet-sample-program-exploits-new-power/

    Marty

  8. Ashbaq says:

    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.

  9. Marty Zigman says:

    It looks like you are making a number of mistakes with authentication. I plan to write an article for token based authentication for web services. For now, you can reference this article: https://blog.prolecto.com/2017/11/11/download-a-java-application-to-connect-to-netsuite-oauth-tba-restlet-endpoints/ or this article: https://blog.prolecto.com/2017/10/14/download-netsuite-oauth-token-based-authentication-sample-node-js-program/

Leave a Reply

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