0

I'm running into some difficulty in getting a case milestone to update correctly. The context is that my Org had three departments, and three separate entitlement processes (which all completed on case closure). Now a new team is added, which completes when a case has been acknowleged (and therefore status has changed to 'In Progress').

Unfortunately for me, the use of Apex triggers on the case object has sent me into a roadblock - I can't seem to get the milestone to complete on the status updating to In Progress, because of the code for the other three departments, and when I modify the logical operator to be'&&' from '||', it works for the new team, but then completes two milestones for the other teams whenever a case changes status (from what I can understand, most likely due to processing the other 'if' statements).

if( Trigger.isUpdate )
    {
        if(Trigger.isAfter)
        {
            Set<Id> exptResolCases = new Set<Id>();  
            Set<Id> updateCasesId = new Set<Id>();  
            List<Case> caseTypeUpdate = new List<Case>();
            if (UserInfo.getUserType() == 'Standard'){
                DateTime completionDate = System.now(); 
                List<Id> updateCases = new List<Id>();
                for (Case c : Trigger.new){

                    if (((c.isClosed == true)||(c.Status == 'Closed'))&&((c.SlaStartDate<= completionDate)&&(c.SlaExitDate == null))){
                        updateCases.add(c.Id);    
                    }                                          
                    if(((c.isClosed != true)||(c.Status != 'Closed')||(c.Status != 'Draft')) && ((c.Area__c != Trigger.oldMap.get(c.Id).Area__c) ||
                     (c.Sub_Area__c != Trigger.oldMap.get(c.Id).Sub_Area__c) || (c.Cause__c != Trigger.oldMap.get(c.Id).Cause__c) )){
                        exptResolCases.add(c.Id);    
                    }
                    if((c.Status != Trigger.oldMap.get(c.Id).Status) && (Trigger.oldMap.get(c.Id).Status == 'In Progress')){
                        updateCasesId.add(c.Id);   
                    }

                }

The statement I've been trying to modify and add as a fourth IF statement is:

if (((c.isClosed != true)||((c.Recordtype.Name == 'New_Department')&&(c.Status == 'In Progress')))&&((c.SlaStartDate<= completionDate)&&(c.SlaExitDate == null))){
                        updateCases.add(c.Id);

However switching the '||' operator to && between the 'case closed not equal to true' and the 'record type name' is what breaks things, so I wanted to add this and isolate it without the trigger running through the other criteria and causing multiple milestones to complete if they're not of the New Department record type.

I would appreciate any and all ideas on how to work through this! Many thanks in advance.

  • N.B. c.isClosed == true should be written as c.isClosed as checkbox fields are booleans in Apex; You should also factor out all of these Trigger.oldMap.get(c.Id) into a variable oldcase at the top of the loop - makes reading code far easier – cropredy Feb 25 at 23:15
0

It'll be a lot easier if you use else to chain your logic:

if(cond1) {
  // do something here
} else if(cond2) {
  // do something else here
} else if(cond3) {
  // even something different here
}

This means that the code will stop at the first successful condition match, instead of attempting to check all the conditions. This should greatly simplify your logic.

Also, you can remove most of the redundant parentheses, which should make it easier to read.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.