You need to sign in to do that
Don't have an account?
shekhar 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
function readOnly(count){ }
You need to sign in to do that
Don't have an account?
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
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,
Thie can be trigger on Contact to count no of conntacts on Account.
Let me know if you face any issues.
If this solution helps, Please mark it as best answer.
Thanks,
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;
}
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