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

Apex trigger on opportunity update


Hey Guys


I am new to Apex coding so please be gentle


I need some help


Basically i have a huge backlog in my pipeline report, i thought of a way that i can overcome this but i am not sure its its viable


What i want to achieve is that is if the opportunity stage are any of the following

Application Docs Out
Application docs in (Complete)
Sent to Risk
Approved by Risk


and there has been no activity ( last modified date in the last 6months from todays date) then change the stage to "Opportunity lost"


Is this easily achievable ?

If any one can supply some basic coding for one /two of the stages above it would be truly appreciated


Many thanks




For this you need to have a schedule class.

global class dailyCron implements Schedulable {

    global static String scheduleIt() {
        dailyCron cron = new dailyCron();
        return System.schedule('Daily Cron', CRON_EXP, cron);

    global void execute(SchedulableContext sc) {

        //Send CRB Emails
        opportunity [] opps = [SELECT Id FROM Opportunity WHERE StageName=='Qualified' OR StageName=='Proposal' AND LastModifiedDate<180];

        for(Opportunity op : opps){
            op.StageName = 'Opportunity Lost';  

        update opps;



Schedule this class by clicking schedule apex button .

There might be some typo errors please check once.


You can probably do this with Workflow Rules. Try that first.

Janet GuoJanet Guo

@sfdcfox: You can't do it with workflows because workflows are only fired when a record has been created or has been edited.


I agree with @anilbathula@ in that you will need an Apex class of some kind (a trigger won't do). However, I disagree about needing a scheduable class unless dmason wants to run the class more than once and on a schedule.


dmason, if you only want to clean out your backlog once, take what @anilbathula@ has written inside that execute method and put it in its own class. Then either call the class using Eclipse's "Anonymous Apex" or make a button that calls the class (you'll have to make a webservice method in the class) and press the button when needed.