Dmitri E
Dmitri E

Reputation: 253

NetSuite SuiteScript, prevent script from firing when API call is done

I cannot seem to come up with a search for my question/problem. Please advise.

When I make an API call to NetSuite (either insert new record or update existing one), SuiteScript is triggered and posts back to my system (posting is expected, trigger on the API call is not).

I found a way how to limit script to interact only with UI actions:

    if (scriptContext.type !== scriptContext.UserEventType.CREATE &&
        scriptContext.type !== scriptContext.UserEventType.EDIT) {
          return;
    }

However, it still fires when API call is done. Any ideas or anyone can point me in the right direction?

Here is the whole script for reference:

 *@NScriptType UserEventScript
 */

define(["N/record", "./cm/graphRequest", "./cm/hubRequest"],
    function(record, graphRequest, hubRequest) {
        function onSubmitProcessing(scriptContext) {
            if (scriptContext.type !== scriptContext.UserEventType.CREATE &&
                scriptContext.type !== scriptContext.UserEventType.EDIT) {
                return;
            }

            var token = graphRequest.getToken();
            var currentRecord = scriptContext.newRecord;
            var vendorRecord = record.load({
                type: record.Type.INVENTORY_ITEM,
                id: currentRecord.id,
                isDynamic: true
            });

            var payload = JSON.stringify(vendorRecord);

            var sendObject = {
                hubAccessToken: token.access_token,
                body: payload,
                method: "/api/Part/UpdateFromNetSuite",
                url: "https://requestinspector.com/inspect/<<some value for testing>>"
            };
            var response = hubRequest.post(sendObject);
            if (response.hubId !== undefined && response.hubId !== null) {
                record.submitFields({
                    type: record.Type.INVENTORY_ITEM,
                    id: currentRecord.id,
                    values: {
                        custitem_inventoryitem_hub_id: response.hubId
                    },
                    options: {
                        enableSourcing: false,
                        ignoreMandatoryFields: true
                    }
                });
            }
        }

        return {
            afterSubmit: onSubmitProcessing
        };
    });

Thanks in advance.

PS (Edit): As mentioned below in the answer, here is what I have successfully missed:

Customizations/Scripting/Script Deployments/[desired script]/Edit/Context Filtering/

There, in "Execution Context" select desired items. In my case it was only "User Interface", which needed to be selected. Save and problem solved. Thanks Jala for pointing me in the right direction.

Upvotes: 0

Views: 2144

Answers (2)

Mikemacx
Mikemacx

Reputation: 31

You can trick it into not executing on beforeSubmit or afterSubmit by checking if there is a value for field copyId before starting the script process. i just had this problem on item when they use copy and it was causing my script to fail. Add this right after const afterSubmit = (context) => {var myvar = context.newRecord;

var copyId = newItem.getValue({
                  fieldId: 'copyid'
                });
      log.debug({title:copyId})
      if(copyId)
        return;

Upvotes: 0

Jala
Jala

Reputation: 919

On the script deployment record, there is a Context Filtering tab that will let you set what context the script will execute in.

N/runtime also has runtime.executionContext and runtime.ContextType.

Upvotes: 2

Related Questions