Input & Output
Validate a phone number
The validatePhone
method is designed to validate and standardize phone numbers. It checks the provided phone number against external validation sources and returns validation advice along with a formatted and categorized phone number. This method works best when paired with the saveValidationResult
method to store validation outcomes in Salesforce.
Method Signature
Method Name: validatePhone
global recordval.PhoneValidationOutput_v1 validatePhone(recordval.PhoneValidationInput_v1 phoneInput)
Parameters
Type | Variable | Description |
---|---|---|
PhoneValidationInput_v1 | phoneInput | The structured input object containing phone number validation data. |
Output
Return type: PhoneValidationOutput_v1
Save Validation Result
The saveValidationResult
method allows to save the outcome of an address validation (status, status message, advice) directly to a Salesforce record, ensuring the results are easily accessible and tied to the appropriate record. For implementation details, see the saveValidationResult Class Reference
Apex Example
// Initialize the Record Validation API
recordval.RecordValidationAPI_v1 api = new recordval.RecordValidationAPI_v1();
System.debug('Record Validation API initialized.');
Id leadId = '00QAa00000JinjeMAB'; // Replace with a valid Lead ID
System.debug('Lead ID: ' + leadId);
// Query the Lead record to retrieve the phone number
Lead leadRecord = [SELECT Id, Phone FROM Lead WHERE Id = :leadId LIMIT 1];
if (leadRecord == null || String.isEmpty(leadRecord.Phone)) {
System.debug('No phone number found for Lead ID: ' + leadId);
return;
}
System.debug('Phone number retrieved from Lead: ' + leadRecord.Phone);
// Create the phone validation input
recordval.PhoneValidationInput_v1 input = new recordval.PhoneValidationInput_v1();
input.phoneNumber = leadRecord.Phone; // Use phone number from Lead record
input.country = 'NL'; // Provide country code
input.format = recordval.PhoneFormat_v1.INTERNATIONAL; // Use the correct PhoneFormat_v1 reference
input.note = 'Testing phone validation for a Lead';
System.debug('Phone Validation Input prepared: ' + input);
// Execute phone validation
recordval.PhoneValidationOutput_v1 output;
try {
output = api.validatePhone(input);
System.debug('Phone validation executed successfully.');
} catch (Exception e) {
System.debug('Phone validation failed: ' + e.getMessage());
return;
}
// Log validation results
System.debug('Validation Advice: ' + output.getAdvice());
System.debug('Validated Phone Number: ' + output.phoneNumber);
System.debug('Country Code: ' + output.countryCode);
System.debug('Phone Type: ' + output.getPhoneType());
// Log the detailed status information
if (output.status != null) {
System.debug('Status Details:');
System.debug('Status Code: ' + output.status.code);
System.debug('Status Message: ' + output.status.message);
System.debug('Credit Used: ' + output.status.credit);
} else {
System.debug('No status information was returned.');
}
// Store the validation result on the Lead
recordval.rv2SaveValidationResultInput_v1 saveRequest = new recordval.rv2SaveValidationResultInput_v1();
saveRequest.setRecordId(leadId);
saveRequest.setValidationResult('Phone', output);
recordval.rv2SaveValidationResultOutput_v1 saveResult;
try {
saveResult = api.saveValidationResult(saveRequest);
if (saveResult.success) {
System.debug('Phone validation result saved successfully for Lead ID: ' + saveResult.recordId);
} else {
System.debug('Failed to save phone validation result.');
}
} catch (Exception e) {
System.debug('Error saving phone validation result: ' + e.getMessage());
}
// Update the Lead with the validated phone number if available
if (output.phoneNumber != null && output.phoneNumber != leadRecord.Phone) {
try {
leadRecord.Phone = output.phoneNumber;
update leadRecord;
System.debug('Lead phone number updated to: ' + leadRecord.Phone);
} catch (Exception e) {
System.debug('Error updating Lead phone number: ' + e.getMessage());
}
} else {
System.debug('No update needed: Validated phone number matches the existing one.');
}
Updated 17 days ago