+ Start a Discussion
Joshua HricJoshua Hric 

Creating an Apex Trigger to update all records on a custom object

I am relatively new to Apex, and I need help with an Apex Trigger that updates all records on a custom object on a daily basis.  The only criteria to not update the record is if "Status" picklist = "Contract Received".  I assume you can use a simple for loop to set the criteria, and any help or assistance on the topic is greatly appreciated.
Best Answer chosen by Joshua Hric
Andrew EchevarriaAndrew Echevarria
You'll have to create a class to carry out this process. For the purpose of this example, I used Object__c as the custom object and Status__c as the Status field assuming it is a field on a custom object and so it would be a custom field.

public class updateContracts {
    public static void checkUpdate(Object__c[] objects){
        for(Object__c obj: objects){
            if(obj.Status__c=='Contract Received'){
                continue;
            }
            else{
            // code to update record with
            }
        }
    }
}

Once you finish the class, you can go to Setup -> App Setup  ->Develop -> Apex Classes and click on the "Schedule Apex" button and set up this class to run on the days/times you'd like. 

Hope this helps and welcome to Apex!

All Answers

Vj@88Vj@88
You should go for Scheduled Apex for this, not Trigger. Chek out this link
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_scheduler.htm
 
Andrew EchevarriaAndrew Echevarria
You'll have to create a class to carry out this process. For the purpose of this example, I used Object__c as the custom object and Status__c as the Status field assuming it is a field on a custom object and so it would be a custom field.

public class updateContracts {
    public static void checkUpdate(Object__c[] objects){
        for(Object__c obj: objects){
            if(obj.Status__c=='Contract Received'){
                continue;
            }
            else{
            // code to update record with
            }
        }
    }
}

Once you finish the class, you can go to Setup -> App Setup  ->Develop -> Apex Classes and click on the "Schedule Apex" button and set up this class to run on the days/times you'd like. 

Hope this helps and welcome to Apex!
This was selected as the best answer
Andrew EchevarriaAndrew Echevarria
Also, you're using a class and not a trigger because a trigger requires a record to change in order for it to fire: thus a trigger. If you want a process to be carried out at your demand and not automatically when it is triggered by an action, it will have to be a class.