Input & Output

Validate an email address

The validateEmail method is designed to validate and verify email addresses. It checks the provided email against trusted external data sources and returns validation insights, such as whether the email is valid, disposable, a spam trap, or undeliverable. This method works best when paired with the saveValidationResult method to store validation outcomes in Salesforce.

Method Signature

Method name: validateEmail

global recordval.EmailValidationOutput_v1 validateEmail(recordval.EmailValidationInput_v1 emailInput)

Parameters

TypeVariableDescription
EmailValidationInput_v1emailInputThe structured input object containing email fields for validation.

Output

Return Type: EmailValidationOutput_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

This Apex code retrieves a Lead’s email, validates it using validateEmail, logs the results, and saves the validation outcome in Salesforce using saveValidationResult. It ensures that email data is accurate and actionable.

// Ask for a Lead ID
String leadId = '00QAa00000JinjeMAB'; // Replace with actual Lead ID
System.debug('Validating email for Lead ID: ' + leadId);

// Query the Lead's Email
Lead leadRecord = [SELECT Id, Email FROM Lead WHERE Id = :leadId LIMIT 1];
if (leadRecord == null || String.isBlank(leadRecord.Email)) {
    System.debug('No valid email found for the specified Lead.');
    return;
}
System.debug('Lead Email: ' + leadRecord.Email);

// Initialize the Record Validation API
recordval.RecordValidationAPI_v1 api = new recordval.RecordValidationAPI_v1();
System.debug('Record Validation API initialized.');

// Prepare the email validation input
recordval.EmailValidationInput_v1 input = new recordval.EmailValidationInput_v1();
input.emailAddress = leadRecord.Email; // Use queried email
input.note = 'Validating email for Lead ID: ' + leadId;
System.debug('Email Validation Input prepared:');
System.debug('Email Address: ' + input.emailAddress);
System.debug('Note: ' + input.note);

// Execute the email validation
recordval.EmailValidationOutput_v1 output;
try {
    output = api.validateEmail(input);
    System.debug('Email validation executed successfully.');
} catch (Exception e) {
    System.debug('Email validation failed: ' + e.getMessage());
    return;
}

// Log the validation result
System.debug('Validation Advice: ' + output.getAdvice());

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

// Log the validated email details
System.debug('Complete Email: ' + output.complete);
System.debug('Addressee: ' + output.addressee);
System.debug('Domain: ' + output.domain);
System.debug('Free Email Provider: ' + output.free);
System.debug('Disposable Email: ' + output.disposable);

// Prepare to save the validation result
if (output != null) {
    recordval.rv2SaveValidationResultInput_v1 saveRequest = new recordval.rv2SaveValidationResultInput_v1();
    saveRequest.setRecordId(leadId);
    saveRequest.setValidationResult('Email', output); // Saving validation result for the Email field

    // Save the validation result
    recordval.rv2SaveValidationResultOutput_v1 saveResult;
    try {
        saveResult = api.saveValidationResult(saveRequest);
        if (saveResult.success) {
            System.debug('Validation result saved successfully for record ID: ' + saveResult.recordId);
        } else {
            System.debug('Validation result save failed.');
        }
    } catch (Exception e) {
        System.debug('Error saving validation result: ' + e.getMessage());
    }
} else {
    System.debug('No validation result to save.');
}