function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Saurav nirwal 21Saurav nirwal 21 

Scheduler of opportunity

Create a Scheduler to mail to Opportunity Owner , if not modified for last 30 days.
KaranrajKaranraj
Saurav - You can able to get the opportunity record which is not modified more than 30 days in a batch apex class and send email to the owner in the batch class. Then using apex scheduler class you can able to run this batch daily to send email to the opportunity owner 

Batch Class
global class sendEmailToOpptyOwner implements Database.Batchable<Opportunity >{

   global Database.QueryLocator start(Database.BatchableContext BC){
     return Database.getQueryLocator('Select Id,ownerID from Opportunity where LastModifiedDate = LAST_N_DAYS:30);
   }

   global void execute(Database.BatchableContext BC, List<Opportunity > scope){
     for(Opportunity s : scope){
       //Write the logic to send email to the opportunity owner
     }
   }

   global void finish(Database.BatchableContext BC){
   }
}

Apex scheduler Class

Then write the Apex Scheduler class, then schedule this class using schedulable interface 
​global class scheduledBatchable implements Schedulable {
   global void execute(SchedulableContext sc) {
      sendEmailToOpptyOwner b = new sendEmailToOpptyOwner(); 
      database.executebatch(b);
   }
}
Follow the below steps to schedule the apex scheduler class
  1. From Setup, click Develop | Apex Classes and click Schedule Apex.
  2. Specify the name of a class that you want to schedule.
  3. Specify how often the Apex class is to run.
    • For Weekly—specify one or more days of the week the job is to run (such as Monday and Wednesday).
    • For Monthly—specify either the date the job is to run or the day (such as the second Saturday of every month.)
  4. Specify the start and end dates for the Apex scheduled class. If you specify a single day, the job only runs once.
  5. Specify a preferred start time. The exact time the job starts depends on service availability.
  6. Click Save.

Thanks,
Karanraj
Jigar.LakhaniJigar.Lakhani

Hello,

You can achive that requirement without any apex code. You need to create workflow rule with time dependent action.

Below are the steps with snapshot to create workflow rule.

Create --> Workflow & Approvals --> Workflow Rules --> Click on New button

1. Select "Opporutnity" object


User-added image

2. Create workflow rule with Entry Criteria and Rule Criteria

User-added image

Click on "Save & Next" button.

3. Click on "Add Time Trigger button" to the next screen.

User-added image

4. Select criteria for time trigger same as below snap and click on save

User-added image

5. Your wokflow rule is created, rule will display like below snap. Active that rule.

User-added image


To check that it is wokring or not, you can view those time trigger from below path
Setup --> Administration Setup --> Monitoring --> Time-Based Workflow

Thanks & Cheers,
Jigar(pateljb90@gmail.com)

Jigar.LakhaniJigar.Lakhani
Hello,

If you want to go with the workfow option, you need to do more changes related to define email alert in "Time dependent workflow actions" as well as need to update entry critria for workflow.
So please let me know if you want to use workflow then I'll provide more changes details.

Thanks & Cheers,
Jigar(pateljb90@gmail.com)
megha agarwal 31megha agarwal 31
can someone please answer this question.
megha agarwal 31megha agarwal 31
what will be the logic to send a email for this requirment :
Create a Scheduler to mail to Opportunity Owner, if not modified for last 30 days.