Friday, October 5, 2018

POST to an Externally Available Suitelet using C# Standard Libraries

Here is the sample code written C# Console Application which posts data into an Externally Available Suitelet.  

Use case in this scenario the Suitelet is updating a Phone Call Record based on the query string paramters from the C# application.


C# Code

using System;
using System.Net;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //URL includes the query string for the internal id for the record to be updated and the phone that will be used for the record update
            Uri url = new Uri("https://forms.netsuite.com/app/site/hosting/scriptlet.nl?script=97&deploy=1&compid=TSTDRV799164&h=fda074e46d984a31121e&internalid=162&phone=999-999-9999");


            HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);


            myHttpWebRequest.Method = "POST"; // Set method POST or GET
            myHttpWebRequest.UserAgent = "Mozilla/5.0"; // Set the user agent which is the type of browser
            myHttpWebRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8"; // Set the Content type
        


            // Assign the response object of 'HttpWebRequest' to a 'HttpWebResponse' variable.
            // Calls the Suitelet which will process and process the response
            HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();


            // Retrieves the response from the Suitelet 
            Stream streamResponse = myHttpWebResponse.GetResponseStream();
            
            StreamReader streamRead = new StreamReader(streamResponse);
            Char[] readBuff = new Char[256];
            int count = streamRead.Read(readBuff, 0, 256);


            //Write the response on the console
            Console.WriteLine("Suitelet Response :\n");            
            while (count > 0)
            {
                String outputData = new String(readBuff, 0, count);
                Console.Write(outputData);
                count = streamRead.Read(readBuff, 0, 256);
            }
            // Release the response object resources.
            streamRead.Close();
            streamResponse.Close();
            myHttpWebResponse.Close();
        }
    }
}


Suitelet Code 

function callSuitelet(request, response)
{
if (request.getMethod() == 'POST')
{
//retrieve the parameters field values
var id = request.getParameter('internalid');
var phone = request.getParameter('phone');

//load and save the Phone Call record
var eventRec = nlapiLoadRecord('phonecall',id);
eventRec.setFieldValue('phone', phone);
var successid = nlapiSubmitRecord(eventRec, false, false);

//Write the Suitelet Response
response.write('Phone Call Record: ' + id + ' has been successfully updated updated.');
}
}
 

 Console Output 


 

 Updated Phone Call Record 

No comments:

Post a Comment