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
Rakshith RamachandraRakshith Ramachandra 

Check if the opportunity has been inactive for a year using last modified date.

I'm trying to check if an opportunity has been inactive for a year. If yes, I'll send an email notification.

I'm using process builder to do this. But I'm not sure how to check if Last modified date was before 12 months from today. I'm able to come up with 
this Opportunity.LastModifiedDate < TODAY - DAYS(365)

Let me know what modifications needs to be done
User-added image
Best Answer chosen by Rakshith Ramachandra
suresh sanneboina 4suresh sanneboina 4
Need to create a Schedule class where it has to run every day and a batch class to run the query to check the records where lastmodifed <365 days. 

Please find the Below Code

Batch Class
global class BatchInactiveOpps implements Database.Batchable<sObject> {

    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        return Database.getQueryLocator([SELECT CloseDate,Description,Id,LastModifiedDate FROM Opportunity WHERE LastModifiedDate < LAST_N_DAYS:365]);
    }

    //execute method
    global void execute(Database.BatchableContext BC, List<Opportunity> scope)
    {
        for(Opportunity opp:scope){
			//logic to be implemented for the inactive records
			System.debug(opp.Name);
		}
    }
    
    //Finish method
    global void finish(Database.BatchableContext BC)
    {
    }
}

Schedulable Class
global class SchedulableOpportunities implements Schedulable
{
    global void execute(SchedulableContext sc)
    { 
		Id InactiveOpps=Database.executeBatch(new BatchInactiveOpps());
    }
}
To schedule the class you need to execute the below code in the developer console.
 
String sch='0 30 09 * * ?';
System.schedule('Check inactive Opps', sch, new SchedulableOpportunities());


 

All Answers

@Karanraj@Karanraj
Process builder is event based, those actions will be triggered only when there is an insert or update action. For your scenario, you have use batch apex class and apex scheduler. Write a batch class to send email for the records and schedule apex class the batch class to run daily at a particular time.

1. https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm
2. https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_scheduler.htm
suresh sanneboina 4suresh sanneboina 4
Need to create a Schedule class where it has to run every day and a batch class to run the query to check the records where lastmodifed <365 days. 

Please find the Below Code

Batch Class
global class BatchInactiveOpps implements Database.Batchable<sObject> {

    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        return Database.getQueryLocator([SELECT CloseDate,Description,Id,LastModifiedDate FROM Opportunity WHERE LastModifiedDate < LAST_N_DAYS:365]);
    }

    //execute method
    global void execute(Database.BatchableContext BC, List<Opportunity> scope)
    {
        for(Opportunity opp:scope){
			//logic to be implemented for the inactive records
			System.debug(opp.Name);
		}
    }
    
    //Finish method
    global void finish(Database.BatchableContext BC)
    {
    }
}

Schedulable Class
global class SchedulableOpportunities implements Schedulable
{
    global void execute(SchedulableContext sc)
    { 
		Id InactiveOpps=Database.executeBatch(new BatchInactiveOpps());
    }
}
To schedule the class you need to execute the below code in the developer console.
 
String sch='0 30 09 * * ?';
System.schedule('Check inactive Opps', sch, new SchedulableOpportunities());


 
This was selected as the best answer
Rakshith RamachandraRakshith Ramachandra
I figured out the part where I had to write apex classes and it's not possible using Process builder recently. However, I was struggling with the apex batch code. So thanks a ton you both. Much Appreciated. 
Rakshith RamachandraRakshith Ramachandra
Hi Suresh,

Can you please this part of the code?
String sch='0 30 09 * * ?';
System.schedule('Check inactive Opps', sch, new SchedulableOpportunities());

What modifications should I do if I want to schedule these check automatically everyday at noon (11:59 AM)?
suresh sanneboina 4suresh sanneboina 4
String sch='0 59 11 * * ?';