Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

i read the documentation and it has been given like the one given below.

global class UpdateInvoiceFields implements Database.Batchable<sObject>{
   global final String Query;
   global final String Entity;
   global final String Field;
   global final String Value;

   global UpdateInvoiceFields(String q, String e, String f, String v){
             Query=q; Entity=e; Field=f;Value=v;

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

   global void execute(Database.BatchableContext BC, 
                       List<sObject> scope){
      for(Sobject s : scope){s.put(Field,Value); 
      }      update scope;

   global void finish(Database.BatchableContext BC){



also, the code snippet which is used to invoke the above batch is given below,

// Query for 10 invoice statements
String q = 'SELECT Description__c FROM Invoice_Statement__c LIMIT 10';
String e = 'Invoice_Statement__c';
String f = 'Description__c';
String v = 'Updated description';
Id batchInstanceId = Database.executeBatch(new UpdateInvoiceFields(q,e,f,v), 5); 

Instead of using a query to invoke a batch, i need to send a custom list which is not a sobject to the batch to start with. Is that possible? Any ideas?

Thanks, Baskaran

share|improve this question
up vote 8 down vote accepted

Would be great to learn more about what is driving your requirement to update fields this way (third party system?). But it's definitely possible, you can hand in a non-SObject iterable like this:

public class InvoiceFieldUpdater implements Database.Batchable<FieldDto> {

     * You will need to massage the response
     * from your web service into this class.
    public class FieldDto {
        String id;
        String entity;
        String field;
        String value;

    private List<FieldDto> dtos;

    public InvoiceFieldUpdater(List<FieldDto> dtos) {
        this.dtos = dtos;

    public Iterable<FieldDto> start(Database.BatchableContext context) {
        return this.dtos;

     * You can dynamically instantiate the correct
     * type of SObject for performing the update.
    public void execute(Database.BatchableContext context, List<FieldDto> scope) {
        FieldDto dto = scope[0];

        SObject invoice = (SObject)Type.forName(dto.entity).newInstance();
        invoice.put(dto.field, dto.value);
        invoice.Id =;
        update invoice;

    public void finish(Database.BatchableContext context) {}

Example usage:

List<InvoiceFieldUpdater.FieldDto> dtos = new List<InvoiceFieldUpdater.FieldDto>();
InvoiceFieldUpdater.FieldDto dto;

dto = new InvoiceFieldUpdater.FieldDto(); = '001d000000UaWiX';
dto.entity = 'Account';
dto.field = 'Name';
dto.value = 'Herp Derp Doop';

dto = new InvoiceFieldUpdater.FieldDto(); = '001d000000UaWiZ';
dto.entity = 'Account';
dto.field = 'Name';
dto.value = 'Foo Bar Baz';

dto = new InvoiceFieldUpdater.FieldDto(); = '001d000000UaWiW';
dto.entity = 'Account';
dto.field = 'Name';
dto.value = 'One Two Three';

Database.executeBatch(new InvoiceFieldUpdater(dtos), 1);
share|improve this answer
few questions, 1. Does the class FieldDto need to implement Iterable interface ? 2. Any reason why all fields of FieldDto are public ? – apn Aug 25 '15 at 3:24
1. no need, as it iterates over a List<Dto> 2. good point, private is ok @apn – bigassforce Aug 25 '15 at 10:06

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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