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
RahulRahul 

I need to update the old opportunity records with stage as closelost if those record is with with stage prospecting since more than 90 days. i

The Idea is when i create a new opportunity and change the stage to Prospecting it should run this trigger and check if any old opportunities with stage Prospecting and which was created 90 days ago and convert that to closelost.
trigger UpdateRiskStatus1 on opportunity (after insert,after update) {
        
    List<opportunity> fvr = new List<opportunity>();
    
    for (opportunity  objvr :[select Id , stagename from opportunity where stagename='pending' and createddate < LAST_90_DAYS])
    {
        for(Opportunity op :trigger.new){
        
        if(op.stagename  == 'prospecting')
        {
        objvr.stagename='closed lost';
        fvr.add(objvr);
        }
        
        }
    }
   upsert fvr;

}
Best Answer chosen by Rahul
Raj VakatiRaj Vakati
Use this code.
trigger Opp on Opportunity (before insert) {
    
    List<opportunity>  objvrList  =[select Id , stagename from opportunity where stagename='prospecting' and createddate < LAST_90_DAYS ];
    boolean flagetoUpdate = false ; 
    for(Opportunity op :trigger.new){
        if(op.stagename  == 'prospecting')
        {
           flagetoUpdate = true ;   
        }
        
    }
    
    if(flagetoUpdate){
        for(opportunity o : objvrList) {
            o.stagename ='Closed Lost' ; 
        }
        update objvrList ;
    }
    
}


 

All Answers

Raj VakatiRaj Vakati
Use this code.
trigger Opp on Opportunity (before insert) {
    
    List<opportunity>  objvrList  =[select Id , stagename from opportunity where stagename='prospecting' and createddate < LAST_90_DAYS ];
    boolean flagetoUpdate = false ; 
    for(Opportunity op :trigger.new){
        if(op.stagename  == 'prospecting')
        {
           flagetoUpdate = true ;   
        }
        
    }
    
    if(flagetoUpdate){
        for(opportunity o : objvrList) {
            o.stagename ='Closed Lost' ; 
        }
        update objvrList ;
    }
    
}


 
This was selected as the best answer
David @ ConfigeroDavid @ Configero
Hi sumit,

First, I want to mention that the previous two answers have bugs in their code, and I do not recommend using their provided code.

Second,  while you can do what you are asking with a trigger, your logic is slightly flawed and you should not be waiting for an update event to trigger your logic for other unrelated records.

Instead, you should be performing this with a time delayed workflow which will trigger upon Stage = 'Prospecting', and execute an action 90 days after the trigger date to set the Stage to 'Closed Lost'.  This will give you the expected behavior you are looking for and I recommend it over the trigger you are trying to create.

Please mark this answer as correct if it helped you!
RahulRahul
Thank you Raj V , piyush_soni and David @ Configero for helping me.
David @ Configero your Answer is for future records but I needed it for old records also. The solution provided by Raj V is working Perfectly for Me. Thanks Once again Every one.