Follow Us On social
5717 Legacy Dr, Ste 250, Plano TX 75024 info@79consulting.com +1 (469) 606-9656

Connecting to NetSuite Using Web Service Consumer in Mulesoft

In MuleSoft, we can use the Web Service Consumer to connect to NetSuite, allowing us to access and interact with NetSuite data efficiently. It supports operations such as sending SOAP requests, handling authentication, and processing paginated results.

However, direct SOAP integration may pose challenges with token-based authentication and XML transformation, particularly when dealing with complex workflows. The following steps outline how to set up and execute a connection to NetSuite using MuleSoft.

Prerequisites

  1. NetSuite credentials:
    • Account ID
    • Consumer Key
    • Consumer Secret
    • Token ID
    • Token Secret
  2. MuleSoft Anypoint Studio.
  3. SOAP Web Services are enabled in your NetSuite account.

 

Web Service Consumer Connector

 

The Web Service Consumer connector in Mule 4 is a powerful tool that makes it simple to integrate with SOAP-based web services. The Web Service Consumer connector is ideal for consuming WSDL-based SOAP services, especially when no pre-built connector exists. It also supports advanced use cases like handling large datasets with pagination.

 

The Web Service Consumer supports SOAP multipart messages, headers, and attachments with embedded DataWeave transformations for enhanced flexibility.

 

Retrieving a Saved Search from Netsuite using Web service consumer

 

Step 1: Setting Up the Connector Configuration

MuleSoft Web Service Consumer

 

To get started, you’ll need to:

    1. Choose Your SOAP Version
      Select this from a dropdown in the connector configuration.
    2. Provide the WSDL Location
      The connector extracts service, port, and endpoint details from the WSDL. SOAP web services use a WSDL file that describes all supported operations and messages. This file location can be found in Netsuite WSDL Documentation.
    3. Provide the Address
      Change the URL with the Netsuite Account Id “https://<account_id>.suitetalk.api.netsuite.com/services/NetSuitePort_2024_1

MuleSoft Web Service Consumer

or login into your Netsuite account, Navigate Setup> Company > Company Information and click the Company URLs subtab to view the account-specific domains. Use the Suite Talk URL. Once the Connection is done we can select the operations that are available in the WSDL file.

 

When calling a SOAP operation, you’ll encounter three main components:

  • Headers: These can include authentication tokens, payment details, or other metadata.
  • Body: Holds the primary data. Mule allows DataWeave scripts to dynamically construct XML requests here.
  • Attachments: Useful for sending supplementary files

MuleSoft Web Service Consumer

 

Step 2: Authentication Setup in Header: 

To connect with NetSuite using SOAP, you must generate a token-based authentication header dynamically for each request. Use the following steps to generate the tokenPassport header dynamically in a DataWeave script.

  • Account ID
  • Consumer Key
  • Token ID
  • Nonce: This is a unique, random string generated for the request to ensure no two requests are identical. It is created by splitting a UUID, joining the pieces, and taking the first 20 characters ((uuid() splitBy “-” )joinBy “”)[1 to 20].
  • Timestamp: The current UNIX timestamp, generated using the MuleSoft expression (now() >> “GMT”) as Number. This ensures the request has a time validity and matches Netsuite’s requirement for timestamp-based security.
  • Signature: 
  • Base String: A concatenation of critical authentication parameters (account, consumerKey, tokenId, randomNum, and current), joined with “&”.
  • Key String: A concatenation of the consumerSecret and tokenSecret, also joined with “&”.
  • Hash: A HMAC-SHA256 hash is generated using the keyString as the key and the baseString as the data. The result is encoded in Base64 and assigned to the signature

 

Dataweave Code for Header:

import dw::Crypto

import * from dw::core::Binaries

 

ns platformMsgs urn:messages_2024_1.platform.webservices.netsuite.com

ns core urn:core_2024_1.platform.webservices.netsuite.com

ns ns0 urn:messages_2024_1.platform.webservices.netsuite.com

ns ns01 urn:core_2024_1.platform.webservices.netsuite.com

 

var accountId = p(‘AccountId’)

var consumerKey = p(‘ConsumerKey’)

var tokenId = p(‘TokenId’)

var consumerSecret = p(‘consumerSecretret’)

var tokenSecret = p(‘tokenSecretret’)

var randomNum = ((uuid() splitBy “-“) joinBy  “”)[1 to 20]

var timeStamp = (now() >> “GMT”) as Number

var baseString = [accountId,consumerKey,tokenId,randomNum,timeStamp] joinBy  ‘&’

var keyString = [consumerSecret,tokenSecret] joinBy  ‘&’

var afterHash = toBase64((Crypto::HMACBinary(keyString, baseString, “HmacSHA256”)))

{

    headers: {

     platformMsgs#searchPreferences:{

            pageSize: 100,

            bodyFieldsOnly:false

        },

        platformMsgs#tokenPassport: {

            core#account: accountId,

            core#consumerKey: consumerKey,

            core#token: tokenId,

            core#nonce: randomNum,

            core#timestamp: timeStamp,

            core#signature @(“algorithm”: “HMAC-SHA256”): afterHash

        }

    }

}

 

In the headers, add the search preferences with a page size of 10. This will paginate the results, retrieving only the first 10 items of the search.

Step 3: SOAP Request for Saved Search

Configure the Web Service Consumer to point to the NetSuite SOAP API endpoint, In the SOAP body Transform the request payload into a SOAP-compatible XML using Dataweave. Use the SavedSearchId to construct a SOAP request with the appropriate NetSuite namespace that retrieves the saved search from NetSuite. 

MuleSoft Web Service Consumer

MuleSoft Web Service Consumer

This request will return the first 10 results, along with the “searchId”, “pageSize”and the current index in the field “PageIndex”. These values can then be used to retrieve subsequent pages by invoking another Web Service Consumer operation with the SearchMoreWithId operation.

Implementing Pagination for Saved Searches Using Web Service Consumer

Use the searchMoreWithId SOAP operation to handle paginated results. This is implemented in the MuleSoft flow

  • HTTP Listener triggers the flow when an HTTP request is received.
  • Web Service Consumer (WSC):
    • With an operation “search”
    • Calls NetSuite’s SOAP API to execute a saved search to retrieve inventory items based on a saved search ID.
    • SOAP Body (wsc:body): Constructs a SOAP request using DataWeave with the saved search ID which performs an ItemSearchAdvanced for inventory items.

MuleSoft Web Service Consumer

  • SOAP Headers (wsc:headers): Include the searchPreferences with pageSize = 10 and tokenPassport for Authentication.

MuleSoft Web Service Consumer

  • The response data will include the pageIndex (set to 1 Indicating the first page), along with totalPages and searchId, which can be used to retrieve subsequent pages.

MuleSoft Web Service Consumer

  • Transform the response data from the Web Service Consumer and set a variable “SavedSearchId” with “searchId”. Use the “totalPages” in the payload to create an array of Indexes using Dataweave code.

“(1 to payload.body.searchResponse.searchResult.totalPages as Number)” 

For example, if the “totalPages” are 10 then the payload will be [1,2,3,4,5,6,7,8,9,10]

MuleSoft Web Service Consumer

Parallel For Each:  Iterates over the array of Indexes in parallel, to process multiple records concurrently. This can speed up processing if there are many records to handle.

  • For each index, a new Web Service call is made. 
  • SOAP Body (wsc:body): Iterate through each page using searchMoreWithId and the Index using the Dataweave inside the Body of the Web Service Consumer.

MuleSoft Web Service Consumer

  • SOAP Headers (wsc:headers): Include the tokenPassport for Authentication.

 

  • After Parallel For Each add a transform message that flatten the data we get from the Parallel for each.

MuleSoft Web Service Consumer

 

MuleSoft Web Service Consumer

Post a Comment

Top