我正在尝试在客户记录上设置地址。我已经掌握了地址是客户子列表的子记录,我相信我能够在所述子记录上设置字段,但我无法得到保存的更改。如何使用SuiteScript 2.0设置客户的地址信息?
当前代码:
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'
});
我也尝试在customer.save()
之后添加.commitList
,但在我尝试时会收到错误Record has changed
。
答案 0 :(得分:3)
我缺少的部分是你需要保存父记录,以便对子列表的子记录进行任何更改才能生效。我无法保存所述父记录,因为我已对该记录进行了更改并保存了以前导致Record has already changed error
的记录。
通常,地址可以通过以下方式添加到客户记录中:
addressbook
子列表中选择一行。addressbook
子列表中提交所选行。例如,这将更新给定客户的第一个地址,或者如果它尚不存在则创建它:
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()
}
答案 1 :(得分:1)
我认为您需要将地址视为正确的记录。因此,在检索它之后,设置其值,然后单独提交它。这些方面的东西:
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'
});