netsuitesuitescript

trigger call to external webservice on record change in netsuite


Let's say I want to add, change, or delete a PO line in NetSuite, and I need another system to know when I do that. How do I go about doing it?

I am using a PO line as an example only, but in theory I am looking for a programming pattern that could be applied to, for instance, item updates, or sales order changes.

I am thinking it has something to do with SuiteScript, and specifically the user events, but I am little unclear on it on HOW to do it. I am not talking about the communication with the webservice - that is actually the easiest part. I am talking about what to put into the SuiteScript code. My current working there is that it will be something like this (this is not perfect JavaScript, treat it more like pseudo-code):

function main ()
{
   return {
      afterSubmit: myAfterSubmit
      beforeSubmit: myBeforeSubmit
   }
}

function myBeforeSubmit(context)
{
    if (context.type == context.UserEventType.DELETE)
    {
        // Talk to other webservice, telling other system to delete record
    }
}

function myAfterSubmit(context)
{
    if (context.type == context.UserEventType.CREATE )
    {
        // Talk to other webservice, telling other system to add record
    }
    else if (context.type !== context.UserEventType.DELETE )
    {
        /* Talk to other webservice, telling other system to update record
         * This will be approve, cancel, reject, pack, ship, and edit, will probably
         * need to ignore some other UserEventType's, or just handle certain event types.
         */
    }
}

Am I at least heading in generally the correct direction. What especially confuses me from everything I have seen online is the "Delete" event.

This post is quite close, but doesn't really give more full fledged examples, examples that show the general workflow. Also, I found this post, but it discussing using events when other systems are making an external API call to Netsuite.

Since I am new to this. there is obviously more I need to learn, but I just want to know if I am heading in close to the correct direction. Some of this I got from the documentation here, but this documentation doesn't really address what events can and should be handled in each "submit" function.


Solution

    1. Use afterSubmit rather than beforeSubmit. If you use beforeSubmit, other scripts could change the record before it's committed to the database.
    2. The pattern you're using for your functions looks fine.
    3. The pattern for your overall script does not look correct to me. NetSuite expects an AMD module as your script and it should have a pattern broadly like the following:
    /**
     * @NScriptType UserEventScript
     * @NApiVersion 2.1
     *
     */
    
    define(["N/https"], function (https) {
    
        function beforeLoad(context) {
            ...
        }
        function beforeSubmit(context) {
            ...
        }
        function afterSubmit(context) {
            ...
        }
        return {
            beforeLoad: beforeLoad,
            beforeSubmit: beforeSubmit,
            afterSubmit: afterSubmit
        }
    });
    

    Note the mandatory JSDoc tags (@NScriptType and @ApiVersion) at the start of the file, the AMD module inclusion on the first line of the define call, and the return statement which passes the functions from the script as values for keys which match what NetSuite expects from this script type.