Take the 2-minute tour ×
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.

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
add comment

1 Answer

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 {
        public String id;
        public String entity;
        public String field;
        public 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 = dto.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();
dto.id = '001d000000UaWiX';
dto.entity = 'Account';
dto.field = 'Name';
dto.value = 'Herp Derp Doop';
dtos.add(dto);

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

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

Database.executeBatch(new InvoiceFieldUpdater(dtos), 1);
share|improve this answer
add comment

Your Answer

 
discard

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.