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
Ruxandra Oprea 9Ruxandra Oprea 9 

Hi, I need to create an account for every exiting contact, could anyone help?

Hi, I need to create an account for every exiting contact, could anyone help?
Note: the existing contacts are added from an external app that's why I was able to have contacts without related account.

I need to do it inn execute nonymus window.

Thank you so much!

Malika Pathak 9Malika Pathak 9

Hi Ruxandra,

Please Find The Solution.Hi, I need to create an account for every existing contact, could anyone help?

List<Contact> contactList=new List<Contact>();
contactList=[select id from Contact];
 
for(Contact con:contactList){
    Account ac=new Account();
    ac.name='raju';
    insert ac;
   con.accountid=ac.id;
}
update contactList;

Please let me know it is working or not?
If You find this solution is helpful for you please mark the best answer.

Thanks

 

ANUTEJANUTEJ (Salesforce Developers) 
Hi Ravindra,

If you have issues with the code aspect then you can try checking OOB tools like flow using which you can create records, flow is a drag and drop tool that you can check out.

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
sachinarorasfsachinarorasf
Hi Ruxandra,
You can use the below piece of code:
List<Contact> contactsList =new List<Contact>();
contactsList = [select id from Contact Limit 49999];
if(contactsList != null && contactsList.size() >0){
  for(Contact con: contactsList){
      Account account = new Account();
      account.name='raju';
      insert account;
      con.AccountId=account.Id;
   }
   update contactsList;
}

I hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too.
Thanks and Regards,
Sachin Arora
www.sachinsf.com (http://www.sachinsf.com" style="text-decoration:none)

Andrew GAndrew G
They are horrible code examples.  INSERT inside a LOOP??  What are they teaching people these days?