-2

I am new to Apex and I need help with scheduled apex job for field update.

The apex job should run every day during the last 5 days of each month. It should check if "Last Status Report__c" field (type: Date/time) has the current month. If the month of "Last Status Report" doesn't equal the current month then it should update "Status Indicator" field to "Red", otherwise the field should be "Green"

  • Object: Project__c
  • Field to be checked: Last_Status_Report__c
  • Condition: MONTH (DATEVALUE (Last_Status_Report__c)) != Month(Today())
  • Field to be updated: Status Indicator__c (Picklist values: Red, Green)
  • Running Time: Daily from 25th to the end of each month

I tried the the following code but it seems it isn't complete and contain mistakes:

global class MyJob implements Schedulable {
        public Status indicator {get;set;}
   public MyJob(Status indicator) {
    this.indicator = indicator;
   }
   public void execute(SchedulableContext ctx)  {


       if (Last_Status_Report__c.month() == Date.Today().month()) {
        indicator = Status.Green; // or likewise
    } else {
        indicator = Status.Red; // or likewise  }
   }   
}

MyJob m = new MyJob(Status_Indicator__c);
    String sch = '0 0 0 25-31 * ? ?'; 
    String jobID = system.schedule('Last 5 days in month', sch, m);

}
1
  • What specifically are you talking about when you say it seems it isn't complete and contain mistakes
    – dphil
    Sep 8, 2014 at 15:07

1 Answer 1

0

It seems like you may have been overcomplicating it a bit, what it sounds like you need to do is loop through the projects, and do your conditional inside that loop, where you update the project record and add it to a collection, which you can then update.

Is this what you are looking to do?

global class MyJob implements Schedulable {

   public void execute(SchedulableContext ctx)  {
    List<Project__c> projs = new List<Project__c>();

    for(Project__c p : [SELECT Last_Status_Report__c, Status_Indicator__c FROM Project__c]){
      if (p.Last_Status_Report__c.month() == Date.Today().month()) {
        p.Status_Indicator__c = Status.Green; // or likewise
      } else {
        p.Status_Indicator__c = Status.Red; // or likewise  }
      }
      projs.add(p);
    }

    update projs;
}

MyJob m = new MyJob(Status_Indicator__c);
    String sch = '0 0 0 25-31 * ? ?'; 
    String jobID = system.schedule('Last 5 days in month', sch, m);
}
3
  • Compile Error: Variable does not exist: Status.Green at line 8
    – Leeuw Dijk
    Sep 8, 2014 at 14:28
  • You should replace those two, with the strings for the picklist values you want. I.e 'Green' and 'Red', sorry, I missed that.
    – kcshafer
    Sep 8, 2014 at 14:37
  • thanks! I corrected it. Now there stays one more problem with line: MyJob m = new MyJob(Status_Indicator__c). Variable does not exist: Status_Indicator__c.
    – Leeuw Dijk
    Sep 8, 2014 at 15:16

You must log in to answer this question.

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