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
Samuel Gonsalves 8Samuel Gonsalves 8 

How to change opportunity stage to closed lost

Hi All,

I need to update Opportunity stage to closed lost when all the quotes inside that opportunity is been updated as rejected.

Can any one help me out with this?

Thanks in advance.

Regards
Samuel Gonsalves.
Grazitti TeamGrazitti Team
Hi Samuel Gonsalves 8,

Please find the below code for your requirement,

trigger updateOpr on Quote (after update) {
    set<ID> oprIds = new set<ID>();
    List<Quote> qList = new List<Quote>();
    Map<Id , List<Quote>> abc = new Map<Id , List<Quote>>();
    List<Opportunity> opr2Update = new List<Opportunity>();
    for(Quote q : Trigger.new){
        oprIds.add(q.opportunityId); 
    }
    List<Opportunity> lst = [select Id, name , (select Id, name, Status from Quotes ) from Opportunity where Id in : oprIds];
    
    for(Opportunity  i : lst) {
        abc.put(i.Id, i.quotes);
    }
    for(Opportunity  i : lst){
        if(abc.get(i.Id).size() > 0){
            for(Quote q1  : abc.get(i.Id)){
                if(q1.status == 'Rejected'){
                    continue;
                }
                else {
                    break;
                }
               
            }
                opr2Update.add(i);
        }
    }
    for(Opportunity op : opr2Update){
        op.StageName = 'Closed Lost';
    }
    update opr2Update;

}

mark it best answer if you find it a solution,
Thanks & regards, 
grazitti Team.