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
Samadhan Sakhale 3Samadhan Sakhale 3 

Copy single field records from one object on another

hi,
     i have to copy one field all records(Which is already Stored)from custom object to Standard object by creating field on standard Object.
Anyone please tell me example of scheduling this type of job.
thanks,
Sam
Best Answer chosen by Samadhan Sakhale 3
Deepak Kumar ShyoranDeepak Kumar Shyoran
Below is an example of batch class to copy Account > Phone to Contact > Phone for all Existing records.

public with sharing class CopyAccPhoneToContPhone_Batch implements Database.Batchable<sObject>
{

public Database.QueryLocator start(Database.BatchableContext BC) 
{
     return Database.getQueryLocator('SELECT Id,Phone FROM Account );
    }
    public void execute(Database.BatchableContext BC, List<Account> scope)
    {
     List<Contact> conToUpdate = [Select id,AccountId,Phone from Contact where AccountId in : scope] ;
    
  Map<Id,Account> accMap = new Map<Id,Account>(scope) ;
 
  // Loop to copy account Phone to contact phone
  for(Contact con : conToUpdate)
      con.Phone =  accMap.get(con.AccountId).Phone ;
    
  if(conToUpdate.size() > 0)
      //update conToUpdate;
    }
    public void finish(Database.BatchableContext BC)
    {}
}



Hope this will help you.

All Answers

Deepak Kumar ShyoranDeepak Kumar Shyoran
You can create a batch class which will the field from Custom Object and will update that on Standard object. And then can schedule that batch class on either hourly/daily/weekly or monthly basic. You can start with batch class from here  http://www.salesforce.com/us/developer/docs/dbcom_apex250/Content/apex_batch_interface.htm and can find cron expression to schedule the batch easily according to your requirement.
Samadhan Sakhale 3Samadhan Sakhale 3
hi Deepak,
      Can you please give any example which shows the batch class those copy address field record from contact and store onto the Account table by using it's ID's.
Thanks.
Sam
Deepak Kumar ShyoranDeepak Kumar Shyoran
Below is an example of batch class to copy Account > Phone to Contact > Phone for all Existing records.

public with sharing class CopyAccPhoneToContPhone_Batch implements Database.Batchable<sObject>
{

public Database.QueryLocator start(Database.BatchableContext BC) 
{
     return Database.getQueryLocator('SELECT Id,Phone FROM Account );
    }
    public void execute(Database.BatchableContext BC, List<Account> scope)
    {
     List<Contact> conToUpdate = [Select id,AccountId,Phone from Contact where AccountId in : scope] ;
    
  Map<Id,Account> accMap = new Map<Id,Account>(scope) ;
 
  // Loop to copy account Phone to contact phone
  for(Contact con : conToUpdate)
      con.Phone =  accMap.get(con.AccountId).Phone ;
    
  if(conToUpdate.size() > 0)
      //update conToUpdate;
    }
    public void finish(Database.BatchableContext BC)
    {}
}



Hope this will help you.
This was selected as the best answer
Samadhan Sakhale 3Samadhan Sakhale 3
thanks dipak,
     But it Gives error 'Arguement must be an object that implements database.batchable'
so tell me the solution.
regards,
Sam
Deepak Kumar ShyoranDeepak Kumar Shyoran
This error is not related to the above code it may be coming when you try to Schedule a batch. This is modified code and it's working fine as I've tested it.

public with sharing class CopyAccPhoneToContPhone_Batch implements Database.Batchable<sObject>
{

public Database.QueryLocator start(Database.BatchableContext BC) 
{
     return Database.getQueryLocator('SELECT Id,Phone FROM Account ');
    }
    public void execute(Database.BatchableContext BC, List<Account> scope)
    {
     List<Contact> conToUpdate = [Select id,AccountId,Phone from Contact where AccountId in : scope] ;
    
  Map<Id,Account> accMap = new Map<Id,Account>(scope) ;
 
  // Loop to copy account Phone to contact phone
  for(Contact con : conToUpdate)
      con.Phone =  accMap.get(con.AccountId).Phone ;
     
  if(conToUpdate.size() > 0)
      update conToUpdate;
    }
    public void finish(Database.BatchableContext BC)
    {}
}
  And schedule this batch.

Database.executeBatch(new CopyAccPhoneToContPhone_Batch ()) ;
Samadhan Sakhale 3Samadhan Sakhale 3
Hi Deepak,
     I am trying to copy all cities from Student custom object in Address1 custom object into City__c field.all code working properly and code coverage also 100%
but the problem is that after running program cities not copied in Address table and i also trying to check program execution using System.Debug(''); but the statement not printed after executon in log my Code is here


global class CopyStudtoAdr_Batch implements Database.Batchable<sObject>
{
global Database.QueryLocator start(Database.BatchableContext BC)
{
     String query='SELECT ID,City__c FROM Student__c';
     return Database.getQueryLocator(query);
   }
    global void execute(Database.BatchableContext BC, List<Student__c> scope)
    {
      System.debug('Batch Class Started....');
      //List<Address1__c> adrToUpdate = [Select id,StudentID__c,City__c from Address1__c where StudentID__c in : scope] ;
      //Set<String> setSturdentIds = new Set<String>();
      List<Address1__c> adrToUpdate = [Select Id,StudentID__c,City__c from Address1__c where StudentID__c in : Scope];

     Map<ID,Student__c> stdMap = new Map<ID,Student__c>(scope) ;
       
       // Loop to copy account Student to Address phone
       for(Address1__c adr : adrToUpdate)
        {
         adr.City__c =  stdMap.get(adr.StudentID__c).City__c ;
         }
       if(adrToUpdate.size() > 0)
       update adrToUpdate;
      }
     global void finish(Database.BatchableContext ctx)
      {}
}

Please check it and tell me Solution

Thanks,
Sam