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
Alex ShAlex Sh 

Copy Fields from one obj to another

Hi All,
I have  a custom objects Obj1 and Obj2. I need to create appropriate Obj2 and copy field to it from Obj1. As I understand I need to use Batch clas but how exactly I can do this?
Thanks!
Best Answer chosen by Alex Sh
Suraj TripathiSuraj Tripathi
Hi Alex Shelaputin,

I have a sample code that may help you, In copying data from one Object to Another.(Using Batch class)

In this code we have a custom field ' count__c 'on custom object  ' obj1 ' and another custom object 'obj2' and data from obj1 is copied to obj2 that has a custom field with same name ie: count__c.
And You can also schedule this class for every 5 mins.
global class Batch8 implements Database.batchable<Sobject> {		// main class
  
  global Database.QueryLocator start(Database.BatchableContext bc)
   {
   
       String s='SELECT Id,count__c  FROM obj1 limit 1 ';
       System.debug(s);
      return Database.getQueryLocator(s);
      
    }
   global void execute(Database.BatchableContext bc, obj1 obj1_type)
    {
       obj2 instance_obj2=new obj2();
      instance_obj2.count__c=obj1_type.count__c;
		insert  instance_obj2;
       
    }   
    global void finish(Database.BatchableContext bc)
    {
        
    }     
    
}
 
public with sharing class batch8_run {		//// this class will execute above batch class
  
  public static void m()
  {
    Batch8 b=new Batch8();
    database.executeBatch(b);
  }
    
}
Ask me if you can any further queries. Or plz mark this solution as the best answer.
 

All Answers

Suraj TripathiSuraj Tripathi
Hi Alex Shelaputin,

I have a sample code that may help you, In copying data from one Object to Another.(Using Batch class)

In this code we have a custom field ' count__c 'on custom object  ' obj1 ' and another custom object 'obj2' and data from obj1 is copied to obj2 that has a custom field with same name ie: count__c.
And You can also schedule this class for every 5 mins.
global class Batch8 implements Database.batchable<Sobject> {		// main class
  
  global Database.QueryLocator start(Database.BatchableContext bc)
   {
   
       String s='SELECT Id,count__c  FROM obj1 limit 1 ';
       System.debug(s);
      return Database.getQueryLocator(s);
      
    }
   global void execute(Database.BatchableContext bc, obj1 obj1_type)
    {
       obj2 instance_obj2=new obj2();
      instance_obj2.count__c=obj1_type.count__c;
		insert  instance_obj2;
       
    }   
    global void finish(Database.BatchableContext bc)
    {
        
    }     
    
}
 
public with sharing class batch8_run {		//// this class will execute above batch class
  
  public static void m()
  {
    Batch8 b=new Batch8();
    database.executeBatch(b);
  }
    
}
Ask me if you can any further queries. Or plz mark this solution as the best answer.
 
This was selected as the best answer
Alex ShAlex Sh
Thank you! That helped.