0
votes

I am trying to retrieve and modify the shipping address shipphone value when an item fulfillment is saved, but the changes do not save (and I have not figured out how to retrieve the shipphone so I was doing initial testing with the phone value). Is there another way that I should be doing this so that I can actually update the transaction-level address details? I am assuming that I need to load the customer record to get the address since I cannot directly get the phone on the transaction record, but that still doesn't explain how I can set shipphone for the shippingaddress on a transaction.

function beforeSubmit(context) {
    var ifNum = 'N/A';
    if (context.newRecord.id) ifNum = context.newRecord.id;

    var recordPhone = context.newRecord.getValue('phone');
    var phoneValid = checkPhone(recordPhone);

    if (!phoneValid) {
        try {
            var correctedPhone = correctPhone(recordPhone);
            var shipaddress = context.newRecord.getSubrecord('shippingaddress')
            shipaddress.setValue({fieldId: 'shipphone', value: correctedPhone});

            log.debug('Set phone to: ' + correctedPhone + ' from ' + recordPhone);
        }
        catch(e) {
            var errorStr = 'Error setting IF #' + ifNum + ' phone value';
            log.error(errorStr, e.message);
            throw error.create({
                name: errorStr,
                message: e.message,
                notifyOff: true
            });
        }
    }
}
1

1 Answers

4
votes

shipphone is a transaction body level field for transactions that have it (sales order, item fulfillment etc)

the address subrecord field that it is sourced from is addrphone so your code should likely be more like:

var recordPhone = context.newRecord.getValue({fieldId:'shipphone'});
...
  context.newRecord.setValue({fieldId:'shipphone', value:correctedPhone}); // for this transaction
  shipaddress.setValue({fieldId: 'addrphone', value: correctedPhone}); // corrected for future uses of this address.