+ Start a Discussion
umair ayazumair ayaz 

How to check if account exists

I am new to salesforce and still under training so i need some help to check if account exists or not. Here is the code that i have written so far.

Following is my visualforce page
<apex:page controller="AddAccountController" tabStyle="Account" standardStylesheets="false" showHeader="false" sidebar="false">
        .myFormStyle {
        background-color: #D3D3D3;
        border:3px outset black;
        border-top:2px dotted black;
        border-left:2px dotted black;
        function myFunction() {
        var x = document.getElementById("fname");
        x.value = x.value.toUpperCase();
    <apex:form styleClass="myFormStyle">
            <apex:pageBlock title="Add Account">
                <apex:pageBlockButtons location="top" >                   
                    <apex:commandButton value="Save" action="{!save}" reRender="accountList"  />
                    <apex:commandButton value="Cancel" action="{!cancel}" />
                <apex:pageBlockSection title="Account Details" columns="1">            
                    <apex:inputField id="accountName" value="{!act.name}"/> 
                    <apex:inputField value="{!act.site}"/> 
                    <apex:inputField value="{!act.type}"/> 
                    <apex:inputField value="{!act.accountNumber}"/>              
            <apex:pageBlockTable value="{!Account}" var="acc" id="accountList" styleClass="tableBorder">              
                <apex:column value="{!acc.Name}" styleClass="innerTableBorder" />            
                <apex:column value="{!acc.Site}" styleClass="innerTableBorder" />               
                <apex:column value="{!acc.type}" styleClass="innerTableBorder" />               

Here on its my controller
public class AddAccountController {
    public List <Account> account=new List <Account>();
    public Account act{get;set;}
    public AddAccountController() {       
        act = new Account();
    public List <Account> getAccount() {
        account = [SELECT Id, Name, Site, Type FROM Account order by createddate desc limit 20];
        return account;
    public PageReference cancel() {
        return null;
    public PageReference save() {
        system.debug('act name:' + act.name + ' = ' + act.accountNumber);
        upsert act;
        return null;
    public Account checkAccount(){
        String accountName =System.currentPageReference().getParameters().get('accountName');
        account = [SELECT Id, Name, Site, Type FROM Account WHERE Name = accountName];
        return act;

I am trying to get an icon next to account name field if it exists. Right now my plan is to check if the account exists in the Account array list. if it does then i would like to show a cross next to account field and if it does not exist then a tick next to the account field. Help would be appreciated.
Hi Umair 
I write a code which will help you to find you existing account here I filter the account with their name and you can add filter whatever you want
public class checkexistingAccount {
  public List<Account> acc;
   public List<Account> accountWithOutRepeat; // List which have without repeat account record 
   public List<Account> accountWithRepeat ; // list which repeats your record in account 
    public checkexistingAccount()
        acc=[select id, name from account];
        accountWithOutRepeat = new List<Account>();
       accountWithRepeat = new List<Account>();
    Public void existingAccountList()
        for(account ac:acc)
            for(account act:accountWithOutRepeat)
                if(String.valueOf(ac.Name).equals(act.Name) ) // here you can add filter what ever you want for repetition 

If this was helpful don't forget to mark as best answer if you have any Query Let me know 

Thank you 
Avaneesh Singh