Search code examples
salesforceapex-codeapex

create a new Apex Class to update field with Active or inactive


This is my fist time with Apex so Im fumbling around. What I am trying to do is read a date field Contract_Expiration__c and then set pdate_active_status_text__c to null, active, or void.

Any help would be great. Thanks in advance.

public class update_active_status {
    public static String saveData(){
        for (Account a : [SELECT Id FROM Account WHERE CreatedDate > 2000-01-16T10:00:00-08:00]){
               if (a.Contract_Expiration__c == null ){ 
                   a.update_active_status_text__c = null;
                   update a;
               } else if (a.Contract_Expiration__c >= Date.today().addDays(60)) {
                a.update_active_status_text__c = 'Active';
                   update a;
               } else {
                a.update_active_status_text__c = 'Void';
                update a;
               }    
        }
     }
}

Solution

  • You need to add the fields to modify/check to your query in order for that method to work:

    [SELECT Id, Contract_Expiration__c, update_active_status_text__c FROM Account WHERE CreatedDate > 2000-01-16T10:00:00-08:00]
    

    Also that method needs to return a String (but the compiler should have complained about it already).

    And finally, the approach of selecting all accounts created from a given date does not seem nice on the long run.