+ Start a Discussion
ChermaduraiChermadurai 

Schedule Jobs

Hello,

 

Here is my code how can i schedule this monthly.. Please help me..

 

 

 

global class FBR_InquiryMonthly{

 

    public  static void ValAdd(Set<Id> InquiryId)
  { 
 
List<FBR_Inquiry__c> Inquiry= [SELECT KPIInquiryContent__c,KPIInquiryContent__r.Account__c,CommodityName__c,ModifyDate__c,Status__c from FBR_Inquiry__c  where Id =:InquiryId];
{
FBR_Inquiry__c FBR= Inquiry[0];
Date startDate=FBR.ModifyDate__c.toStartOfMonth();
Date endDate=FBR.ModifyDate__c.toStartOfMonth().addMonths(1);

 

LIST<AggregateResult> countmonth=[SELECT count(KPIInquiryContent__c) coun from FBR_Inquiry__c where KPIInquiryContent__r.Account__c=:FBR.KPIInquiryContent__r.Account__c and ModifyDate__c >= :startDate AND ModifyDate__c < :endDate and PayoffObject__c=:'○' and Status__c IN('ftyti ,'vinjk')];
 
LIST<AggregateResult> calmonth=[Select count(ImportCount__c) icount from WorkResultsMonth__c where WorkDate__c >= :startDate and WorkDate__c < :endDate and Account_Name__c=:FBR.KPIInquiryContent__r.Account__c];

 

 

List<KPIAccident__c> acc= [SELECT Date__c,MonthlyGeneratedCount__c,MonthlyPPM__c from KPIAccident__c where  Account_Name__c=:FBR.KPIInquiryContent__r.Account__c and Date__c >= :startDate and Date__c < :endDate];    
if(!acc.isEmpty() )
      {
         
          KPIAccident__c accupdate = acc[0];        
          Integer totalcountmonth=Integer.Valueof(countmonth[0].get('coun'));
         
          accupdate.MonthlyGeneratedCount__c=totalcountmonth;  
        
          Integer totalmonthwork= Integer.Valueof(calmonth[0].get('icount'));
          accupdate.MonthlyPPM__c=((totalmonthwork/totalcountmonth)*1000000);
          update accupdate;
      }else
      {
         KPIAccident__c kpi = new KPIAccident__c();
         List<KPIAccident__c> ToUpdate = new List<KPIAccident__c>();
         kpi.Date__c=FBR.ModifyDate__c;        
          kpi.Account__c=FBR.KPIInquiryContent__r.Account__c;        
         kpi.itemname__c= FBR.CommodityName__c;      
         Integer totalcountmonths=Integer.Valueof(countmonth[0].get('coun'));
         kpi.MonthlyGeneratedCount__c=totalcountmonths;
         Integer totalmonthworks= Integer.Valueof(calmonth[0].get('icount'));
                   
         kpi.MonthlyPPM__c=((totalmonthworks/totalcountmonths)*1000000);
         ToUpdate.add(kpi);
         insert ToUpdate;

 

}
}
 }}

 

ChermaduraiChermadurai

Help me to schedule this class 1st day of every month..

TejTej

you have to write a schedulable class and schedule it trhough Schedule apex under develop--> apex classes.

 

Please refer to the following link for more info.: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_scheduler.htm

 

global class myclassSchedule implements Schedulable{

public static String CRON_EXP = '0 0 0 3 9 ? 2022'; 
global void execute( SchedulableContext SC) {
CronTrigger ct = [select id , 
CronExpression , 
TimesTriggered , 
NextFireTime 
from CronTrigger 
where id = :sc.getTriggerId()];

myclass cls= new myclass();
Database.executeBatch(cls);

}
}

 

neophyteneophyte

Create a class which implements schedulable interface

 

Example:

global class scheduledMerge implements Schedulable{
   global void execute(SchedulableContext SC) {
      mergeNumbers M = new mergeNumbers();
   }
}

 

1)After this go to develop -> apex classes

2)Click on Schedule Apex button

3)Here you can select your class and can schedule it to run every month.

 

- Anand