0151 355 4555

Salesforce Bank Account Validation

As there are no out-of-the-box fields for storing bank account details you will need to do a few more configuration steps to trigger the bank account validation service and display the results.

Overview

The process for enabling bank account validation for your Salesforce records is:

  1. Create a new field to store the bank account validation results
  2. Create an Apex Trigger to run when you create or update records that will populate the bank account validation results
  3. Create a VisualForce page to display the bank account validation results

In addition to storing a simple valid/invalid result, the service can update other fields on your record to indicate the name of the bank and the name and address of the branch the account is with.

1. Create Bank Account Validation Result field

Use the standard Salesforce tools to create a new "Long Text Area" field on the same record type that you have bank account details on, e.g. Account or Contact. This field can be called anything you like, just take a note of the "API Name" of the field you create. For the examples below we will assume this is called "Bank_Val_Result__c".

2. Create Apex Trigger

Within the Customize > Record Type > Triggers menu, click New. Copy & paste the example code below.

trigger BankValUpdatedAccount on Account (after update) {
    for (Account a: Trigger.new) {
        d8.BankAccountValidation.validateBankAccount(
            a, 
            a.Sort_Code__c, 
            a.Account_Number__c, 
            'Bank_Val_Result__c',
            null, // street
            null, // city
            null, // state
            null, // zip
            null, // bank name
            null  // branch name
        );
    }

The example code above assumes that you have the following custom fields created on the Account record type:

  • Sort_Code__c
  • Account_Number__c
  • Bank_Val_Result__c

If you are using different field names, replace them in the code with the names that are appropriate for you. Note that the name of the field created earlier to store the bank account validation result is supplied as a string value in quotes.

Repeat the process to create an identical trigger but change after update to after create and give the trigger a different name such as BankValCreatedAccount.

These two triggers will now ensure that the Bank Val Result field is kept up to date when a record is first created and whenever the sort code or account number fields change.

3. Create Visualforce Page

The trigger will now be running to gather bank account validation details as you create or update records, so you now need a Visualforce page to display the results. Most of this is already encapsulated in a custom Visualforce component, you just need to connect it up to your specific fields.

In Develop > Visualforce Pages, click New. Give your page a useful label and name, then copy & paste the following code into the Visualforce Markup section:

<apex:page standardController="Account" showHeader="false">
    <apex:form>
        <script type="text/javascript">
            var pageUrl = '{!URLFOR('/apex/' + $CurrentPage.Name, null, ['id'=$CurrentPage.Parameters.id])}';
        </script>

        <table border="0" cellspacing="0" cellpadding="0" width="100%" style="font-size: 75%">
            <tbody>
                <tr>
                    <td width="36%"></td>
                    <td style="padding: 3px 10px 16px 3px">
                        <d8:Data8BankAccountValidation sortCode="{!Account.Sort_Code__c}" accountNumber="{!Account.Account_Number__c}" bankValResponse="{!Account.Bank_Val_Result__c}">
                        </d8:Data8BankAccountValidation>
                    </td>
                </tr>
            </tbody>
        </table>
    </apex:form>
</apex:page>

Again, if you are using different field names from our examples then update them in the code before clicking Save. Note that the field names used in this part of the configuration do not include the initial prefix (d8_ in our example).

Finally, add the Visualforce page to your page layout. We recommend setting the height of the Visualforce Page in your layout to be 20 pixels.

Advanced: Extracting Bank Name and Branch Name & Address

In the example above we have described how to get a simple valid/invalid indicator on your records. However, when the details are valid you may want to access other information about the bank account, including the name of the bank (e.g. "Halifax") and the name of the specific branch (e.g. "Chester") and even the branch address.

If you have fields to store this additional information you can populate it in one of two ways:

Automatic Population

In this method, whenever the bank account number or sort code is changed, the associated details will be populated in your record automatically. To do this, edit the two triggers you created in step 1 to include the field names for the appropriate data elements in place of the null values as indicated by the comments. For example, to store the bank name in a custom field named Bank_Name__c you would use:

        d8.BankAccountValidation.validateBankAccount(
            a, 
            a.Sort_Code__c, 
            a.Account_Number__c, 
            'Bank_Val_Result__c',
            null, // street
            null, // city
            null, // state
            null, // zip
            'Bank_Name__c', // bank name
            null  // branch name
        );

Again, note the quotes around the field name.

Manual Population

In this method, the user is given an option within the Visualforce page to update the record with the bank name & address details so they can choose to do it on a record-by-record basis.

To do this, include the fields to update in the parameters to the Data8BankAccountValidation component and add a commandLink or commandButton component to save the record:

<d8:Data8BankAccountValidation sortCode="{!Account.Sort_Code__c}" accountNumber="{!Account.Account_Number__c}" street="{!Account.BillingStreet}" city="{!Account.BillingCity}" state="{!Account.BillingState}" zip="{!Account.BillingZip}" bankName="{!Account.Bank_Name__c}" branchName="{!Account.Bank_Branch_Name__c}" bankValResponse="{!Account.Bank_Val_Result__c}">
    <apex:commandLink id="applyAddressButton" action="{!quickSave}" value="Use Bank Address" />
</d8:Data8BankAccountValidation>

You will then be able to click the "Use Bank Address" link within the Visualforce page to update the record with the bank name and address values.