Pmac08
Pmac08

Reputation: 11

Looking for Scripting Help on accessing the billaddresslist

I have a script that "should"

  1. Get the customer ID from a created customer field on my form
  2. Load the customer record
  3. Remove options that are already in the 'billaddresslist'
  4. Search for addresses associated with my selected customer
  5. add each address as a select option in the 'billaddresslist' field.

I continue to get an error when reloading my form, "Cannot find function removeSelectOption in object Field".

I tried this script below, any suggestions or advice?

/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 */
define(['N/record', 'N/search'], function(record, search) {
  function beforeLoad(context) {
    var currentRecord = context.newRecord;

    // Get the customer ID from the "customer" field on the form
    var customerId = currentRecord.getValue({
      fieldId: 'my_custom_field'
    });

    if (customerId) {
      // Load the customer record
      var customerRecord = record.load({
        type: record.Type.CUSTOMER,
        id: customerId,
        isDynamic: true
      });

      // Remove any existing options in the "billaddresslist" field
      currentRecord.getField({
        fieldId: 'billaddresslist'
      }).removeSelectOption({
        value: null,
        filter: null,
        operator: null
      });

      // Search for all addresses associated with the customer
      var addressSearch = search.create({
        type: 'address',
        filters: [
          ['entity', 'is', customerId],
        ],
        columns: [
          'internalid',
          'addressbookaddress'
        ]
      });

      // Add each address as a select option in the "billaddresslist" field
      addressSearch.run().each(function(result) {
        currentRecord.getField({
          fieldId: 'billaddresslist'
        }).addSelectOption({
          value: result.getValue('internalid'),
          text: result.getValue('addressbookaddress')
        });

         return true;
      });
    }
  }

  return {
    beforeLoad: beforeLoad
  };
});

Upvotes: 0

Views: 101

Answers (1)

Krypton
Krypton

Reputation: 5231

The removeSelectOption method is only available on Client scripts - not User Event scripts. See the excerpt below from this SuiteAnswers page. Note that it's also only available on fields dynamically added to the user interface - either by a suitelet or a beforeLoad user event script, and that it's part of the N/currentRecord module, which you don't have loaded.

You could create a new field to take the place of the 'billaddresslist' field within your beforeLoad function and hide the existing native field. Then populate that custom field with the desired values. You would need a corresponding function on the beforeSubmit user event to update the value of the native 'billaddresslist' field before the record is saved for it to persist to the database.

Method Description Removes a select option from certain types of select and multiselect fields. This method is usable only in select fields that were added by a front-end Suitelet or beforeLoad user event script. The IDs for these fields always have a prefix of custpage.
Returns Void
Supported Script Types Client scripts.For more information, see SuiteScript 2.x Client Script Type.
Governance None
Module N/currentRecord Module
Since 2016.2

Upvotes: 0

Related Questions