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
Sana123Sana123 

I have a custom object about_contact__c in my account and and i have to apply trigger on contct to fetch contact details to that field of account

when a contact is inserted or updated then my trigger run on contact and fetch the contact name to that related account..
Like i add two contacts to account 'abc' then in the field of account naemed as 'about_contact__c' details of the contact attached to it.for example my contacts are xyz and kkr then thes two contact details add to my account abc field about_contact__c
Best Answer chosen by Sana123
CharuDuttCharuDutt
Hii Sana 
Try Below Code
trigger NumberOfChild on Contact (After Insert,After Update,After Delete) {
    
    If(trigger.isAfter &&(Trigger.isInsert || Trigger.isUpdate)){
        NumberOfChildTriggerHandler.InsertUpdateDeleteMethod(trigger.new);
    }
     If(trigger.isAfter &&Trigger.isDelete){
        NumberOfChildTriggerHandler.InsertUpdateDeleteMethod(trigger.old);
    }
}




public class NumberOfChildTriggerHandler {
 
    public static Void InsertUpdateDeleteMethod(list<Contact> lstCon){
        List<Account> accList=new List<Account>();

    Set<Id> setAccIds = new Set<Id>();
         for(Contact con : lstCon){ 
            if(con.AccountId != null){
               	setAccIds.add(con.AccountId);      
			}   
    	}
        for(Account acc :[Select id,about_contact__c ,(Select id,name from contacts) from Account where Id in : setAccIds]){ 
String s =''; 
for(Contact Con :acc.contacts){ 
s+=Con.Name +','; 
} 
acc.about_contact__c = s.removeEnd(','); 
acclist.add(acc); 
} 
if(acclist.size()>0){ 
update accList; 
    }
    }
}




@isTest
public class NumberOfChildTest {
@isTest
    public Static Void UnitTest(){
        Account Acc = new account();
        Acc.name ='test';
        insert Acc;
        
        Account Acc2 = new account();
        Acc2.name ='test2';
        insert Acc2;
        
        Contact Con = new Contact();
        Con.LastName = 'TestCon';
        Con.AccountId = Acc.Id;
        Insert Con;
        Con.AccountId = Acc2.Id;
      	Update Con;
Delete Con;
    }
}
Please Mark It As Best Answer If It Helps
Thank You!

 

All Answers

CharuDuttCharuDutt
Hii Sana
Try Below Trigger
trigger NumberOfChild on Contact (After Insert,After Update,After Delete) {
   List<Account> accList=new List<Account>();

    Set<Id> setAccIds = new Set<Id>();
    if(Trigger.isInsert){
         if(trigger.isAfter){
        for(Contact con : Trigger.new){
            if(con.AccountId != null){
            setAccIds.add(con.AccountId);
            	}
			}
		}
    } 
    system.debug('setAccIds ==> '+setAccIds);
    if(Trigger.isUpdate){
         if(trigger.isAfter){
        for(Contact con : Trigger.new){ 
            if(con.AccountId!=Trigger.oldMap.get(con.Id).AccountId){
               	setAccIds.add(con.AccountId);
                setAccIds.add(Trigger.oldMap.get(con.Id).AccountId);
            	}
          
			}        
        }
    }
    if(Trigger.isDelete){
        if(trigger.isAfter){
        for(Contact con : Trigger.old) { 
            if(con.AccountId != null){
            setAccIds.add(con.AccountId);
            	}
        	}
        }
    }    
    for(Account acc :[Select id,about_contact__c ,(Select id,name from contacts) from Account where Id in : setAccIds]){
      String s ='';
        for(Contact Con :acc.contacts){
            s+=Con.Name +',';
        }
        acc.about_contact__c =  s.removeEnd(',');
        acclist.add(acc);
    }
    if(acclist.size()>0){
        update accList;     
    }
}
Please Mark It As Best Answer If It Helps
Thank You!

 
Sana123Sana123
Thanku Charu ! Its works
Sana123Sana123
Can You Please tell me how to write this code on helper class because i am getiing error
Sana123Sana123
Its very urgent
Sana123Sana123
How to write handler class for this code
 
CharuDuttCharuDutt
Hii Sana 
Try Below Code
trigger NumberOfChild on Contact (After Insert,After Update,After Delete) {
    
    If(trigger.isAfter &&(Trigger.isInsert || Trigger.isUpdate)){
        NumberOfChildTriggerHandler.InsertUpdateDeleteMethod(trigger.new);
    }
     If(trigger.isAfter &&Trigger.isDelete){
        NumberOfChildTriggerHandler.InsertUpdateDeleteMethod(trigger.old);
    }
}




public class NumberOfChildTriggerHandler {
 
    public static Void InsertUpdateDeleteMethod(list<Contact> lstCon){
        List<Account> accList=new List<Account>();

    Set<Id> setAccIds = new Set<Id>();
         for(Contact con : lstCon){ 
            if(con.AccountId != null){
               	setAccIds.add(con.AccountId);      
			}   
    	}
        for(Account acc :[Select id,about_contact__c ,(Select id,name from contacts) from Account where Id in : setAccIds]){ 
String s =''; 
for(Contact Con :acc.contacts){ 
s+=Con.Name +','; 
} 
acc.about_contact__c = s.removeEnd(','); 
acclist.add(acc); 
} 
if(acclist.size()>0){ 
update accList; 
    }
    }
}




@isTest
public class NumberOfChildTest {
@isTest
    public Static Void UnitTest(){
        Account Acc = new account();
        Acc.name ='test';
        insert Acc;
        
        Account Acc2 = new account();
        Acc2.name ='test2';
        insert Acc2;
        
        Contact Con = new Contact();
        Con.LastName = 'TestCon';
        Con.AccountId = Acc.Id;
        Insert Con;
        Con.AccountId = Acc2.Id;
      	Update Con;
Delete Con;
    }
}
Please Mark It As Best Answer If It Helps
Thank You!

 
This was selected as the best answer