0
votes

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!

3

3 Answers

0
votes

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.

1
votes

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 });
}
0
votes

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