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
ksauceksauce 

Need help with an apex class

Hello!

This is a 2 part question.  I have a class that I created to move the Opportunity stage when a related case status moves.  Currently on the first part of the code works but not the rest of it.  I can get the first stage in the code to carry over but the rest of the stages do nothing.  Also, I would like to ignore this if the Opp stage is closed won.  

Here is the Trigger:
trigger updateOppStageNamefromCaseStatus on Case (after update)
{

    Case[] c = trigger.new;
     
     
     
     UpdateOppStageName.OpportunityRecord(c);


{
}
}


and here is the class I am using.

public with sharing class UpdateOppStageName
{  
    public static void OpportunityRecord(Case[] cases)
    {
        try
        {
        for (Case c: cases)
        {
            String sOppId = (String) c.Opportunity__c;
            List<Opportunity> listOpps = [SELECT Id, StageName FROM Opportunity WHERE Id = :sOppId limit 1];
           
            if(sOppId != '' && sOppId != null )
            {

                if (c.Status == 'Submitted')
                {                
                    for (Opportunity cOpp : listOpps) { cOpp.StageName = 'Qualified'; }
                    if (listOpps.isEmpty() == false){ update listOpps; } 
                }
                               
                if (c.Status == 'Estimate in Progress')
                {                
                    for (Opportunity cOpp : listOpps) { cOpp.StageName = 'Estimate in Progress'; }
                    if (listOpps.isEmpty() == false){ update listOpps; } 
                }
               
                if (c.Status == 'Information Needed from Client')
                {                
                    for (Opportunity cOpp : listOpps) { cOpp.StageName = 'Pending Client Response/Review'; }
                    if (listOpps.isEmpty() == false) { update listOpps; }  
                }
               
                if (c.Status == 'Work in Progress')
                {                
                    for (Opportunity cOpp : listOpps) { cOpp.StageName = 'Closed Won'; }
                    if (listOpps.isEmpty() == false) { update listOpps; }
                }   
              
                if (c.Status == 'Closed')
                {                
                    for (Opportunity cOpp : listOpps) { cOpp.StageName = 'Closed Declined'; }
                    if (listOpps.isEmpty() == false){ update listOpps; } 
                
                }
                  
              
                if (c.Status == 'Estimate Pending Client Approval')
                {                
                    for (Opportunity cOpp : listOpps) { cOpp.StageName = 'Estimate Pending Client Approval'; }
                    if (listOpps.isEmpty() == false){ update listOpps; } 
                
                }
               
               
                if (c.Status == 'Pending Client Acceptance')
                {                
                    for (Opportunity cOpp : listOpps) { cOpp.StageName = 'Closed Won'; }
                    if (listOpps.isEmpty() == false){ update listOpps; } 
                
                }
                if (c.Status == 'Closed Declined')
                {                
                    for (Opportunity cOpp : listOpps) { cOpp.StageName = 'Closed Declined'; }
                    if (listOpps.isEmpty() == false){ update listOpps; } 
                
                }
                /*
                if(c.Status == 'Closed (Declined)')
                {     
                    for (Opportunity cOpp : listOpps) { cOpp.Status = 'Closed (Declined)'; }
                    if (listOpps.isEmpty() == false) { update listOpps; }  
                }  */
               
            } // if(sCaseId != '' && sCaseId != null)
               
        } // for (Opportunity o: case)
        } // try
        catch(DmlException e)
        {
            System.debug('The following DML exception has occurred: ' + e.getMessage());
        }
        catch(Exception e)
        {
            System.debug('The following exception has occurred: ' + e.getMessage());
        }
        finally
        {
        }
       
} // public static void CaseRecord(Opportunity[] case)
}


Can anyone help?  I dont know what I am doing wrong?!

GlynAGlynA
@ksauce,

I'm not entirely sure why your code isn't behaving the way you expect, but I do see that it is not bulk-safe - you've got queries and DML inside of loops.  I've refactored the code to make it bulk-safe.  Hopefully, this will also fix your original problem.  Try this:

public with sharing class UpdateOppStageName
{
    public static void OpportunityRecord( List<Case> cases )
    {
        Set<Id> set_OpportunityIDs = new Set<Id>();

        //  figure out which Opportunity records we will need
        for ( Case theCase : cases )
        {
            if ( theCase.Opportunity__c == null ) continue;
            set_OpportunityIDs.add( theCase.Opportunity__c );
        }

        //  get the required Opportunity records
        Map<Id,Opportunity> map_Opportunities = new Map<Id,Opportunity>
        (   [   SELECT  Id, StageName
                FROM    Opportunity
                WHERE   Id IN :set_OpportunityIDs
            ]
        );

        //  create a map from Case Status to Opportunity StageName
        Map<String,String> map_Status_StageName = new Map<String,String>
        {
            'Submitted'                         =>  'Qualified',
            'Estimate in Progress'              =>  'Estimate in Progress',
            'Information Needed from Client'    =>  'Pending Client Response/Review',
            'Work in Progress'                  =>  'Closed Won',
            'Closed'                            =>  'Closed Declined',
            'Estimate Pending Client Approval'  =>  'Estimate Pending Client Approval',
            'Pending Client Acceptance'         =>  'Closed Won',
            'Closed Declined'                   =>  'Closed Declined'
        };

        List<Opportunity> list_OpportunitiesToUpdate = new List<Opportunity>();

        //  change the Opportunity StageName for those Opportunities whose Cases have
        //  a Status we care about and who are not already Closed Won
        for ( Case theCase : cases )
        {
            if ( !map_Status_StageName.containsKey( theCase.Status ) ) continue;

            Opportunity theOpportunity = map_Opportunities.get( theCase.Opportunity__c );

            if ( theOpportunity.StageName == 'Closed Won' ) continue;

            theOpportunity.StageName = map_Status_StageName.get( theCase.Status );
            list_OpportunitiesToUpdate.add( theOpportunity );
        }

        update list_OpportunitiesToUpdate;
    }
}

I removed the try/catch/finally blocks.  You can add them back in if you want.  I hope this helps.

Glyn Anderson
Sr Developer / Systems Analyst  |  ClosedWon  |  closedwon.com
Certified Developer | Certified Advanced Administrator

endowance1.3879012557122808E12endowance1.3879012557122808E12
Thank you very much for your reply!  However I am now getting an error when trying to execute the code.


updateOppStageNamefromCaseStatus: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 006c0000009iqBUAAY; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, updateCaseStatusfromOpptySalesStage: maximum trigger depth exceeded Case trigger event AfterUpdate for [500c0000002PNQy] Opportunity trigger event AfterUpdate for [006c0000009iqBU] Case trigger event AfterUpdate for [500c0000002PNQy] Opportunity trigger event AfterUpdate for [006c0000009iqBU] Case trigger event AfterUpdate for [500c0000002PNQy] Opportunity trigger event AfterUpdate for [006c0000009iqBU] Case trigger event AfterUpdate for [500c0000002PNQy] Opportunity trigger event AfterUpdate for [006c0000009iqBU] Case trigger event AfterUpdate for [500c0000002PNQy] Opportunity trigger event AfterUpdate for [006c0000009iqBU] Case trigger event AfterUpdate for [500c0000002PNQy] Opportunity trigger event AfterUpdate for [006c0000009iqBU] Case trigger event AfterUpdate for [500c0000002PNQy] Opportunity trigger event AfterUpdate for [006c0000009iqBU] Case trigger event AfterUpdate for [500c0000002PNQy] Opportunity trigger event AfterUpdate for [006c0000009iqBU]: []: Class.UpdateOppStageName.OpportunityRecord: line 51, column 1


Not sure why this would execute in this fashion as your code seems pretty spot on.  Maybe you can notice something that I do not.  Thanks and happy holidays!
endowance1.3879012557122808E12endowance1.3879012557122808E12
I just ran a few more tests and this seems to only error when the case status equals Estimate Pending Client Approval.
endowance1.3879012557122808E12endowance1.3879012557122808E12
Interesting, seems to work now!  I think it may be because I have a similar trigger on the Opp that controls the case stage.  Once I commented out the estimate in progress section on that class, this one seemed to work perfectly!
GlynAGlynA
@endowance...

I'm glad it worked out.  Sorry I didn't see your replies until today - the new site doesn't notify you when new replies are made...  If this has worked for you, could you mark it as a solution?  Thanks!

-Glyn