function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AbAb 

Need solution to execute trigger forcefully for all existing objects

Hello,

I have a Object 2 Objects like below, which has Master detail Relationship.

Object 1 (Master)
Field 1 (Number Type)

Object 2 (Detail)
Field 2 (Currency type)

I have a trigger which will calculate from Field 2(Field in Detail object) and put a calculated value on Field 1 (Of Master Object).

As this trigger is implemented now, 
How is it possibel to execute the trigger for the already exsisting Objects, which are not gonna be updated.

Thank you for suggestion !

Best Answer chosen by Ab
Antonio ManenteAntonio Manente
You could bulkify and move your logic into a utility class and invoke that from a batch process. This can perform your calculations/operations on all existing records. Here's an example of a batch class...
 
global class ExampleBatchProcess implements Database.Batchable<sObject>{
    
    global final String query;
    
    global ExampleBatchProcess(){
        query = 'SELECT Id FROM Account';
    } 
    
    global Database.queryLocator start(Database.BatchableContext bc){
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext bc, List<sObject> scope){
        // this will give you a set of Ids you can pass to your utility class
        Set<Id> resultIds = (new Map<Id,SObject>(scope)).keySet();
        
        YOUR_UTILITY_CLASS_HERE.UTILITY_CLASS_METHOD(resultIds);
    }
    
    global void finish(Database.BatchableContext bc){
        
    }
}

Realistically, you could just perform the logic on your "scope" list directly in the execute method. I always find it cleaner to invoke a utility class and perform the logic there. 

All Answers

Mathew Andresen 5Mathew Andresen 5
I don't believe it is.  The records passed into the the trigger are only the ones that are have a DML performed on them insert/update etc.

 
James WooleyJames Wooley
You would need to force a data update on all the records, so that they are forced into the trigger. Depending on the number of records you could use execute anonymous, or the dataloader would be better for larger volumes.

Alternatively, you calculate the value of the master field 1 and update the field directly via dataloader, without relying on the trigger.
Antonio ManenteAntonio Manente
You could bulkify and move your logic into a utility class and invoke that from a batch process. This can perform your calculations/operations on all existing records. Here's an example of a batch class...
 
global class ExampleBatchProcess implements Database.Batchable<sObject>{
    
    global final String query;
    
    global ExampleBatchProcess(){
        query = 'SELECT Id FROM Account';
    } 
    
    global Database.queryLocator start(Database.BatchableContext bc){
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext bc, List<sObject> scope){
        // this will give you a set of Ids you can pass to your utility class
        Set<Id> resultIds = (new Map<Id,SObject>(scope)).keySet();
        
        YOUR_UTILITY_CLASS_HERE.UTILITY_CLASS_METHOD(resultIds);
    }
    
    global void finish(Database.BatchableContext bc){
        
    }
}

Realistically, you could just perform the logic on your "scope" list directly in the execute method. I always find it cleaner to invoke a utility class and perform the logic there. 
This was selected as the best answer