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
nilesh walkenilesh walke 

. If you delete any account record corresponding child contact records to be assigned to a Particular account. (Associate Contact record to another Account)

i am new to this fail to build the logic of it  any ides please ping me thanks
Best Answer chosen by nilesh walke
Suraj Tripathi 47Suraj Tripathi 47
Hi nilesh,

Thanks for your reply. Please check this code

Trigger:-
trigger AccountTrigger on Account(before delete){
  if(trigger.isBefore && trigger.isDelete){
      contactRelatedToAccount.method(trigger.old);
  }
}

Apex:-
public class ContactRelatedToAccount {
    public static void method(List<account> accList){
 
        list<contact> conList= [select lastname,accountid from contact where accountid IN: accList];
        
        account acc=[select id from account LIMIT 1];
        
        list<contact> updateConList= new List<contact>();
        if(conList.size()>0){
            for(contact con: conList){
                con.AccountId=acc.Id;
                updateConList.add(con);
            }  
        }
        if(updateConList.size()>0){
            update updateConList;
        }
    }
}

 

All Answers

Suraj Tripathi 47Suraj Tripathi 47
Hi Nilesh,

You can take reference from this below code.
public class ContactRelatedToAccount {
    public static void method(){
        Account acc=[select name from account where name ='rahul' LIMIT 1];
        
        list<contact> conList= [select lastname,accountid from contact where accountid =: acc.Id];
        
        account ac=new account();
        ac.Name='new account2';
        insert ac;
        
        list<contact> updateConList= new List<contact>();
        if(conList.size()>0){
            for(contact con: conList){
                con.AccountId=ac.Id;
                updateConList.add(con);
            }  
        }
        if(updateConList.size()>0){
            update updateConList;
        }
        
        if(acc!=null){
           delete acc;
        }
        
    }
}

In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer. 

Thanks and Regards
Suraj Tripathi.
nilesh walkenilesh walke
hello suraj we dont need to create a custom record it ill be trigger after we delete it  
Suraj Tripathi 47Suraj Tripathi 47
Hi nilesh,

Thanks for your reply. Please check this code

Trigger:-
trigger AccountTrigger on Account(before delete){
  if(trigger.isBefore && trigger.isDelete){
      contactRelatedToAccount.method(trigger.old);
  }
}

Apex:-
public class ContactRelatedToAccount {
    public static void method(List<account> accList){
 
        list<contact> conList= [select lastname,accountid from contact where accountid IN: accList];
        
        account acc=[select id from account LIMIT 1];
        
        list<contact> updateConList= new List<contact>();
        if(conList.size()>0){
            for(contact con: conList){
                con.AccountId=acc.Id;
                updateConList.add(con);
            }  
        }
        if(updateConList.size()>0){
            update updateConList;
        }
    }
}

 
This was selected as the best answer