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
Abhay MohanAbhay Mohan 

How to remove account record along with related contact based on account name supplied at runtime

Suraj Tripathi 47Suraj Tripathi 47

Hi,

Please find the solution.

List<Contact> contactData=new List<Contact>([select id,accountId from Contact where accountid !=null limit 1000]);
	List<Account> accountList=new List<Account>([select id,Name from Account where Name='abc']);
	
	List<Contact> contactUpdate=new List<Contact>();
	for(Contact con:contactData){
	if(con.accountId==accountList[0].id){
	 Contact conn=new Contact();
	 conn.id=con.id;
	 conn.accountId=null;
	 contactUpdate.add(conn);
	}
	}
	update contactUpdate;


Please mark it as the best answer so that other people would take reference from it.

Thank You