NetSuite SuiteScript,防止在API调用完成时触发脚本

NetSuite SuiteScript,防止在API调用完成时触发脚本,netsuite,suitescript2.0,Netsuite,Suitescript2.0,我似乎找不出答案来回答我的问题。请告知 当我对NetSuite进行API调用(插入新记录或更新现有记录)时,会触发SuiteScript并将其发回我的系统(预计会发回,但不会触发API调用) 我找到了一种限制脚本仅与UI操作交互的方法: if (scriptContext.type !== scriptContext.UserEventType.CREATE && scriptContext.type !== scriptContext.UserEvent

我似乎找不出答案来回答我的问题。请告知

当我对NetSuite进行API调用(插入新记录或更新现有记录)时,会触发SuiteScript并将其发回我的系统(预计会发回,但不会触发API调用)

我找到了一种限制脚本仅与UI操作交互的方法:

    if (scriptContext.type !== scriptContext.UserEventType.CREATE &&
        scriptContext.type !== scriptContext.UserEventType.EDIT) {
          return;
    }
但是,当API调用完成时,它仍然会激发。有什么想法或任何人能给我指出正确的方向吗

以下是整个脚本供参考:

 *@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
        };
    });
*@NScriptType UserEventScript
*/
定义([“N/record”、“/cm/graphRequest”、“/cm/hubRequest”],
函数(记录、graphRequest、hubRequest){
函数onSubmitProcessing(脚本上下文){
if(scriptContext.type!==scriptContext.UserEventType.CREATE&&
scriptContext.type!==scriptContext.UserEventType.EDIT){
返回;
}
var token=graphRequest.getToken();
var currentRecord=scriptContext.newRecord;
var vendorRecord=record.load({
类型:record.type.INVENTORY\u物料,
id:currentRecord.id,
isDynamic:对
});
var payload=JSON.stringify(vendorRecord);
变量sendObject={
hubAccessToken:token.access\u token,
机身:有效载荷,
方法:“/api/Part/UpdateFromNetSuite”,
url:“https://requestinspector.com/inspect/"
};
var response=hubRequest.post(sendObject);
if(response.hubId!==undefined&&response.hubId!==null){
record.submitFields({
类型:record.type.INVENTORY\u物料,
id:currentRecord.id,
价值观:{
custitem\u inventoryitem\u hub\u id:response.hubId
},
选项:{
enableSourcing:错误,
ignoreMandatoryFields:true
}
});
}
}
返回{
后提交:onSubmitProcessing
};
});
提前谢谢

PS(编辑):正如下面在回答中提到的,以下是我成功错过的:

自定义/脚本/脚本部署/[所需脚本]/编辑/上下文筛选/

在“执行上下文”中,选择所需的项目。在我的例子中,只有“用户界面”需要选择。保存并解决问题。
感谢Jala为我指出了正确的方向。

在脚本部署记录上,有一个上下文筛选选项卡,可以让您设置脚本将在哪个上下文中执行


N/runtime还有runtime.executionContext和runtime.ContextType。

在脚本部署记录上,有一个上下文筛选选项卡,用于设置脚本将在哪个上下文中执行


N/runtime还有runtime.executionContext和runtime.ContextType。

脚本上下文和用户事件类型是两种不同的东西。谢谢,我有点觉得苹果不是桔子。脚本上下文和用户事件类型是两种不同的东西。谢谢,我有点觉得苹果不是桔子。酷!我完全忘记了脚本部署记录,过于关注代码而完全忽略了它。非常感谢。酷!我完全忘记了脚本部署记录,过于关注代码而完全忽略了它。非常感谢。