+ Start a Discussion
SimrinSimrin 

Adding data to custom object

Hello,

I want to add sample data to a custom object.
This data will be inserted to around 300 uers and 100 data each so I want to insert 300 users * 100 rows.

For (All users){
custom__C c = custom__C();
c.user = user;
c.column1 = 'XYZ';
c.column2 = 2;
c.column3 = 'PQR';
insert c; 
}

i assume that the saleforce has limitation for number of SQl queries.

How can i achieve it ?
Best Answer chosen by Simrin
Himanshu ParasharHimanshu Parashar
you can use following code for this
 
global class ExampleBatchClass implements Database.Batchable<sObject>{

        global ExampleBatchClass(){
                   // Batch Constructor
        }
       
        // Start Method
        global Database.QueryLocator start(Database.BatchableContext BC){
         String query='WRITE YOUR SOQL HERE.';
         return Database.getQueryLocator(query);
        }
      
      // Execute Logic
       global void execute(Database.BatchableContext BC, List<sObject>scope){
              // Logic to be Executed batch wise      
              
       }
     
       global void finish(Database.BatchableContext BC){
            
       }
    }

and here is the way to call this 
 
ExampleBatchClass b = new ExampleBatchClass(); 
//Parameters of ExecuteBatch(context,BatchSize)
database.executebatch(b,10);


Thanks,
Himanshu

All Answers

Himanshu ParasharHimanshu Parashar
Hi Simrin,

Here is the correct way to do that.
 
List<Custom__c> lstCustomObject=new List<Custom__c>();

For (All users){
custom__C c = custom__C();
c.user = user;
c.column1 = 'XYZ';
c.column2 = 2;
c.column3 = 'PQR';
lstCustomObject.add(c); //add items to list
}

if(lstCustomObject.size()>0)
{
insert lstCustomObject;
}


Thanks,
Himanshu
SimrinSimrin

error

I have below errror.
how can i tackle this one.

Can i export the output to some excel file and import it using dataloader

Himanshu ParasharHimanshu Parashar
As I can see you hve 36120 records where limit is 10000 so you have two options.

1. add Limit to your SOQL.
2. use batch to update records.
SimrinSimrin
If i reduce number of rows it gives new error 

"Time limit exceeded
Your request exceeded the time limit for processing. "

How can i do batch to insert records
Himanshu ParasharHimanshu Parashar
you can use following code for this
 
global class ExampleBatchClass implements Database.Batchable<sObject>{

        global ExampleBatchClass(){
                   // Batch Constructor
        }
       
        // Start Method
        global Database.QueryLocator start(Database.BatchableContext BC){
         String query='WRITE YOUR SOQL HERE.';
         return Database.getQueryLocator(query);
        }
      
      // Execute Logic
       global void execute(Database.BatchableContext BC, List<sObject>scope){
              // Logic to be Executed batch wise      
              
       }
     
       global void finish(Database.BatchableContext BC){
            
       }
    }

and here is the way to call this 
 
ExampleBatchClass b = new ExampleBatchClass(); 
//Parameters of ExecuteBatch(context,BatchSize)
database.executebatch(b,10);


Thanks,
Himanshu
This was selected as the best answer
SimrinSimrin
i break the SOQL results to derive less rows and it worked ok but Good way to do it will be in Batch (I have not tried this).

For others i want to jot down some problems i faced
- CPU limit exceeeded
- time out as query taking long times
- maximum view state reached.

Thank you Himanshu for your inputs
Himanshu ParasharHimanshu Parashar
HI Kiran,

Your welcome!.

Yes, It is always advised to use batch while you are working on millions of records to avoid all those limit which you have got.