Set Billing Address on Invoice to Customer’s Default Billing Address

Sample script shows how to retrieve the default Billing Address on the Customer record and set the Billing Address fields on an Invoice to the retrieved address values

function beforeSubmit(scriptContext) {‌
    	var curRec = currentRecord.get()
    	var customerID = curRec.getValue({‌ fieldId: 'entity'});
    	if(customerID){‌
    		var customerRecord = record.load({‌ type: record.Type.CUSTOMER,
    					   id: customerId })
    		var numberOfAddress = customerRecord.getLineCount({‌ sublistId: 'addressbook'})
    		var addressId;

    		for(var x =1; x<= numberOfAddress; x++){‌
    			var defaultaddress = customerRecord.getSublistValue({‌ sublistId: 'addressbook',
    							      fieldId: 'defaultbilling',
    							      line: x });
    			if(defaultaddress == 'T'){‌
    				addressId = customerRecord.getSublistValue({‌ sublistId: 'addressbook',
    						     fieldId: 'internalid',
    						     line: x })
    				var addressText = customerRecord.getSublistValue({‌ sublistId: 'addressbook',
    							   fieldId: 'addrtext',
    							   line: x })
    				break;
    			}
    		}
    		if(addressID != '' || addressID != null){‌
    			curRec.setValue({‌ fieldId: 'billaddresslist', value: addressId });
    			curRec.setValue({‌ fieldId: 'billaddress', value: addressText })
    		}
	}
}

Leave a comment

Your email address will not be published. Required fields are marked *