4

I'm trying to set an address on a customer record. I've grasped that the address is a subrecord of a customer sublist and I believe I'm able to set fields on said subrecord, but I'm unable to get the changes to save. How can one set address information on a Customer using SuiteScript 2.0?

Current code:

customer.selectNewLine({
  sublistId: 'addressbook'
});

var addressSubrecord = customer.getCurrentSublistSubrecord({
  sublistId: 'addressbook',
  fieldId: 'addressbookaddress'
});

subrecordAddressDetail.setValue({
  fieldId: 'addr1',
  value: 'Test Street'
});

subrecordAddressDetail.setValue({
    fieldId: 'country',
    value: 'US'
});

customer.commitLine({
  sublistId: 'addressbook'
});

I've also tried adding customer.save() after .commitList, but I get the error Record has changed when I try to do so.

John Lucas
  • 588
  • 5
  • 20

2 Answers2

9

The part I was missing was that you need to save the parent record for any changes to a subrecord of a sublist to take effect. I was unable to save said parent record because I had made changes to that record and saved them previously which caused the Record has already changed error.

Generally addresses can be added to customer records by:

  1. Selecting a line in the addressbook sublist.
  2. Retrieving the address subrecord of the currently selected line.
  3. Updating the subrecord.
  4. Committing the selected line in the addressbook sublist.
  5. Saving the parent record.

For example this will update the first address for a given customer or create it if it doesn't already exist:

function updateAddress(customer, address) {
    var currentAddressCount = customer.getLineCount({
      'sublistId': 'addressbook'
    });

    if (currentAddressCount === 0){
      customer.selectNewLine({
         sublistId: 'addressbook'
       });
    } else {
      customer.selectLine({
        sublistId: 'addressbook',
        line: 0
      });     
    } 

    var addressSubrecord = customer.getCurrentSublistSubrecord({
      sublistId: 'addressbook',
      fieldId: 'addressbookaddress'
    });

    // Set all required values here.
    addressSubrecord.setValue({
        fieldId: 'addr1',
        value: address.line_one
    })

    customer.commitLine({
       sublistId: 'addressbook'
    });
    customer.save()
  }
John Lucas
  • 588
  • 5
  • 20
  • 2
    One annoying snag with this: Since those sublist APIs are only available in dynamic mode, you have to make sure you set the Country field first if you're going to change it. If you change the country field after setting the others while in dynamic mode, then you will lose all of your changes. Best to do a dry run in the UI to make sure you know the order the fields should be set. – Eidolon108 Jun 21 '16 at 19:40
4

I think you need to treat the address as a proper record. Therefore, after you retrieve it, set its values and then commit it separately. Something along these lines:

customer.selectNewLine({
    sublistId: 'addressbook'
});

var addressSubrecord = customer.getCurrentSublistSubrecord({
    sublistId: 'addressbook',
    fieldId: 'addressbookaddress'
});

addressSubrecord.setValue({
    fieldId: 'addr1',
    value: 'Test Street'
});

addressSubrecord.setValue({
    fieldId: 'country',
    value: 'US'
});

addressSubrecord.save()

customer.commitLine({ //probably not necessary since address is already updated
    sublistId: 'addressbook' 
});
TonyH
  • 1,117
  • 8
  • 18
  • Hi @TonyH - thanks for your help. It looks like subrecords do not have a `.save()` method in SuiteScript2.0. Calling save returns an error that the method is not defined. I was able to figure out my issue - I've posted an answer that outlines the problem. – John Lucas May 17 '16 at 15:37