+ Start a Discussion
Todd B.Todd B. 

Adding Records to an object

I want to create a class that will allow me, on a monthy basis, to take a list of records from one object and add it to another.

Scenerio:  I have an object Service_Center__c that has the names of all my service centers.  On a monthly basis I want to query all the Services_center__C.name and place that list of Service Centers into another custom object Quota__c.  I want it to map the following:

Services_center__C.name - Quota__c.Servicer_center_name__c
01/01/2014 - Quota__c.Period_Start_date__c (this is a date field)
0 - Quota__c.quota__c (this is an integer)

How do I go about doing this?

James LoghryJames Loghry
You'll want to create two classes.  A batch job and a Scheduleable job that executes that batch job.

Your batch job will query for the Service_Center__c for the scope, and then create your Quota records in the execute method like below:

public class MyServiceCenterBatch implements Database.Batchable<Service_Center__c>{
    public List<Service_Center__c> start(Database.BatchableContext BC){
      return Database.getQueryLocator('Select Name From Service_Center__c');

    public void execute(Database.BatchableContext BC, List<Service_Center__c> scope){
        List<Quota__c> quotaList = new List<Quota__c>();
        for(Service_Center__c sc : scope){
            quotaList.add(new Quota__c(Name=sc.Name,//Other fields go here);
        insert quotaList;

    public void finish(Database.BatchableContext BC){}

Your schedulable class will kick off the batch job:

global class MyServiceCenterSchedulable implements Schedulable {
   global void execute(SchedulableContext ctx) {
       Database.executeBatch(new MyServiceCenterBatch());

Then if you go into Setup->Develop->Apex Classes, you can kick off the "MyServiceCenterSchedulable" class on a schedule using the "Scheduled Apex" button at the top of the Apex Classes list.

See the following links for more info on Batches and Schedulable Apex:



Todd B.Todd B.

Thanks for the help.  I am getting an error on line three of the MyServiceCenterBatch class:
Save error: Return value must be of type: LIST<Service_Center__c> 

Any ideas?


Todd B.