You need to sign in to do that
Don't have an account?
pchowdary
Apex class to update a field with count
I need to update a field on a object with count of a field from another object (parent object) by writing a apex class. I'm stuck on this. Can anyone just guide me on this.
trigger updateCountOnAccount on Contact(after insert, after update, after delete, after undelete){
Set<Id> accIds = new Set<Id>();
if(trigger.isDelete){
for(Contact con : trigger.old){
if(con.AccountId != null){
accIds.add(con.AccountId);
}
}
}
else{
for(Contact con : trigger.new){
if(trigger.isInsert || trigger.isUndelete){
if(con.AccountId != null){
accIds.add(con.AccountId);
}
}
else{
Contact oldCon = trigger.oldMap.get(con.id);
if(oldCon.AccountId != con.AccountId){
if(oldCon.AccountId != null)
accIds.add(oldCon.AccountId);
if(con.AccountId != null)
accIds.add(con.AccountId);
}
}
}
}
Map<Id,Account> accMap = new Map<Id,Account>([Select count__c from Account where id in :accIds]);
if(trigger.isDelete){
for(Contact con : trigger.old){
if(accMap.containsKey(con.AccountId)){
accMap.get(con.AccountId).count__c = accMap.get(con.AccountId).count__c - 1;
}
}
}
else{
for(Contact con : trigger.new){
if(trigger.isInsert || trigger.IsUndelete){
if(accMap.containsKey(con.AccountId)){
if(accMap.get(con.AccountId).count__c == null){
accMap.get(con.AccountId).count__c = 1;
}
else{
accMap.get(con.AccountId).count__c = accMap.get(con.AccountId).count__c + 1;
}
}
}
if(trigger.isUpdate){
Contact oldCon = trigger.oldMap.get(con.id);
if(oldCon.AccountId != con.AccountId){
if(oldCon.AccountId != null){
accMap.get(oldCon.AccountId).count__c = accMap.get(oldCon.AccountId).count__c - 1;
}
if(con.AccountId != null){
if(accMap.get(con.AccountId).count__c == null){
accMap.get(con.AccountId).count__c = 1;
}
else{
accMap.get(con.AccountId).count__c = accMap.get(con.AccountId).count__c + 1;
}
}
}
}
}
}
update accMap.values();
}
Please make sure to replace following variables :
1. Account: Your Parent Object
2. Contact: Your Child Object
3. Count__c: Field on Account where you want the no. of child records.
4. AccountId: API name of Parent Lookup field on Child object
All Answers
Can you share you code? It would be lost easier to help you that way
trigger updateCountOnAccount on Contact(after insert, after update, after delete, after undelete){
Set<Id> accIds = new Set<Id>();
if(trigger.isDelete){
for(Contact con : trigger.old){
if(con.AccountId != null){
accIds.add(con.AccountId);
}
}
}
else{
for(Contact con : trigger.new){
if(trigger.isInsert || trigger.isUndelete){
if(con.AccountId != null){
accIds.add(con.AccountId);
}
}
else{
Contact oldCon = trigger.oldMap.get(con.id);
if(oldCon.AccountId != con.AccountId){
if(oldCon.AccountId != null)
accIds.add(oldCon.AccountId);
if(con.AccountId != null)
accIds.add(con.AccountId);
}
}
}
}
Map<Id,Account> accMap = new Map<Id,Account>([Select count__c from Account where id in :accIds]);
if(trigger.isDelete){
for(Contact con : trigger.old){
if(accMap.containsKey(con.AccountId)){
accMap.get(con.AccountId).count__c = accMap.get(con.AccountId).count__c - 1;
}
}
}
else{
for(Contact con : trigger.new){
if(trigger.isInsert || trigger.IsUndelete){
if(accMap.containsKey(con.AccountId)){
if(accMap.get(con.AccountId).count__c == null){
accMap.get(con.AccountId).count__c = 1;
}
else{
accMap.get(con.AccountId).count__c = accMap.get(con.AccountId).count__c + 1;
}
}
}
if(trigger.isUpdate){
Contact oldCon = trigger.oldMap.get(con.id);
if(oldCon.AccountId != con.AccountId){
if(oldCon.AccountId != null){
accMap.get(oldCon.AccountId).count__c = accMap.get(oldCon.AccountId).count__c - 1;
}
if(con.AccountId != null){
if(accMap.get(con.AccountId).count__c == null){
accMap.get(con.AccountId).count__c = 1;
}
else{
accMap.get(con.AccountId).count__c = accMap.get(con.AccountId).count__c + 1;
}
}
}
}
}
}
update accMap.values();
}
Please make sure to replace following variables :
1. Account: Your Parent Object
2. Contact: Your Child Object
3. Count__c: Field on Account where you want the no. of child records.
4. AccountId: API name of Parent Lookup field on Child object