2

We need to accomplish the following:

On the 1st day of each quarter, we would like to update all open opportunities that have "close date" in the past quarter and set the new "close date" to be the last day of this new quarter.

I'm new to SFDC, but from my research it seems triggers or Time-dependent workflow may not do this. Can we write and schedule a cron script to do this?

Any help will be greatly appreciated! Thanks!

1
  • 1
    Yes, you would likely want to look into writing a scheduled batch class for this. Triggers and Workflows depend on DML occurring. You want this to happen on the first day of the quarter regardless if the record has been changed. Scheduled Batch Apex will help you accomplish this – Chris Duncombe Jun 27 '15 at 12:03
1

Please Use this Class and run from Developer console: put this code on Anonymous window:

 sceduleUpdate obj = new sceduleUpdate();  
 obj.UpdateclosedDate();

==========================================

public class sceduleUpdate implements schedulable{

    public void UpdateclosedDate(){

        sceduleUpdate su = new SceduleUpdate();

        integer CurMonth = system.now().month();
        Integer CurYear = system.now().year();       

        dateTime sdt = Datetime.newInstance(1960, 2, 17);

        if(3>= curmonth && curmonth >=1){

            sdt = Datetime.newInstance(curyear, 4, 1);

        }else if(6>= curmonth && curmonth >=4){

                sdt = Datetime.newInstance(curyear, 7, 1);

        }else if(9>= curmonth && curmonth >=7){

                sdt = Datetime.newInstance(curyear, 10, 1);

        }else{

                sdt = Datetime.newInstance(curyear +1 , 1, 1);

        }


            string day = string.valueOf(sdt.day());
            String month = string.valueOf(sdt.month());
            String hour = string.valueOf(sdt.hour());
            String minute = string.valueOf(sdt.minute());
            String second = string.valueOf(sdt.second());
            String year = string.valueOf(sdt.year());  


        String JobName = 'UpdateclosedDate';
        String strSchedule = second +' ' + minute+' '+ hour +' '+ day+' '+month+' ?'+' '+ year;

            if([select id from CronJobDetail where name =:jobName].size() > 0){

                CronJobDetail sb = [select id from CronJobDetail where name =:jobName];
                CronTrigger Ct = [select id from CronTrigger where CronJobDetailId =:sb.id];
                system.abortJob(Ct.id);

            }

        System.schedule(JobName, strSchedule, su);

    }   

    public void execute(SchedulableContext sc){

         list<opportunity> opps = [select id, closeDate from opportunity where DeliveryInstallationStatus__c != 'completed' ];

     integer mnth = system.now().month();
     integer yr =   system.now().year();

     if(10>= mnth && mnth >=12){
        yr = yr+1;
        mnth =3;
     }


     Integer numberOfDays = Date.daysInMonth(yr, mnth);
     Date lastDayOfMonth = Date.newInstance(yr, mnth, numberOfDays);

     for(opportunity opp : opps){
        opp.closeDate = lastDayOfMonth;
     }
     update opps;

     sceduleUpdate obj = new sceduleUpdate();  
     obj.UpdateclosedDate();




    }


} 
4

To make this a fully automated process you can combine a scheduled apex class and a batch class.

The scheduled apex class will be executed on the first day of every quarter. Once executed, it will start a batch job that will find all open opportunities that have "close date" in the past quarter. For each of those opportunities the batch job will set the new "close date" to be the last day of this new quarter.

The batch job is important to ensure it can scale as the number of opportunities increases.

For a CronExpression, try something like:

  • 0 0 0 1 1/3 ? * - Runs on the first of every third month starting January
  • 0 0 0 1 JAN,APR,JUL,OCT ? * - Runs on the first of the specified months.

See also Apex Scheduler

0

Write a batch script which would automatically calculate the date of each quarter and update all the opportunities. Make sure there are no other triggers dependent on the field unless you need to run them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.