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
tarun jain 110tarun jain 110 

How to make copy of Account record in to custom object

Hi,
I want to copy all accounts record in custom object. How can we copy all record in the custom object. Please provide apex class code.

Thanks
Aniket Malvankar 10Aniket Malvankar 10
Hello Tarun,

Write a Apex class and execute it from console window.
You need to keep this as bulify. 

Thanks
Aniket
tarun jain 110tarun jain 110
I need code how to make copy of Account object in to custom object
gayatri sfdcgayatri sfdc
Hi tarun,

Here is the code sample
1. you need to add all the necessary data of account into custom object 
2.If the account is having huge data, then you should  use a batch class

public class AccountCloneData{
    public list<Account> acclist = new  list<Account>();
    public list<CustomerInfo__c> cuslist = new  list<CustomerInfo__c>();
    public AccountCloneData(){
      acclist = [Select id, name from account];
    }
    public void clonyacct(){
        for(Account a : acclist){
            CustomerInfo__c  c = new CustomerInfo__c();
            c.name= a.name;
            cuslist.add(c);            
        }
      insert cuslist;
    }
}

AccountCloneData ap = new AccountCloneData();
ap.clonyacct();

Keep Learning:)


Thanks Gayatri
tarun jain 110tarun jain 110
Thanks Gayatri,

Can you also provide such activity done by batch class.

Thanks
Tarun
Jitendra pratap singhJitendra pratap singh
Hello Gayatri
No need to create instance again. You can do this as below.

public class AccountCopy{
    public List<Account> accList = new List<Account>();
    public List<CustomObject__c> custObjList = new List<CustomObject__c>();
    public accountCopy(){
        accList = [SeELECT Id from Account];
        copyData();
    }
    public void copyData(){
        for(Account objAccount : accList){
        CustomObject__c objCustomObject = new CustomObject__c();
        objCustomObject.name =     objAccount.name;
        custObjList.add(objCustomObject);
        }        
        insert custObjList;
    }
}
 
Jitendra Pratap Singh
www.mirketa.com
tarun jain 110tarun jain 110
Hi Jitendra,

Facing error of "DML currently not allowed" when i create object of class in another class.
gayatri sfdcgayatri sfdc
Hi Jitendra,

Thank you, you are correct.

Tarun,
 I have executed the jitendra changes and its working fine. please tell me where exactly you are getting that.
 
gayatri sfdcgayatri sfdc
Hi Tarun,

I have executed in anonymous window, may be thats why its working fine. you would have used that in component/apex class.

DML not currently allowed errors will occur if:
 
(1) You attempt DML in a component without the AllowDML=true   ()
(2) You attempt DML in a controller constructor (If you are using this controller  from a VF Page, then use the action attribute on the apex:page tag) - I Guess you are facing this error as you are calling the copydata() method in the constructor
(3) You attempt DML in a get method in a controller.
tarun jain 110tarun jain 110
Hi Gayatri,

Thanks for your suggestions.

If there is scenario that i want to do same work only with class not with visual page as when class create it automatically make copy of account in custom object. How it would be done in salesforce.

Gayatri, can you tell me if the account is having huge data. You told about batch class so can you provide me sample code for that it would be helpful for me.

 
gayatri sfdcgayatri sfdc
Hi tarun,

Here is the logic for  Batch class:

global class CopyAccBatch implements Database.Batchable<sObject> {
    public List<Account> accList = new List<Account>();
    public List<CustomerInfo__c> custObjList = new List<CustomerInfo__c>();
    global Database.QueryLocator start(Database.BatchableContext bc)
    {
        String query= 'SELECT id, Name from Account';
        return Database.getQueryLocator(query); 
    }
    global void execute(Database.BatchableContext bc, List<Account> scope)
    {
        for(Account a : scope)
        {
            CustomerInfo__c  objCustomObject = new CustomerInfo__c  ();
            objCustomObject.name = a.name;
            custObjList.add(objCustomObject);
        }
           insert custObjList;
    }
    
    global void finish(Database.BatchableContext bc)
    {
        
    }
}


Execution:

Id batchJobId = Database.executeBatch(new CopyAccBatch(), 10);

Mark this answer as best, if its helpful for you.

Keep Learning:)

Thanks
Gayatri