3

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.

1
  • 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 Commented Jan 1, 2013 at 2:47

3 Answers 3

3

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.

7
  • 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
    Commented Dec 31, 2012 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/… Commented Dec 31, 2012 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. Commented Jan 1, 2013 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
    Commented Jan 1, 2013 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. Commented Jan 2, 2013 at 16:35
4

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);
}
1
  • 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
    Commented Jan 2, 2013 at 22:02
2

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .