【问题标题】:How to prevent error being displayed in NetSuite?如何防止 NetSuite 中显示错误?
【发布时间】:2018-10-24 19:21:11
【问题描述】:

我正在编写一个脚本来从一个字段中获取一个值并将其放入 NetSuite 中的一个列表字段中。我认为问题是因为在保存记录时,试图在不包含该值的列表中设置一个值。

我希望在我的脚本上下文中设置时静默失败,如何防止出现错误消息并允许创建记录但不填充该字段?

场景 - 值被放入此字段,脚本尝试将该值映射到列表值(它不存在),记录仍应保存,但没有设置该数据 - 没有错误。

/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 */
define(['N/record', 'N/log'],
/**
 * @param {record} record
 */
function(record) {

    function customer_beforeLoad(scriptContext) {

    }

    function customer_beforeSubmit(scriptContext) {

        //Segment
        setNonIntegrationFieldValue(scriptContext, 'custentity_cus_segmentintegration', 'custentity_cus_segment');
        //Currency
        setNonIntegrationFieldValue(scriptContext, 'custentity_cus_primarycurrencyintegratio', 'currency');
        //Billing Cycle
        setNonIntegrationFieldValue(scriptContext, 'custentity_cus_billingcycleintegration', 'custentity_cus_billingcycle');
        //Type
        setNonIntegrationFieldValue(scriptContext, 'custentity_cus_typeintegration', 'custentity_cus_type');
        //Industry
        setNonIntegrationFieldValue(scriptContext, 'custentity_cus_industryintegration', 'custentity_esc_industry');
        //Sales Rep
        setNonIntegrationFieldValue(scriptContext, 'custentity_cus_salesrepintegration', 'salesrep');
    }

    function customer_afterSubmit(scriptContext) {

    }

    function setNonIntegrationFieldValue(scriptContext, integrationFieldName, actualFieldName){
      try {
        var integrationFieldValue = scriptContext.newRecord.getValue(integrationFieldName);
        if(integrationFieldValue == '' || integrationFieldValue == null){
            scriptContext.newRecord.setValue({
                fieldId: actualFieldName,
                value: ''
            });
        } else {
            scriptContext.newRecord.setText({
                fieldId: actualFieldName,
                text: integrationFieldValue
            });
        }
      } catch(e){
        log.error({
          title: "setNonIntegrationFieldValue() has encountered an error.",
          details: e.message
        });
        //nlapiLogExecution('ERROR','setNonIntegrationFieldValue() has encountered an error.', errText(e));
      }
    }

    return {
        //beforeLoad: customer_beforeLoad,
        beforeSubmit: customer_beforeSubmit,
        //afterSubmit: customer_afterSubmit
    };

});

【问题讨论】:

    标签: javascript netsuite suitescript suitescript2.0


    【解决方案1】:

    我认为解决方案不应该阻止消息显示,而是应该阻止设置无效值时的值。您可以尝试首先获取该列表中所有有效选项的数组,然后检查您要使用的值是否包含在数组中,如果是则设置该值。

    具体来说:

    var actualField = scriptContext.form.getField(actualFieldName);
    var options = actualField.getSelectOptions();
    var integrationFieldValue = scriptContext.newRecord.getValue(integrationFieldName);
    if (options.indexOf(integrationFieldValue) > -1) {
    scriptContext.newRecord.setText({
                    fieldId: actualFieldName,
                    text: integrationFieldValue
                });
    }
    

    getSelectOptions函数的更多详细信息,请参考Netsuite帮助中心(搜索Field.getSelectOptions)

    【讨论】:

      【解决方案2】:

      只需用 try catch 包装任何和所有网络套件逻辑。我们在 netsuite 中有超过 614,00 个 LOC,这 100% 有效:

      try {
         // your code here
      } catch (e) {
         // this ternary operator will catch all the ns errors. you can fail silently here
         var error = e.details || e.message || e.toString();
         throw error;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-12
        • 1970-01-01
        • 2016-03-12
        • 1970-01-01
        相关资源
        最近更新 更多