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);
}
}

Hi Mohd, I have gone through your problem please refer bellow code:-
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.Thanks and Regards,Deepali Kulshresthawww.kdeepali.comApex 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);
}
}