开发者问题收集

SuiteScript 2.0 TypeError 无法调用未定义的方法“getValue”

2020-12-22
2985

我尝试使用下面的代码从两个交易主体字段中获取值。

/**
 *@NApiVersion 2.x
 *@NScriptType UserEventScript
 *@param {Record} context.currentRecord
 */

define(['N/record'], 
function (msg) {
    
    function beforeSubmit(context) {
      try {

        var record = context.currentRecord;               

        var createdDate = record.getValue({
            fieldId: 'createddate'
        });

        var dataNecessidade = record.getValue({
            fieldId: 'custbodyek_data_nece_requ_po'
            
        });

        console.log(createdDate ,dataNecessidade);        

        }
        catch(ex){
        log.error(ex);
        }
    }
    return {
        beforeSubmit : beforeSubmit,
    
    };
});

引发的错误是“ TypeError:无法调用未定义的方法“getValue”

我在这里做错了什么?

谢谢!

3个回答

传递到用户事件的 context 上没有 currentRecord 属性,因此错误消息告诉您 record 未定义。查看 beforeSubmit 入口点的文档以找到适当的值。

erictgrubaugh
2020-12-22

在 SuiteScript 2 上,每个入口点都有不同的参数,因此您需要在帮助中检查这些参数,或者如果您使用 Eclipse 之类的 IDE,您将在创建新脚本时获取该信息,因此对于 UserEvent 脚本和 beforeSubmit 入口点,您将获得类似以下内容:

/**
 * Function definition to be triggered before record is loaded.
 * 
 * Task #5060 : calculate PO Spent Amount and Balance in realtime
 *
 * @param {Object} scriptContext
 * @param {Record} scriptContext.newRecord - New record
 * @param {Record} scriptContext.oldRecord - Old record
 * @param {string} scriptContext.type - Trigger type
 * @Since 2015.2
 */

然后您可以看到 context 参数没有 currentRecord 属性,而是有两个其他参数,您可以使用 newRecord oldRecord ,因此您的代码可以是这样的:

/**
 *@NApiVersion 2.x
 *@NScriptType UserEventScript
 *@param {Record} context.currentRecord
 */

define(['N/record'], 
function (msg) {
    // are you aware that you are "injecting" the 'N/record' library into the 'msg' variable ???
    function beforeSubmit(context) {
      try {

        var record = context.newRecord;               

        var createdDate = record.getValue({
            fieldId: 'createddate'
        });

        var dataNecessidade = record.getValue({
            fieldId: 'custbodyek_data_nece_requ_po'
            
        });

        console.log(createdDate ,dataNecessidade);        

        }
        catch(ex){
        log.error(ex);
        }
    }
    return {
        beforeSubmit : beforeSubmit,
    
    };
});
B. Assem
2020-12-26

你这样写试试,我一直都是用这种方法获取字段值的

const bfRecord= context.newRecord;
const createdDate = bfRecord.getValue('createddate');
charles
2020-12-26