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
Akshata ambigAkshata ambig 

trigger to insert, update and delete records in contact when account is created

Suraj Tripathi 47Suraj Tripathi 47

Hi Akshata,

I am giving you a sample of the code. You can take references from it.

Case 1: Insert

trigger InsertContact on Account (after insert)
{
    List <Contact> cntLst = new List<Contact>();
        for(Account acc: Trigger.New)
        {
            Contact cnt = new Contact();
            cnt.LastName = 'chirag'+''+ acc.name;
             cnt.AccountId=acc.id;
            cntLst.Add(cnt);
         }
             insert cntLst;
}


Case 2: Update 

trigger accountcontat on Account (after update){
   List<Contact> contactList = new List<Contact>();
   List<Contact> contactList1 = new List<Contact>();
for(Account acc: [SELECT Id,(SELECT Id,checkbox__c FROM Contacts) FROM Account WHERE Id in: Trigger.new]){
   If(acc.Contacts.size()>0){
     contactList.addAll(acc.Contacts);
    }
}
for(contact c:contactList){
       c.checkbox__c=true;
       contactList1.add(c);
}
update contactList1;
}

Case 3: Delete

trigger deleteAccountRecord on Contact (after delete) {


    RecordType donorRecType = [SELECT ID FROM RecordType WHERE sObjectType = 'Contact' AND DeveloperName = 'Donor_Contact'];
    if(Trigger.isDelete){
        List<ID> accIds = new List<ID>();
        for(Contact contactToDelete: Trigger.old) {
            if (contactToDelete.RecordTypeId != donorRecType.Id ) {
                  
                    contactIDList.add(contactToDelete.AccountId);
            }
        }
      
       
       List<Account> DelAccountRecordList = [select id from Account where Id IN:accIds];

        if(DelAccountRecordList.size() >0){
           delete DelAccountRecordList ;
         
        }
     }
  }

Please let me know it is working for you or not,

Please mark it as the Best Answer if it helps you.

Thank You

 

 

CharuDuttCharuDutt
Hii Akshata
Try Below Code
trigger AccountRElatedContact on Account (After insert, After update,Before delete) {
    Set<Id> lstIds = new Set<Id>();
    list<Account>lstAcc=new list<Account>();
    if (trigger.isAfter && trigger.IsInsert) {
        for (Account Acc : trigger.new) {
            Contact Con = new Contact();
            Con.FirstName = Acc.Name;
            Con.LastName = 'Test';
            Con.AccountId = Acc.Id;
            Insert Con;
        }
    }
    if (trigger.isAfter && trigger.IsUpdate) {
        for (Account Acc : trigger.new) {
            if(Acc.Name != Trigger.oldMap.get(Acc.Id).Name){
                lstIds.Add(Acc.Id);
            }
        }
    }
    if (trigger.isBefore && trigger.IsDelete) {
        for (Account Acc : trigger.old) {   
                lstIds.Add(Acc.Id);  
        }
    }
    
	for(Account acc :[Select Id,Name,(Select id,FirstName,LastName from Contacts) from Account where Id IN : lstIds]);	
    string Name = Acc.name;
        for(Contact Con : acc.Contacts){ 
			Con.FirstName = Name;
            lstCon.Add(Con);
        }
    lstAcc.Add(Acc);
    }
    if(lstAcc.size()>0){
        update lstAcc;     
    }

	if (trigger.isBefore && trigger.IsDelete) {
        list<Contact> lstConDelete = [Select id,FirstName,LastName,AccountId from Contact Where AccountId IN:lstIds];
        if(lstConDelete.size()>0){
            delete lstConDelete;
        }
	}

}
Please Mark it As Best Answer If It Helps Thank You!
 
Britto Fernandez 2Britto Fernandez 2

Please use helper class.
Suraj Tripathi 47Suraj Tripathi 47

Hi,

Trigger:

trigger InsertContact on Account (after undelete, before delete,After Insert,After Update,before insert,before update)
{

if(Trigger.isAfter && trigger.isInsert) {
	  data.createData(trigger.new);
}else if(Trigger.isAfter && trigger.isUpdate){
     data.updateData(trigger.new);
}else if(Trigger.isAfter && trigger.isDelete){
       data.deleteData(trigger.old);
}
 
}


Helper class:

public class data{

 public static void createData(List<Account> newAccountList){
 
 List <Contact> cntLst = new List<Contact>();
        for(Account acc: newAccountList)
        {
            Contact cnt = new Contact();
            cnt.LastName = 'chirag'+''+ acc.name;
             cnt.AccountId=acc.id;
            cntLst.Add(cnt);
         }
             insert cntLst;
 
 }




 public static void updateData(List<Account> newAccountList){
 List<Contact> contactList = new List<Contact>();
   List<Contact> contactList1 = new List<Contact>();
for(Account acc: [SELECT Id,(SELECT Id,checkbox__c FROM Contacts) FROM Account WHERE Id in: newAccountList]){
   If(acc.Contacts.size()>0){
     contactList.addAll(acc.Contacts);
    }
}
for(contact c:contactList){
       c.checkbox__c=true;
       contactList1.add(c);
}
update contactList1;
 }



 public static void deleteData(List<Account> oldAccountList){
 RecordType donorRecType = [SELECT ID FROM RecordType WHERE sObjectType = 'Contact' AND DeveloperName = 'Donor_Contact'];
    
        List<ID> accIds = new List<ID>();
        for(Contact contactToDelete: oldAccountList) {
            if (contactToDelete.RecordTypeId != donorRecType.Id ) {
                  
                    contactIDList.add(contactToDelete.AccountId);
            }
        }
      
       
       List<Account> DelAccountRecordList = [select id from Account where Id IN:accIds];

        if(DelAccountRecordList.size() >0){
           delete DelAccountRecordList ;
         
        }
 }

 }

Please mark it as the Best Answer

Thank You