1

We have created a new field on the proposal that sources from our customer list. This will be the general contractor that we will bill. The original customer field will be the end user that the product is installing. The end game would be, when we have an entry in the new customer field, the billaddresslist will show the addresses associated with that new customer. I've created a fieldchanged script that uses that new field and references the customer entered and pulls the addresses associated with that entity.

This is not working, I get an error that it cannot read properties of undefined. I believe I'm referencing the customer incorrectly, I trimmed the name so it only pull the CU id number but that may be the wrong way to do this. I'm new to scripting so any advice or tips would be great :).

I expected this to work but I only get errors.

/**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */
define(['N/record', 'N/log'], function(record, log) {
  var cache = {};

  function fieldChanged(context) {
    if (!context.form) {
      return;
    }

    var customerId = context.currentRecord.getValue({
      fieldId: 'my_custom_field'
    });

    // Check if the customer ID is valid
    if (!customerId) {
      log.error('Invalid customer ID', customerId);
      return;
    }

    var customerIdArray = customerId.split(" ");
    var numericalCustomerId = customerIdArray[0].trim();

    // Check if the customer record is already in cache
    var customerRecord = cache[numericalCustomerId];
    if (!customerRecord) {
      // Load the customer record
      customerRecord = record.load({
        type: record.Type.CUSTOMER,
        id: numericalCustomerId,
        isDynamic: true
      });

      // Store the customer record in cache
      cache[numericalCustomerId] = customerRecord;
    }

    var addressCount = customerRecord.getLineCount({
      sublistId: 'addressbook'
    });

    var billaddresslist = context.form.getField({
      id: 'billaddresslist'
    });
    billaddresslist.removeAllOptions();

    for (var i = 0; i < addressCount; i++) {
      var address = customerRecord.getSublistValue({
        sublistId: 'addressbook',
        fieldId: 'addressbookaddress',
        line: i
      });

      var option = billaddresslist.addOption({
        text: address,
        value: i
      });
    }
  }

  return {
    fieldChanged: fieldChanged,
    pageInit: function(context) {
      context.form.addFieldChangeListener({
        fieldId: 'my_custom_field',
    after: fieldChanged
  });
}

}; });`

Pmac08
  • 11
  • 1

1 Answers1

0

fieldChanged and pageInit context doesn't have a form property. I think you're looking for a beforeLoad on a UserEvent script.

This would explain your cannot read properties of undefined in your pageInit entry point.

replace the if block checking for form in your fieldChanged method to if (context.fieldId !== "my_custom_field") return;

  • Am I referencing the customer ID correctly? Cutting off the actual customer name and just leaving the CU number? – Pmac08 Feb 10 '23 at 01:07
  • if the field is a select type that sources from the customer record then `currentRecord.getValue` should give you the internal ID and not the customer number. – Steffen Andersland Feb 10 '23 at 05:34