+ Start a Discussion
Orkhan ShukurovOrkhan Shukurov 

scheduled apex job

I need a scheduled apex job for field update. I have the following code but it doesn't work. Any ideas about what could be the problem?


global class ReportStatusNotification implements Schedulable {

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

    for(Project__c p : [SELECT Last_Status_Report__c, Report_Status_Indicator__c FROM Project__c]){
      if (p.Last_Status_Report__c.month() == Date.Today().month()) {
        p.Report_Status_Indicator__c = 'Green';
      } else {
        p.Report_Status_Indicator__c = 'Red';
      }
      projs.add(p);
    }

    update projs;
}

ReportStatusNotification m = new ReportStatusNotification();
    String sch = '0 0 0 5-31 * ?';
    String jobID = system.schedule('ReportStatusNotification', sch, m);

}
Best Answer chosen by Orkhan Shukurov
Vatsal KothariVatsal Kothari
That means Last_Status_Report__c doesn't contains any value.

Updated code:
global class ReportStatusNotification implements Schedulable {

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

	for(Project__c p : [SELECT Last_Status_Report__c, Report_Status_Indicator__c FROM Project__c]){
		if(p.Last_Status_Report__c != null){
			if (p.Last_Status_Report__c.month() == System.Today().month()) {		
				p.Report_Status_Indicator__c = 'Green';
			} else {
				p.Report_Status_Indicator__c = 'Red';
			}
			projs.add(p);
		}
	}

    update projs;
}
If this solves your problem, kindly mark it as the best answer.

Thanks,
Vatsal

All Answers

Vatsal KothariVatsal Kothari
Hi,

Try replacing below line of code 
if (p.Last_Status_Report__c.month() == Date.Today().month()) {
with 
if (p.Last_Status_Report__c.month() == system.today().month()) {
If this solves your problem, kindly mark it as the best answer.

Thanks,
Vatsal
Orkhan ShukurovOrkhan Shukurov
@ Vatsal : I scheduled it but It failed : Reason: Attempt to de-reference a null object

What does it mean?
Vatsal KothariVatsal Kothari
That means Last_Status_Report__c doesn't contains any value.

Updated code:
global class ReportStatusNotification implements Schedulable {

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

	for(Project__c p : [SELECT Last_Status_Report__c, Report_Status_Indicator__c FROM Project__c]){
		if(p.Last_Status_Report__c != null){
			if (p.Last_Status_Report__c.month() == System.Today().month()) {		
				p.Report_Status_Indicator__c = 'Green';
			} else {
				p.Report_Status_Indicator__c = 'Red';
			}
			projs.add(p);
		}
	}

    update projs;
}
If this solves your problem, kindly mark it as the best answer.

Thanks,
Vatsal
This was selected as the best answer
Orkhan ShukurovOrkhan Shukurov
@ Vatsal : Thank you very much!!!!!!!!!!!!! :)