0

I am very new to NetSuite and I don't know if this makes sense. I have a requirement to send a rest call when an Invoice is created in NetSuite.

I saw that it is achievable using SuiteScript using nlapiRequestURL method.

define(['N/record'],
function(record)
{
        function afterSubmit(context) 
        {
        var url = 'myURLHere';
        var payload = 'myBodyHere';      
        var response = nlapiRequestURL(url, payload);
        }

        return {                             
           afterSubmit: afterSubmit
        };
 }

This looks like a trigger to me. But when is it invoked? How do I specify that this should run only on Invoice record insert?

Thanks in advance!

Venkataraman
  • 151
  • 3
  • 10

3 Answers3

1

You code is for SuiteScript 2.0 and nlapiRequestURL() is SuiteScript 1.0. You can try below

function afterSubmit(scriptContext) {
    var type = scriptContext.type;
    if(type == 'create'){ //Only when a new record is created in system
        var myUrl = 'http://www.google.com';
        var payload = 'myBodyHere';
        var response = http.get({ url: myUrl }); //Try exploring more methods here
    }
    log.debug({title: 'response ', details: response });
}
NetSuite Help
  • 218
  • 3
  • 12
  • Thank you for your response! I will try this.. However, how do we specify that this should be done only when an "Invoice" is created and not a Sales Order? Is that through configuration? Sorry if I am not making sense! – Venkataraman Mar 08 '18 at 17:31
  • Actually I figured out how to deploy it to an entity (Applies to). But now I get 'http' is not defined error. I googled this and understood that I need to include 'N/http' along with 'N/record'. But the error still persists. Any idea? – Venkataraman Mar 08 '18 at 22:14
  • "I get 'http' is not defined error" -> You need to import the https module as a dependency. `define(['N/record', 'N/https'], function(record, https)` Note that I specified https and not http, the https module must be used for https calls. – playalpha Mar 09 '18 at 16:57
  • @playalpha That works!! thanks a ton! I imported the module but did not add it in as a parameter in the function. – Venkataraman Mar 12 '18 at 16:58
  • @NetSuiteGuru earlier I was doing this -> `define(['N/record', 'N/https'], function(record)`.. Now I changed it to `define(['N/record', 'N/https'], function(record, https)` – Venkataraman Mar 12 '18 at 17:02
0

I do this with a script (suitescript 1.0) that looks like this:

function notifySubmit() {
    // build request url, headers, and post data...
    ...
    // when I'm sending the submit notification to my API, I want to know
    // what record is being submitted and who is submitting, so some of the
    // data I'll add to by postData object is:
    var recordType = nlapiGetRecordType();
    var internalId = nlapiGetRecordId();
    var requestUser = nlapiGetUser();
    ...

    // send the request
    return nlapiRequestURL(url, JSON.stringify(postData), headers);
}

I upload the file with this function in it. Then I create a new script record (at Customization > Scripting > Scripts > New) attached to that file, with a type of "User Event".

On that script record, Netsuite has form fields to specify the function to be called before load, before submit, and after submit. Since I only have a single function, and I want that function to be called after a record has been submitted, I set the value of the "After Submit Function" field to "notifySubmit".

Scripts are attached to record types through "Script Deployments". After saving the script record, hit "Deploy Script". That takes you to a new page for a script deployment, where in the "Applies To" field you would select "Invoice". You'll probably need to set the status, logging level, and audience of the deployment as well.

The relationship of script to script deployment is one to many. So if later you decide you want to send the notification when sales orders and purchase orders are created, you'll still just have the one script record, but that single script record will have 3 script deployments (one for invoice, one for sales order, one for purchase order).

SuiteAnswer 29246 explains this whole script creation and deployment process in more detail.

NetSweet
  • 58
  • 4
  • Thanks a lot for the deployment part! That makes sense. However, I prefer to do this with 2.0. I am currently getting 'http' is not defined error. I googled this and understood that I need to include 'N/http' along with 'N/record'. But the error still persists. Any idea? – Venkataraman Mar 08 '18 at 22:17
0

How you want to do this depends on when your script runs. Since you have a method called "afterSubmit", this looks like a user event script - if so, set this script to run when an Invoice is created. You can do this by creating a new script: Customization -> scripting -> scripts -> new Script.

Select the script file you created and create the script record. Select "User Event" as the script type. When deploying this script, apply the deployment to the Invoice record and "Create" event.

My script is applied to Purchase Orders, just apply this to Invoice. See screenshots below: PO script deployment

Charl
  • 812
  • 1
  • 8
  • 22