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 

Batchable Class Query

How to Copy Custom Object Field record (i.e.phone) to Standard Object field.
Reply me.
Best Answer chosen by Samadhan Sakhale 3
Shashikant SharmaShashikant Sharma
As I see the code It appears to me that field StudentID__c on is Text field not a reference field (Lookup or Master detail) . I think you should make it Lookup in case if it is text field.

If you need to have it as text then you have to creae a
Set<String> setSturdentIds = new Set<String>();
and loop over scope and fill it. Then use it this query

List<Address1__c> adrToUpdate = [Select id,StudentID__c,City__c from Address1__c where StudentID__c in : setSturdentIds ]


Let me know if it does not solve your problem.

All Answers

Shashikant SharmaShashikant Sharma
To copy a field value from one object to another is done mostly by trigger, Could you give more information on this. 
Samadhan Sakhale 3Samadhan Sakhale 3
hi Shashikant,
      actually ihave to copy all the records of field city from student object to address1 object in field city so by using batch job
all records copied at a time and placed in front of related data only and that may use ID's to find actual record
here is code by using i am trying to copy

lobal 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)
    {
     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 Phone to contact phone
   for(Address__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)
      {}
}

But in this code it gives error    'Invalid bind expression type of SOBJECT:Student__c for column of type String'
so please help me for solve example
Thank you,
Regards,
Sam
Shashikant SharmaShashikant Sharma
As I see the code It appears to me that field StudentID__c on is Text field not a reference field (Lookup or Master detail) . I think you should make it Lookup in case if it is text field.

If you need to have it as text then you have to creae a
Set<String> setSturdentIds = new Set<String>();
and loop over scope and fill it. Then use it this query

List<Address1__c> adrToUpdate = [Select id,StudentID__c,City__c from Address1__c where StudentID__c in : setSturdentIds ]


Let me know if it does not solve your problem.
This was selected as the best answer
Samadhan Sakhale 3Samadhan Sakhale 3
hi Shashikant,
    As you suggested i follow each and every procedure but when i run my test class that time it gives only 11% coverage i didnt understand the problem
so please help me.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)
    {
     //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 : setSturdentIds];

      Map<ID,Student__c> stdMap = new Map<ID,Student__c>(scope) ;

      // Loop to copy account Phone to contact 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)
      {}
}

and test class is,

@isTest(seeAllData = true)
public class TestStudent
{
static testMethod void Test_Copystudtoadr()
{

String query = 'select ID,City__c from Student__c';
List<Student__c> stdList = new List<Student__c>();

for (Integer i=0;i<200;i++)
{
Student__c std = new Student__c(Name='Test Student' + i);
stdList.add(std);
}
insert stdList;
System.Test.startTest();
System.debug('Test Started....');
CopyStudtoAdr_Batch csta = new CopyStudtoAdr_Batch();
Database.executeBatch(csta, 200);
System.Test.stopTest();
System.debug('Test stopped');   
}
}
Thanks.