首页 > 解决方案 > 如何在 SuiteScript 中实现 findSublistLineWithValue 函数?

问题描述

我正在尝试在创建后立即将账单信用应用于供应商账单。

下面是创建账单信用记录的函数,它接受一个 JSON 对象和一个应应用到的供应商账单记录 ID。

function createBillCreditRecord(cr, vendor_bill_record_id) {
  var commission_response = cr;

  log.debug('vendor_bill_record_id cbc',vendor_bill_record_id)
  log.debug('commission_response cbc',commission_response)


  var bill_credit_record_obj = record.create({
    type: record.Type.VENDOR_CREDIT,
    isDynamic: true,
  });

  bill_credit_record_obj.setValue({fieldId:'custbody_callidus_deals_id_main',value: parseInt(commission_response.deal_id)})
  bill_credit_record_obj.setValue({fieldId:'memo',value:commission_response.deal_address})
  bill_credit_record_obj.setValue({fieldId:'tranid',value:commission_response.deal_address + '_' + commission_response.deal_id})
  bill_credit_record_obj.setValue({fieldId:'entity',value: parseInt(commission_response.vendor_bills[0].entity_id)})
  bill_credit_record_obj.setValue({fieldId:'location',value: parseInt(commission_response.vendor_bills[0].location)})
  bill_credit_record_obj.setValue({fieldId:'department',value: parseInt(commission_response.vendor_bills[0].department)})
  //Line of business has items with the same text but different internal id, we need to do mapping
  bill_credit_record_obj.setValue({fieldId:'class',value: parseInt(commission_response.vendor_bills[0].line_of_business)})


  //trandate field takes in a Date() object as a parameter. bill_credit_data also has funky characters & need to string manipulate
   var date_array = commission_response.vendor_bills[0].date.split('-')
   var date_obj = new Date(date_array[1] + '/' + date_array[2][0] + date_array[2][1] + '/' + date_array[0])
   bill_credit_record_obj.setValue({fieldId:'trandate',value:date_obj})
  //Go through 'invoices' array from commission response to set field values iteratively

  commission_response.vendor_bills[0].lines.forEach(function (bill_credit_data) {

    //For instances of multiple lines in the invoice we need to iteratively set the sublist values, here we handle only the negative amounts
          if (bill_credit_data.amount < 0) {
            bill_credit_record_obj.selectNewLine({sublistId:'item'})
            bill_credit_record_obj.setCurrentSublistValue({sublistId:'item',fieldId:'item',value:bill_credit_data.item_id})
            bill_credit_record_obj.setCurrentSublistValue({sublistId:'item',fieldId:'amount',value:bill_credit_data.amount * -1})
            bill_credit_record_obj.setCurrentSublistValue({sublistId:'item',fieldId:'custcol3',value:bill_credit_data.listing_type_id})
            bill_credit_record_obj.commitLine({sublistId:'item'});
          }
    }
  )

    var bill_credit_record_id;
    try {
      bill_credit_record_id = bill_credit_record_obj.save()
      log.debug('bill_credit_record_id: ' + bill_credit_record_id)
    } catch (e) {
      log.debug(e.message + ': bc not created !');
    }
    

     var lineWithVendorBill = bill_credit_record_obj.findSublistLineWithValue({
        sublistId: 'apply',
        fieldId: 'internalid',
        value: vendor_bill_record_id
     });

log.debug(lineWithVendorbil)

      var totalToPay = bill_credit_record_obj.getSublistValue({
         sublistId: 'apply',
         fieldId: 'total',
         line: 0
      });
      
      bill_credit_record_obj.selectLine({
          sublistId: 'apply',
          line: lineWithVendorBill
      });
      bill_credit_record_obj.setCurrentSublistValue({
          sublistId: 'apply',
          fieldId: 'apply',
          value: true
      });
      
      bill_credit_record_obj.setCurrentSublistValue({
          sublistId: 'apply',
          fieldId: 'amount',
          value: totalToPay
      });
      bill_credit_record_obj.commitLine({
        sublistId: 'apply'
      });

    return bill_credit_record_id

}

有人可以帮我解决这个 findSublistLineWithValue 函数,因为它似乎返回 -1,尽管我已经在调试器中对其进行了测试,并且能够成功找到我正在寻找的子列表值,以及相关的供应商账单 ID。有什么东西阻止我正确执行这个功能吗?

标签: suitescript2.0

解决方案


如果您从 internalid 获得 -1,请尝试检查 doc 字段:

    var lineWithVendorBill = bill_credit_record_obj.findSublistLineWithValue({
        sublistId: 'apply',
        fieldId: 'doc',
        value: vendor_bill_record_id
    });

推荐阅读