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
ruchika Nayyarruchika Nayyar 

Write a trigger on Account when Account is update check all opportunity inside the account. Update all Opportunities Stage to close lost if an opportunity created date is greater than 30 days from today and stage not equal to close won.

how to write this code help me out?/
Write a trigger on Account when Account is update check all opportunity inside the account. Update all Opportunities Stage to close lost if an opportunity created date is greater than 30 days from today and stage not equal to close won.
GauravGargGauravGarg
Hi Ruchika,

Please find below code:

Trigger Account_Trigger on Account(After Update){
    List<Opportunity> OpportunityList = new List<Opportunity>();
    Map<Id, Opportunity> opptyMap = new Map<Id, Opportunity>([select id, stageName, AccountId, createdDate from Opportunity where AccountId IN: trigger.newMap().keyset()]);
    DateTime Opportunity30Days = system.now()-30;
    for(Opportunity opp: opptyMap.values()){
        if(opp.createdDate < Opportunity30Days && opp.stageName != 'close won'){
            opp.stageName = 'close lost';
            opp.closeDate = system.today();
            OpportunityList.add(opp);
        }
    }
    if(OpportunityList != null && OpportunityList.size() >)
        insert OpportunityList;
}

Let me know if you need assistance on this.

Thanks,
Gaurav
Vishnu Vardhan Reddy VelamuriVishnu Vardhan Reddy Velamuri
How to write test class for that trigger?