httppostnetsuitesuitescript

HTTP Post From NetSuite SuiteScript To Boomi Endpoint


I've recently started a project where I'll need to make an HTTP Post request from NetSuite to a Boomi endpoint. I've found this really good example to go off of but it's missing what seem to be crucial libraries in step #8 for the SuiteScript to work properly.

The code below is what I've taken out and changed to try and suit my needs and it runs without error inside of NetSuite (as far as I can tell), but doesn't seem to make the connection to the endpoint. I'm hoping to find a simplified version of this to make the connection to the endpoint with basic auth and pass minimal XML/JSON data over.

function Customer_callBoomi()
{
    var objType = nlapiGetContext().getSetting('SCRIPT', 'custscriptset_value_object_type');
    var BOOMI_USER = nlapiGetContext().getSetting('SCRIPT', 'custscriptset_value_boomi_user');
    var BOOMI_PASSWORD = nlapiGetContext().getSetting('SCRIPT', 'custscriptset_value_boomi_password'); 
    var BOOMI_URL = nlapiGetContext().getSetting('SCRIPT', 'custscriptset_value_boomi_url');

    try
    {
    /*Get Record Internal ID.*/
    var customerId = nlapiGetRecordId();
    var stInputXML = '<ID value=\"' + customerId + '\" type=\"' + objType + '\"/>';

    /*create authentication header.*/
    var arBoomiHeader = new Array();
    var sAuthoizationString = Base64.encode(BOOMI_USER + ':' + BOOMI_PASSWORD);
    arBoomiHeader['Content-Type']= 'application/xml';
    arBoomiHeader["Authorization"] = "Basic " + sAuthoizationString;

    // Suitelet invokes Boomi's real time integration workflow and receives response.
    var objBoomiResponse = nlapiRequestURL(BOOMI_URL,stInputXML,arBoomiHeader);

    // If there is no timout exception then
    if (objBoomiResponse)
    { 
        // if response status is Success
        if ((objBoomiResponse.getCode() == '200') || (objBoomiResponse.getCode() == '201') || (objBoomiResponse.getCode() == '202')) 
        {
            // Success!
        } else {
            // something bad happened
        }
    }
    }
    catch (e) 
    {
        var stExceptionDetails = '';
        if (e.getDetails != undefined)
        {
            stExceptionDetails = e.getCode() + ': ' + e.getDetails();
        }
        else 
        {
            stExceptionDetails = e.toString();
        }   
    }
}

This is the original code:

When I tried to run this without the aforementioned libraries I got an error similar to the one below except it said "logger". Which is why I went down the path of removing the mentions of that object to accommodate for the missing libraries:

enter image description here

// Script for Real Time Event Trigger
var stLoggerTitle = 'Real-Time Integration with BOomi';
function EmployeeUpsert_callBoomi(type)
{
    var logger = new Logger();
    logger.enableDebug();
    logger.debug('Entry Log', 'Begin EmployeeUpsert_callBoomi');
    var objType = nlapiGetContext().getSetting('SCRIPT', 'custscriptset_value_object_type');
    var BOOMI_USER = nlapiGetContext().getSetting('SCRIPT', 'custscriptset_value_boomi_user');
    var BOOMI_PASSWORD = nlapiGetContext().getSetting('SCRIPT', 'custscriptset_value_boomi_password'); 
    var BOOMI_URL = nlapiGetContext().getSetting('SCRIPT', 'custscriptset_value_boomi_url');
        logger.debug('BOOMI_USER',BOOMI_USER);
        logger.debug('BOOMI_PASSWORD', BOOMI_PASSWORD);
        logger.debug('BOOMI_URL', BOOMI_URL);
        try
        {
        /*Get Record Internal ID.*/
        var sEmployeeRecIntId = nlapiGetRecordId();
        var stInputXML = '<ID value=\"' + sEmployeeRecIntId + '\" type=\"' + objType + '\"/>';
        logger.debug('Record ID', sEmployeeRecIntId);
        logger.debug('Object Type', objType);
        /*create authentication header.*/
        var arBoomiHeader = new Array();
        var sAuthoizationString = Base64.encode(BOOMI_USER + ':' + BOOMI_PASSWORD);
        arBoomiHeader['Content-Type']= 'application/xml';
        arBoomiHeader["Authorization"] = "Basic " + sAuthoizationString;
        // Suitelet invokes Boomi's real time integration workflow and receives response.
        logger.debug(stLoggerTitle, '['+new Date() + ']Initiate Boomi Request');
        var objBoomiResponse = nlapiRequestURL(BOOMI_URL,stInputXML,arBoomiHeader);
        logger.debug(stLoggerTitle, '['+new Date() + ']Process Boomi Response');
        // If there is no timout exception then
        if (objBoomiResponse)
        {
            logger.debug(stLoggerTitle,'HTTP Response Code=' + objBoomiResponse.getCode());
                
            // if response status is Success
            if ((objBoomiResponse.getCode() == '200') || (objBoomiResponse.getCode() == '201') || (objBoomiResponse.getCode() == '202')) 
            {
                // Success!
            } else {
                // something bad happened
            }
        }
       }
       catch (e) 
       {
            var stExceptionDetails = '';
            if (e.getDetails != undefined)
            {
                stExceptionDetails = e.getCode() + ': ' + e.getDetails();
            }
            else 
            {
                stExceptionDetails = e.toString();
            }
        // login execution logs
            logger.error(stLoggerTitle, 'Exception Details: '+stExceptionDetails);    
       }
    logger.debug('Exit Log', 'Exit EmployeeUpsert_callBoomi Successfully.');
}

Solution

  • Figured it out

    1. The username and password for basic authentication the instanceID and the Authentication Token forgot about that quirk.
    2. The Script and file name need to be aligned and also have ".js" at the end, like this --> BoomiTestv5.js
    3. I used the actual Id values for the parameters inside the code

    Modified code:

    define(['N/https', 'N/log', 'N/runtime', 'N/encode'], function(https, log, runtime, encode) {
    
        function afterSubmit(context) {
            try {
                // Retrieve script parameters
                var endpointUrl = runtime.getCurrentScript().getParameter({
                    name: 'custscript5'
                });
                var username = runtime.getCurrentScript().getParameter({
                    name: 'custscript6'
                });
                var password = runtime.getCurrentScript().getParameter({
                    name: 'custscript7'
                });
    
                // Get internal ID of the current record
                var recordId = context.newRecord.id;
                
                // Define your payload
                var payload = {
                    recordId: recordId
                };
                
                // Convert payload to string
                var payloadString = JSON.stringify(payload);
                
                // Define headers
                var arBoomiHeader = new Array();
                var sAuthoizationString = encodeBase64(username + ':' + password);
                arBoomiHeader['Content-Type']= 'application/JSON';
                arBoomiHeader["Authorization"] = "Basic " + sAuthoizationString;
              
                // Send HTTPS POST request
                var response = https.post({
                    url: endpointUrl,
                    body: payloadString,
                    headers: arBoomiHeader
                });
                
                // Log response
                log.debug({
                    title: 'HTTPS Response',
                    details: response.body
                });
                
                // Process response as needed
                
            } catch (e) {
                // Log error
                log.error({
                    title: 'Error',
                    details: e
                });
            }
        }
    
        // Function to encode a string to base64
        function encodeBase64(str){
                var reencoded = encode.convert({
                string: str,
                inputEncoding: encode.Encoding.UTF_8,
                outputEncoding: encode.Encoding.BASE_64,
                });
          return reencoded;
        }
    
        return {
            afterSubmit: afterSubmit
        };
    });