+ Start a Discussion
sushanth s 2sushanth s 2 

Anyone suggest to Right way to handle Execptions

Hi,

This my apex class  already developed the class with exception handling but i'm not sure this is right way to handle or not so please modift if any better way to handle

public class Helper {

    public static void updateCaseStage(List<Case> cases) {
    
             List<ID> cIds = new List<ID>();
        
             List<Case_User__c> cuser = New  List<Case_User__c>();
             Map<ID, Case_User__c> cusermap = new Map<ID, Case_User__c>();

             Set<String> skillSet = new Set<String>();
             List<String> skillSetList = new List<String>();
    
             for(Case c: cases){
        
                   if(c.Status == 'New') {
                   cIds.add(c.Id); //add case ids.
                   }     
                   skillSet.add(c.CaseRelatedTo__c);
             }   
           skillSetList.addAll(skillSet);    
        
         //available users               
           try{
                   List<Case_User__c> availableCaseUsers = [SELECT AvailableUsers__c, Timings__c, SkillSet__c  FROM Case_User__c where Availability__c = TRUE ORDER BY Last_Case_Allocated_Time__c ASC];
                 //List<Case_User__c> availableCaseUsers = Database.query(queryString);
                    integer i=0;
                    integer nomatchingSkillset = availableCaseUsers.size();
                    for(Case c: cases){
                        for(; i<=availableCaseUsers.size();i++){
                        if(availableCaseUsers.get(i).SkillSet__c.contains(c.CaseRelatedTo__c)){
                          c.OwnerId = availableCaseUsers.get(i).AvailableUsers__c;
                          availableCaseUsers.get(i).Last_Case_Allocated_Time__c = DateTime.now();
                          cusermap.put(availableCaseUsers.get(i).id, availableCaseUsers.get(i));
                         } else {
                              nomatchingSkillset--;
                              if(nomatchingSkillset == 0){
                                  //TODO assign to case queue
                                  
                                     User u =[SELECT Email FROM User WHERE Id IN (SELECT UserOrGroupId FROM GroupMember WHERE Group.DeveloperName ='CaseQueue')];
                                     String us = u.Email;
                                                          
                                      Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                                      String[] toAddresses = new String[] {us};
                                      mail.setToAddresses(toAddresses );
                                      mail.setSubject('No Users available to handle this case');
                                      mail.plainTextBody='handle this case';
                                      Messaging.SendEmail(new Messaging.SingleEmailMessage[] {mail});
                                                          
                                  break;
                              }
                              continue;
                        }
                        if(i>=availableCaseUsers.size()){
                            i=0;
                        }
                        break;
                     }      
                }
            }catch(System.DmlException e){
            
                 System.debug('Missing the required field in record');
                 
            }catch(Exception e){
            
                 System.debug('An Error Occured at:' +e.getMessage());
                 
            }finally{
            
                 System.debug('This is block just for closing the class');
            }      
          
 
     if(!cusermap.isEmpty()) {
      update cusermap.values();
     }

        
   }
}

anyone help me please
Thanks In Advance
Hemant_JainHemant_Jain
Have a look at below article. This might help.

http://sfdcsrini.blogspot.com/2014/04/what-is-exception-handling-in-apex.html
sushanth s 2sushanth s 2
Hemanth is that above class is fine ?
brahmaji tammanabrahmaji tammana

Hi

We write try-catch-finally blocks to handle exceptions.

This code seems to be OK but i think you can still handle it better.
There is one update statement in the end. You can also put it in try catch block to avoid any exception. This is my idea.

Thanks

Brahma

sushanth s 2sushanth s 2
thank you Brahma
Prithviraj_ChavanPrithviraj_Chavan
Hi
Your code seems great to me .. Good way to handle exceptions ..
there is 1 suggession form me
- whenever writing logic for list first we should compare like if( list.size() > 0 ) for un-necessary list has no rows to assign exception