function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
shekhar 46shekhar 46 

write a trigger on account

Whenever u are updating account object , find the number of contact which are present on account itself.show that count on account object
Best Answer chosen by shekhar 46
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Shekhar,

There are multiple issues in the code.

-> Usually the code should work on contact creation and deletion as well but you have written only update and as you will be updating Account field so it should be After  context.

-> you have written query before for loop and there the Accountid is not added to the accID set so the soql will return null.

->  Numberofcontact is integer variable and you are assiging it with Query which is wrong.

-> With all the above changes it works only in update of Contacts but wont work in insert or delete scenerios.

Thanks
 

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Shekhar,

Ideally the trigger should be on Contact object I guess. Because when a new contact is created or contact is deleted the count should be updated. So trigger on Account cannot wokrk in this scenerio. Can you confirm if Trigger on Contact is fine?

Thanks,
 
shekhar 46shekhar 46
will the trigger on conatct work for this scenario, as i am also trying to write trigger on account  
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Shekhar,

Thie can be trigger on Contact to count no of conntacts on Account.
 
trigger CountContactOnAccount on Contact (after INSERT, after UPDATE, after DELETE ) {
Set <Id> accountIds = new Set <Id>();
List <Account> lstAccountsToUpdate = new List <Account>();
 if(Trigger.isInsert){
    for(Contact con:trigger.new){
        accountIds.add(con.accountID);
    }
}
if(Trigger.isUpdate|| Trigger.isDelete){
    for(Contact con:trigger.old){
        accountIds.add(con.accountID);
    }
}

for(Account acc:[SELECT Id,Name,Count_Contact__c,(Select Id from Contacts) from Account where Id IN: accountIds]){
    Account accObj = new Account ();
    accObj.Id = acc.Id;
    accObj.Count_Contact__c = acc.Contacts.size();
    lstAccountsToUpdate.add(accObj);
}

UPDATE lstAccountsToUpdate;
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
shekhar 46shekhar 46
what is wrong in this trigger for above scenrio, it is not working
Trigger updatetrigger on contact(before update) {


Set<id> accID=new set<id>();
Numberofcontact= SELECT Count FROM  contact WHERE accountID IN=: accID];

For (contact c: trigger.new){
If(c.accountID!= “”){
accID.add(c.accountID);
             }
              a.count__C=numberofcontact;
}
Update c;
}
 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Shekhar,

There are multiple issues in the code.

-> Usually the code should work on contact creation and deletion as well but you have written only update and as you will be updating Account field so it should be After  context.

-> you have written query before for loop and there the Accountid is not added to the accID set so the soql will return null.

->  Numberofcontact is integer variable and you are assiging it with Query which is wrong.

-> With all the above changes it works only in update of Contacts but wont work in insert or delete scenerios.

Thanks
 
This was selected as the best answer