0

need yur help to create account record using batch class,,

global class batchClass implements Database.batchable<sObject>{ 
    public String query;

       global Database.QueryLocator start(Database.BatchableContext BC){

          return Database.getQueryLocator(Query); 
         }   
       global void execute(Database.BatchableContext info, List<Account> scope){
           Account accsToUpdate=new Account(Name='ssa');
           insert accsToUpdate; 
       }     
       global void finish(Database.BatchableContext info){     
       } 

}

i started like this,i know it is wrong, please tell me what to do,

7
  • you want to create account using batch class. so what is the Query ?
    – Ratan Paul
    Aug 3, 2016 at 6:42
  • i tired with the abovre code,,,record not created,,i want to know what is the problem there
    – Satya
    Aug 3, 2016 at 6:50
  • How are you executing this batch class ?
    – Devendra
    Aug 3, 2016 at 6:52
  • from developer console
    – Satya
    Aug 3, 2016 at 6:54
  • batchClass bulkupdate= new batchClass (); database.executeBatch(bulkupdate);
    – Satya
    Aug 3, 2016 at 6:54

3 Answers 3

2

If you don't have any records to use, you can use an iterable instead. Here's an iterable-based example:

public class CreateAccounts implements Database.Batchable<String> {
  public String[] start(Database.BatchableContext context) {
    return new String[] { 'ssa' };
  }
  public void execute(Database.BatchableContext context, String[] scope) {
    Account[] records = new Account[0];
    for(String accountName: scope) {
      records.add(new Account(Name=accountName));
    }
    insert records;
  }
  public void finish(Database.BatchableContext context) {
  }
}

It's up to you to determine what the appropriate data structure would look like. The main point is that a query is not the only way to kick off a batchable process.

0

Check the Salesforce Doc for for execution of batch class Batch Apex

global class CreateAccountRecordsBatch implements Database.Batchable<sObject>{

   global final String Query;
   global CreateAccountRecordsBatch(String q){
        Query=q;
   }

   global Database.QueryLocator start(Database.BatchableContext BC){
      return Database.getQueryLocator(query);
   }

   global void execute(Database.BatchableContext BC, List<Account> scope){
     List<Account> accountList = new List<Account>();
     for(Account acc : scope){
        Account objA = new Account(Name = acc.Name);
        accountList.add(objA);
     }
     insert accountList;
    }

   global void finish(Database.BatchableContext BC){
   }
}


//Execute Batch class from developer console
// Query for 10 accounts
String q = 'SELECT Id, Name, Industry FROM Account LIMIT 10';

Id batchInstanceId = Database.executeBatch(new CreateAccountRecordsBatch(q), 5);
0
-1

Below is the sample code, by which you can create records by batch class.


global class InsertAccountContact implements Database.Batchable<sObject>{

     global InsertAccountContact(){
                // Batch Constructor
     }

     // Start Method
     global Database.QueryLocator start(Database.BatchableContext BC){
     // Generate your string query on any object here with limit 10000
     String Query = 'select id,name from account limit 2';
     //Query is Required on object which you want to run Batch
     return Database.getQueryLocator(Query);
     }

   // Execute Logic
    global void execute(Database.BatchableContext BC, List<sObject>objlist){
    system.debug('>>>>>>execute>>>>>'+objlist);
        List<Account> acclist = new List<Account>();
        list<Contact> conlist = new list<contact>();
          for(Sobject obj: objlist){
                Account acc = new account();
                acc.name = 'Account_CreatedByBatchClass';
                acclist.add(acc);
          }
       insert acclist;
    for(Account acc : Acclist){
          Contact con = new Contact();
           con.lastname = 'Contact_CreatedByBatchClass';
           con.accountid = acc.id;
           conlist.add(con);
   }  
   Insert conlist;
    }
    global void finish(Database.BatchableContext BC){
         // Logic to be Executed at finish
    }
 }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.