+ Start a Discussion
Mohd NabeelMohd Nabeel 

UpdateContact: execution of AfterInsert caused by: System.SObjectException: Invalid field Account Id for AggregateResult Class.AccountContact.updateCheckBox: line 22, column 1 Trigger.UpdateContact: line 3, column 1

Hi,
I am trying to create an account and when the account is created a contact will be created and the checkbox should be true..

i have created an account but the checkbox remainis false and when i am again trying to create a new contact i am getting this error... UpdateContact: execution of AfterInsert caused by: System.SObjectException: Invalid field Account Id for AggregateResult Class.AccountContact.updateCheckBox: line 22, column 1 Trigger.UpdateContact: line 3, column 1

public class AccountContact{
    public static void onBefore(List<Account> acctList){
        List<Contact> conList = new List<Contact>();
        for(Account acc: acctList){
            Contact con = new Contact(AccountId = acc.ID);
            con.FirstName = 'Info';
            con.LastName = 'Default';
            con.Email = 'info@websitedomain.tld';
            acc.OnlyDefaultContact__c = TRUE;
            conList.add(con);
        }
        insert conList;
    }
    public static void updateCheckBox(List<Contact> contList){
        Set<id> accountIds = new Set<id>();
        for(Contact con: contList){
            accountIds.add(con.Accountid);
        }
        List<Account> updateAccount = new List<Account>();
        for(AggregateResult ar: [Select count(id), AccountId from Contact where AccountId IN
                                 : accountids group by AccountId having count(id)>1]){
			updateAccount.add(new Account(id = (id)ar.get('Account Id'),OnlyDefaultContact__c
                                = FALSE));
        }
    }
}

//trigger

trigger InsertContact on Account (after insert) {
    if(Trigger.isBefore && Trigger.isInsert){
    	AccountContact.onBefore(Trigger.new);
    }
}


trigger UpdateContact on Contact (after insert) {
	if(Trigger.isInsert && Trigger.isAfter){
		AccountContact.updateCheckBox(Trigger.new);
    }
}
Best Answer chosen by Mohd Nabeel
Deepali KulshresthaDeepali Kulshrestha

Hi Mohd,

I have gone through your problem please refer bellow code:-  
Apex Class:-


public class AccountContact {
    public static void onBefore(List<Account> acctList){
        List<Contact> conList = new List<Contact>();
        for(Account acc: acctList){
            Contact con = new Contact(AccountId = acc.ID);
            con.FirstName = 'Info';
            con.LastName = 'Default';
            con.Email = 'info@websitedomain.tld';
            
            conList.add(con);
        }
        insert conList;
          List<Account> lacc=[select Id,name from Account where id in :acctList ];
        for(Account a:lacc){
            a.OnlyDefaultContact__c = TRUE;

        }
        update lacc;
    }
    public static void updateCheckBox(List<Contact> contList){
        Set<id> accountIds = new Set<id>();
        for(Contact con: contList){
            accountIds.add(con.Accountid);
        }
        List<Account> updateAccount = new List<Account>();
        for(AggregateResult ar: [Select count(id), AccountId from Contact where AccountId IN
                                 : accountids group by AccountId having count(id)>1]){
                                     updateAccount.add(new Account(id = (id)ar.get('Account Id'),OnlyDefaultContact__c
                                                                   = FALSE));
                                 }
      
    }
}

Trigger

trigger InsertContact on Account (After insert) {
     if(Trigger.isAfter && Trigger.isInsert){
        AccountContact.onBefore(Trigger.new);
    }

}



trigger UpdateContact on Contact (after insert) {
    if(Trigger.isInsert && Trigger.isAfter){
        AccountContact.updateCheckBox(Trigger.new);
    }

}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com

All Answers

Ajay K DubediAjay K Dubedi
Hi Nabeel,

Please refer below code it works correctly:
 
public class AccountContact{
    public static void onBefore(List<Account> acctList){
        List<Contact> conList = new List<Contact>();
        for(Account acc: acctList){
            Contact con = new Contact(AccountId = acc.ID);
            con.FirstName = 'Info';
            con.LastName = 'Default';
            con.Email = 'info@websitedomain.tld';
            conList.add(con);
        }
        insert conList;
        List<Account> Account_List=[select Id,name from Account where id in :acctList];
        for(Account a:Account_List)
        {
            a.OnlyDefaultContact__c = TRUE;
        }
        update Account_List;
    }
    public static void updateCheckBox(List<Contact> contList){
        Set<id> accountIds = new Set<id>();
        for(Contact con: contList){
            accountIds.add(con.Accountid);
        }
        List<Account> updateAccount = new List<Account>();
        for(AggregateResult ar: [Select count(id), AccountId from Contact where AccountId IN
                                 : accountids group by AccountId having count(id)>1]){
            updateAccount.add(new Account(id = (id)ar.get('Account Id'),OnlyDefaultContact__c
                                = FALSE));
        }
    }
}

//trigger

trigger InsertContact on Account (after insert) {
    if(Trigger.isBefore && Trigger.isInsert){
        AccountContact.onBefore(Trigger.new);
    }
}


trigger UpdateContact on Contact (after insert) {
    if(Trigger.isInsert && Trigger.isAfter){
        AccountContact.updateCheckBox(Trigger.new);
    }
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
Mohd NabeelMohd Nabeel
Thanku Ajay it is working fine now.. Can you please tell me why did you this? 
Mohd NabeelMohd Nabeel
But when i am creating new contact under the same or different account it is giving me this error UpdateContact: execution of AfterInsert caused by: System.SObjectException: Invalid field Account Id for AggregateResult Class.AccountContact.updateCheckBox: line 27, column 1 Trigger.UpdateContact: line 3, column 1
Actually i am new to salesforce thats why little bit confused in these things..
Your help will be highly appreciable..
Ajay K DubediAjay K Dubedi
Hi Nabeel,
 
* First please update this trigger 
 with 'Trigger.isAfter'


trigger InsertContact on Account (after insert) {
    if(Trigger.isAfter && Trigger.isInsert){
        AccountContact.onBefore(Trigger.new);
    }
}

* And Update this method in apex 
 class "AccountContact"

public static void updateCheckBox(List<Contact> contList){
        Set<id> accountIds = new Set<id>();
        for(Contact con: contList)
        {
            accountIds.add(con.Accountid);
        }
        List<Contact> Contact_List = [select id,accountid from contact where accountid=:accountIds];
        set<Account> new_set = new set<Account>();
        for(Contact con:Contact_List)
        {
            if(accountIds.contains(con.AccountId))
            {
                accountIds.remove(con.AccountId);
            }
        }
        List<Account> updateAccount = new List<Account>();
        for(AggregateResult ar: [Select count(id), AccountId from Contact where AccountId IN
                                 : accountids group by AccountId having count(id)>1])
        {
            updateAccount.add(new Account(id = (id)ar.get('Account Id'),OnlyDefaultContact__c= FALSE));
        }
    }

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
Deepali KulshresthaDeepali Kulshrestha

Hi Mohd,

I have gone through your problem please refer bellow code:-  
Apex Class:-


public class AccountContact {
    public static void onBefore(List<Account> acctList){
        List<Contact> conList = new List<Contact>();
        for(Account acc: acctList){
            Contact con = new Contact(AccountId = acc.ID);
            con.FirstName = 'Info';
            con.LastName = 'Default';
            con.Email = 'info@websitedomain.tld';
            
            conList.add(con);
        }
        insert conList;
          List<Account> lacc=[select Id,name from Account where id in :acctList ];
        for(Account a:lacc){
            a.OnlyDefaultContact__c = TRUE;

        }
        update lacc;
    }
    public static void updateCheckBox(List<Contact> contList){
        Set<id> accountIds = new Set<id>();
        for(Contact con: contList){
            accountIds.add(con.Accountid);
        }
        List<Account> updateAccount = new List<Account>();
        for(AggregateResult ar: [Select count(id), AccountId from Contact where AccountId IN
                                 : accountids group by AccountId having count(id)>1]){
                                     updateAccount.add(new Account(id = (id)ar.get('Account Id'),OnlyDefaultContact__c
                                                                   = FALSE));
                                 }
      
    }
}

Trigger

trigger InsertContact on Account (After insert) {
     if(Trigger.isAfter && Trigger.isInsert){
        AccountContact.onBefore(Trigger.new);
    }

}



trigger UpdateContact on Contact (after insert) {
    if(Trigger.isInsert && Trigger.isAfter){
        AccountContact.updateCheckBox(Trigger.new);
    }

}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
This was selected as the best answer