简体   繁体   中英

How to set an address on customer using SuiteScript 2.0

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.

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

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' 
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM