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 need to loop over a bunch of records from a custom object and set a value on a new field. I expect the total number of records at about 1000-1200 only.

Is batch apex the only way to do this? I feel its a little overkill for the task I need to accomplish. Any pointers/examples would be welcome.

Thank you.

share|improve this question
1  
Batch Apex is certainly not the only option. The question is when should this be happening? Every time a record is inserted, or every night, or whenever you want? That would determine how the process is initiated. Please clarify in your question –  Jeremy Nottingham Jan 1 '13 at 2:47
add comment

3 Answers 3

up vote 3 down vote accepted

How do you intend for this update to be triggered, batch apex is typically for high volume processing, where you select upto 50 million records as part of the start method and the result set is chunked and passed to execute.

If all you intend is for it to be scheduled, you can implement Schedulable.

Alternatively, if the event can be based on a database operation, a trigger could also be a possibility.

share|improve this answer
    
The "triggering" is actually part of my question. An APEX trigger is out of the question as I need to go and modify historical records. How can "Schedulable" classes be run? –  Richard N Dec 31 '12 at 23:26
    
Implement the Schedulable interface and the. You can schedule either via the UI or using a cron expression using System.schedule salesforce.com/us/developer/docs/apexcode/Content/… –  techtrekker Dec 31 '12 at 23:30
    
I don't understand why a trigger would not work. If this really should happen when a database operation is being performed, it shouldn't be a challenge to work with other records. –  Jeremy Nottingham Jan 1 '13 at 2:42
    
@Jeremy, can triggers be used to update existing records? If so what EVENT can do this? Per my understanding triggers can be used during insert,updates and deletes. –  Richard N Jan 1 '13 at 23:04
    
Triggers have access to the entire database, regardless of which specific record caused the trigger to fire. For example, an Account trigger can do updates on all Contacts attached to that Account. I would need more detail on your use case to say whether this would work for you. –  Jeremy Nottingham Jan 2 '13 at 16:35
show 2 more comments

If this is a one-time operation you can update your 1200 records and set the new field value using Execute Anonymous. This can be done through the Developer Console or via the Eclipse IDE.

System.Savepoint sp1 = Database.setSavepoint();
try {
    // query the original data
    List<YourThing__c> originalThings = [SELECT Id
                                            FROM YourThing__c 
                                            WHERE YourNewField__c = null];

    // create a list to hold the records being updated
    List<YourThing__c> thingsToUpdate = new List<YourThing__c>();

    for (YourThing__c thing : originalThings) {
        // set the new value of the new field as required
        thingsToUpdate.add(new YourThing__c(Id = thing.Id, YourNewField__c = 'yourNewValue'));
    }
    // perform the DML operation
    update thingsToUpdate;

} catch (Exception ex) {
    // if there was a problem with the DML operation, roll it back
    Database.rollback(sp1);
}
share|improve this answer
    
Thanks so much for explaining this. Anonymous execution will help me a lot in future. For now I had to create a schedulable class. Upvoted answer. –  Richard N Jan 2 '13 at 22:02
add comment

If this needs to happen on demand, you can set up a Visualforce page that does nothing but call your method. This could work one of two ways:

  • From a button on the standard page layout, call a Visualforce page that uses the action attribute in the page tag to run your process then redirect back to the page layout.
  • Create a VF page with one commandbutton on it, that runs your process when you click the button.

Either way is straightforward; you need a small controller or controller extension class to enable this.

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.