Find your content:

Search form

You are here

Time-based Trigger to update all records

 
Share

If I want to "touch" (save, update) all records once a morning in an object (let's use Account as the example, though it would more likely be in a Custom object), how would someone go about that? To my understanding, you can use a Workflow now (ENTERPRISE ED) to fire a Trigger. The main issue is the trigger itself. I wrote this up to see if it would do the trick (several revisions later). No go.

trigger touchAll_ACCOUNT on Account(before update, before insert) {

List<Account> updateList = new List<Account>();

for(Account accObj : [ SELECT Last_TouchAll__c FROM account WHERE Id != null ]  )
{
    accObj.Last_TouchAll__c = System.now();
    updateList.add(accObj);

    if(updateList.size() >= 199){
        update updateList;
        updateList.clear();
    }
}
if(updateList.size() > 0){ update updateList; }
}

The main issue is... this trigger cannot be running each time an Account is edited. It needs to run ONLY when time-based Workflow runs it. I purposely did not include any "trigger." references trying to avoid it running each time I touch an Account. Hrumph.

Ideas??


Attribution to: AMM

Possible Suggestion/Solution #1

If I've understood the question correctly, I think your case would be better suited to an Apex class (as opposed to Trigger) scheduled with a scheduled job, possibly also incorporating Batch Apex (depending on your volumes). See:

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_scheduler.htm

For documentation about implementing the Schedulable interface and this post here about batch apex:

How do I convert a method to Batch Apex?


Attribution to: Phil Hawthorn
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/3625

My Block Status

My Block Content