想要从采购订单记录中的合同记录中搜索fileld值

时间:2016-05-20 12:29:27

标签: netsuite suitescript

我在购买记录中搜索合同记录中的值,但我无法获得值所在的字段。下面是代码。我将此代码应用于合同记录,该功能适用​​于提交之前。 我想我正在以错误的方式应用搜索。

 function srchfield() 
 {

        var recordid = nlapiGetRecordId() //retunrs the contract id
        nlapiLogExecution('DEBUG', 'recordid ', recordid );

        var recordtype = nlapiGetRecordType();     //retunrs the contract recordtype = jobs   
        nlapiLogExecution('DEBUG', 'RecordType', recordtype);

        var loadrecord = nlapiLoadRecord(recordtype, recordid); //loads the record
        nlapiLogExecution('DEBUG', 'Load Record', loadrecord );

        var contractname = nlapiGetFieldValue('entityid'); //returs the value of the field contractname whose fieldid is = entityid
        nlapiLogExecution('DEBUG', 'ContractName ', contractname );


   var filters = new Array();
      new nlobjSearchFilter('entityid', null, 'anyof', contractname ); // entityid is field id in contract Record and contractname is defined above for contract record

          // nlapiLogExecution('DEBUG', 'SearchFilter', filters );

  var columns = new Array();
  new nlobjSearchColumn('custbodycontract'); // custbodycontractis field id in PO Record


  var searchresults = nlapiSearchRecord('purchaseorder', null, filters, columns);


for ( var i = 0; searchresults != null && i < searchresults.length; i++ )
{
   var searchresult = searchresults[ i ];
   var record = searchresult.getId( );
   var rectype = searchresult.getRecordType( );
   var cntrct_name= searchresult.getValue( 'custbodycontract' );

}

 }

提前致谢

2 个答案:

答案 0 :(得分:0)

****更新:我已根据提供的评论完成了我的回答。请参阅最后一节。我希望它有所帮助。谢谢!***


**如果你能进一步解释,我可能会给你提供片段。请指教

如果我理解你的代码,这些就是你正在做的事情,你想做的事情:

  • 您的合约记录是本机实体记录 - 项目/工作。它刚刚由您的职能顾问重新命名为合同。
  • 在提交并部署到合同(项目/作业)记录之前,您有一个用户事件脚本(SuiteScript 1.0)。
  • 使用合同记录中的信息(在这种情况下使用合同名称(entityid)),您希望搜索PO以在标题处获得'custbodycontract'。
  • 然后您使用“entityid”过滤PO上的搜索,但PO在标题上没有该原生字段。它只在费用行和项目行上有,但id是'customer'。

    你能解释一下合同记录与PO有什么关系吗? PO上可用的哪个字段(本机/自定义)可用作合同记录中的连接?


    然后,请注意这个最佳做法作为免费赠品。
    1. 优化脚本。在beforesubmit上你不需要使用nlapiLoadRecord。请参阅下面的最佳做法:
      - 使用beforesubmit并且如果您只是在脚本部署记录上获取或设置值,请使用nlapiGet **和nlapiSet ** API。
      - 在提交之前,如果要获取并设置不同记录的子列表中的值,则仅使用nlapiLoadRecord。但是如果你只是在线级获得价值(不同的记录),那么首先使用nlapiSearchRecord。如果它不能满足您的需求,请使用nlapiLoadRecord。
      - 在aftersubmit上,如果您在已部署记录的行级别和不同记录中设置并获取值,则仅使用nlapiLoadRecord。如果您只是在级别上获得价值,那么与提交前相同的做法。
      - 在aftersubmit上,使用nlapiLookUp从头部获取值,如果设置则使用nlapiSubmitField。如果它不能满足您的需求,请仅使用nlapiLoadRecord。


      ****这可能是你需要的......

function srchfield()
{
    var stRecordid = nlapiGetRecordId(); /*retunrs the contract id*/
    nlapiLogExecution('DEBUG', 'recordid ', stRecordid);

    var stRecordtype = nlapiGetRecordType(); /*retunrs the contract recordtype = jobs*/
    nlapiLogExecution('DEBUG', 'RecordType', stRecordtype);

    var stContractname = nlapiGetFieldValue('entityid'); /*returs the value of the field contractname whose fieldid is = entityid*/
    nlapiLogExecution('DEBUG', 'ContractName ', stContractname);

    var arrFilters = new Array();
    arrFilters.push(new nlobjSearchFilter('type', null, 'anyof',
        [
            'PurchOrd'
        ])); /*As best practice, instead of directly searching on POs, add filter for transaction type since you might use this later in other transaction.*/
    arrFilters.push(new nlobjSearchFilter('mainline', null, 'is', 'T')); /*This is to exclude line level results*/
    arrFilters.push(new nlobjSearchFilter('custbodycontract', null, 'is', stContractname));

    var arrColumns = new Array();
    arrColumns.push(new nlobjSearchColumn('trandate')); /*I just wanted to include this column on the result. :)*/
    arrColumns.push(new nlobjSearchColumn('type')); /*I just wanted to include this column on the result. :)*/
    arrColumns.push(new nlobjSearchColumn('tranid')); /*I just wanted to include this column on the result. :)*/
    arrColumns.push(new nlobjSearchColumn('custbodycontract')); /*This is what you need.*/

    var arrSearchresults = nlapiSearchRecord('transaction', null, arrFilters, arrColumns);
for (var i = 0; arrSearchresults != null && i < arrSearchresults.length; i++)
{
    var objResult = arrSearchresults[i];
    var stRecId = objResult.getId();
    var stRecType = objResult.getRecordType();
    var stCntrctName = objResult.getValue('custbodycontract');
}

答案 1 :(得分:0)

我在代码中注意到的问题是过滤器的创建 您实例化了nlobjSearchFilter但是没有将它推入数组中。基本上,你正在搜索没有过滤器。
我相信您的搜索条件已经完成 请尝试以下代码。顺便说一下,确保custbodycontract的字段类型是自由格式文本,以便将其与合同记录的entityid进行正确比较。

function srchfield()
{
var stRecordid = nlapiGetRecordId(); /*retunrs the contract id*/
nlapiLogExecution('DEBUG', 'recordid ', stRecordid);

var stRecordtype = nlapiGetRecordType(); /*retunrs the contract recordtype = jobs*/
nlapiLogExecution('DEBUG', 'RecordType', stRecordtype);

var stContractname = nlapiGetFieldValue('entityid'); /*returs the value of the field contractname whose fieldid is = entityid*/
nlapiLogExecution('DEBUG', 'ContractName ', stContractname);

var arrFilters = new Array();
arrFilters.push(new nlobjSearchFilter('type', null, 'anyof',
    [
        'PurchOrd'
    ])); /*As best practice, instead of directly searching on POs, add filter for transaction type since you might use this later in other transaction.*/
arrFilters.push(new nlobjSearchFilter('mainline', null, 'is', 'T')); /*This is to exclude line level results*/
arrFilters.push(new nlobjSearchFilter('custbodycontract', null, 'is', stContractname));

var arrColumns = new Array();
arrColumns.push(new nlobjSearchColumn('trandate')); /*I just wanted to include this column on the result. :)*/
arrColumns.push(new nlobjSearchColumn('type')); /*I just wanted to include this column on the result. :)*/
arrColumns.push(new nlobjSearchColumn('tranid')); /*I just wanted to include this column on the result. :)*/
arrColumns.push(new nlobjSearchColumn('custbodycontract')); /*This is what you need.*/

var arrSearchresults = nlapiSearchRecord('transaction', null, arrFilters, arrColumns);
for (var i = 0; arrSearchresults != null && i < arrSearchresults.length; i++)
{
    var objResult = arrSearchresults[i];
    var stRecId = objResult.getId();
    var stRecType = objResult.getRecordType();
    var stCntrctName = objResult.getValue('custbodycontract');
}
}
相关问题