How to implement findSublistLineWithValue function in SuiteScript?

I'm trying to apply a bill credit to a vendor bill right after its creation.

Below is the function to create the bill credit record, and it takes in a JSON object, and a vendor bill record ID that should be applied to.

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(lineWithVendorbill)

      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

}

Can someone help me with this findSublistLineWithValue function, as it seems to return -1, though i have tested it in the debugger and have been able to successfully find sublist values that i am looking for, with the associated vendor bill id. Is ther anything preventing me from executing this function properly ?



Read more here: https://stackoverflow.com/questions/64310047/how-to-implement-findsublistlinewithvalue-function-in-suitescript

Content Attribution

This content was originally published by Ken Jeong at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: