-1

My method is now returning null. However, I want to return a String saying that 'No value found'. My method is public static Map<Id,sObject>() type. Please refer to my code below:

public static Map<Id,Workday_Project_Hierarchy__c> getWorkdayHierarchies(Map<Id,Opportunity> WorkdayOppProject,Map<Id,String> Opp_ProjectTypeVal){
        Map<Id,String> workDayProjectHierarchyNames = new Map<Id,String>();
        Map<Opportunity,String> workdayProHierMap = new Map<Opportunity,String>();
        Map<Id,Workday_Project_Hierarchy__c> proHierarchyMap = new Map<Id,Workday_Project_Hierarchy__c>();
        List<String> Projecttypelst = new List<String> ();
        List<String> ProjectHierlst = new List<String> ();
        Map<Id,Workday_Project_Hierarchy__c> hierarchyList;
        Map<Id,Workday_Project_Hierarchy__c> Opp_ProjectHierFinalVal = new Map<Id,Workday_Project_Hierarchy__c> ();
        String ADS = 'Adaptive Insights';
        String DA = 'Delivery Assurance';
        String NonDANonAI = 'Non DA && Non AI';
        String PSA ='Planning Office Hours';
        String Greater = 'greater than or equals to';
        String Hire_Val = 'Null Values';
        List<String> WDHlst = new List <String> ();
        List<Workday_Project_Hierarchy__c> WorkProjHie_With_State = new List<Workday_Project_Hierarchy__c> ();
        List<Workday_Project_Hierarchy__c> WorkProjHie_Without_State = new List<Workday_Project_Hierarchy__c> ();
        if(!Opp_ProjectTypeVal.isEmpty())
            Projecttypelst.addall(Opp_ProjectTypeVal.values());
        
        System.debug('values of Projecttypelst' + Projecttypelst);
        if(!Projecttypelst.isEmpty())
            hierarchyList = new Map<Id,Workday_Project_Hierarchy__c>([SELECT Id,Name,Customer_Account_Region__c,Customer_Address__c,Employee_Count__c,Operator__c,Project_Type__c,
                                                                      PS_Region__c,PS_Sub_Region__c,Type__c,Workday_Services__c,State__c,Maximum_Employee_Count__c,Region__c,
                                                                      Minimum_Employee_Count__c
                                                                      From Workday_Project_Hierarchy__c /*Where Project_Type__c IN : Projecttypelst*/]);
        System.debug('values of hierarchyList' + hierarchyList);  
        
        if(!hierarchyList.isEmpty()){
            for(Workday_Project_Hierarchy__c w : hierarchyList.values()){
                if(w.State__c != null && w.State__c != '')
                WorkProjHie_With_State.add(w);
                else
                WorkProjHie_Without_State.add(w);
                 WDHlst.add(w.Project_Type__c);
            }
         
        system.debug('WorkProjHie_Without_State' + WorkProjHie_Without_State);
            //loop through all the Opp values to get the Map<OppId,ProjectHierarchy>.
        if(!WorkProjHie_With_State.isEmpty() || !WorkProjHie_Without_State.isEmpty())
       for(Opportunity o : WorkdayOppProject.values()){
                    
        if(o.Opportunity_PS_Region__c != null && o.Opportunity_PS_Region__c != '' && o.WD_Prime_Type__c == 'Planning' 
           && o.Opportunity_PS_Region__c != 'EMEA' && o.Opportunity_PS_Region__c != 'APJ'){
                           
              for(Workday_Project_Hierarchy__c wp0 : WorkProjHie_With_State){  
                  system.debug('Project type' + wp0.Project_Type__c);
                  system.debug( 'Opp Project type' + Opp_ProjectTypeVal.get(o.id).trim());
                               
                      List<String> w0 = new List<String> ();
                      String s0 = '';
                      s0 = wp0.Project_Type__c;
                      w0.addall(s0.split(';')); 
              if(w0.contains(Opp_ProjectTypeVal.get(o.id).trim())){       
                     system.debug('Contains check' + wp0.PS_Region__c + '>>> '+ o.Opportunity_PS_Region__c); 
                         if(wp0.Employee_Count__c != null && wp0.Operator__c == 'greater than or equals to' &&  o.APTS_Account_NumberOfEmployees__c >= wp0.Employee_Count__c
                            && (wp0.State__c != null || wp0.State__c != '') && o.Account_billing_state__c != null && o.Account_billing_state__c == wp0.State__c 
                            && (wp0.Workday_Services__c == '' || wp0.Workday_Services__c == null)){
                            system.debug('with state / greater than ');
                                          
                                 Opp_ProjectHierFinalVal.put(o.id,wp0);
                             }
                          if(wp0.Employee_Count__c != null && wp0.Operator__c == 'less than or equals to' &&  o.APTS_Account_NumberOfEmployees__c <= wp0.Employee_Count__c
                             && (wp0.State__c != null || wp0.State__c != '') && o.Account_billing_state__c != null && o.Account_billing_state__c == wp0.State__c 
                             && (wp0.Workday_Services__c == '' || wp0.Workday_Services__c == null)){
                                          
                                   Opp_ProjectHierFinalVal.put(o.id,wp0);
                             }
                           if(wp0.Employee_Count__c != null && wp0.Operator__c == 'between' && wp0.Employee_Count__c == null  && o.APTS_Account_NumberOfEmployees__c != null 
                              && o.APTS_Account_NumberOfEmployees__c < wp0.Maximum_Employee_Count__c 
                              && o.APTS_Account_NumberOfEmployees__c > wp0.Minimum_Employee_Count__c
                              && (wp0.State__c != null || wp0.State__c != '') && o.Account_billing_state__c == wp0.State__c && (wp0.Workday_Services__c == '' || wp0.Workday_Services__c == null)){
                                          
                                     Opp_ProjectHierFinalVal.put(o.id,wp0);
                              }                              
                          }       
                    }           
      } else if (o.Opportunity_PS_Region__c != null && o.Opportunity_PS_Region__c !='' 
                      /* && o.Opportunity_PS_Sub_Region__c !=null && o.Opportunity_PS_Sub_Region__c !=''*/){         
                                      
           for(Workday_Project_Hierarchy__c wp : WorkProjHie_Without_State){
               
              system.debug('Project type without' + wp.Project_Type__c);
              system.debug( 'Opp Project typewithout' + Opp_ProjectTypeVal.get(o.id).trim());
                                          
                      List<String> w = new List<String> ();
                      String s = '';
                      s = wp.Project_Type__c;
                      w.addall(s.split(';'));
               system.debug('split project type-->' + w);
                                          
            if(w.contains(Opp_ProjectTypeVal.get(o.id).trim())){
                                              
               system.debug('Contains check without' + wp.PS_Region__c + '>>> '+ o.Opportunity_PS_Region__c);
       //check for the Opp deal type is planning and PS region not equal to EMEA and APJ to check the state values.
                                              
                if(wp.PS_Region__c != null && wp.PS_Region__c != ''){                                                  
                    if(wp.PS_Sub_Region__c == '' || wp.PS_Sub_Region__c == null){
                       System.debug('In Ps sub region Null');                               
                       if(o.Opportunity_PS_Region__c != null && o.Opportunity_PS_Region__c == wp.PS_Region__c.trim()
                           && wp.Employee_Count__c == null  && (wp.Workday_Services__c == '' || wp.Workday_Services__c == null)){
                                                             
                                  Opp_ProjectHierFinalVal.put(o.id,wp);
                         }
                       If(o.Opportunity_PS_Region__c != null && o.Opportunity_PS_Region__c == wp.PS_Region__c.trim()
                          && (wp.Workday_Services__c == '' || wp.Workday_Services__c == null)
                          &&  wp.Employee_Count__c != null && wp.Operator__c == 'greater than or equals to' 
                          &&  o.APTS_Account_NumberOfEmployees__c >= wp.Employee_Count__c){
                                                             
                                   Opp_ProjectHierFinalVal.put(o.id,wp);
                         }
                       If(o.Opportunity_PS_Region__c != null && o.Opportunity_PS_Region__c == wp.PS_Region__c.trim()
                          && (wp.Workday_Services__c == '' || wp.Workday_Services__c == null)
                          && wp.Employee_Count__c != null && wp.Operator__c == 'less than or equals to' 
                          &&  o.APTS_Account_NumberOfEmployees__c <= wp.Employee_Count__c){
                                                             
                                    Opp_ProjectHierFinalVal.put(o.id,wp);
                         }                                                      
                 }// Sub region null check
                If(wp.PS_Sub_Region__c != null && wp.PS_Sub_Region__c != ''){
                     System.debug('In PS sub region Not Null--> ');                                 
                   if(o.Opportunity_PS_Region__c.trim() == wp.PS_Region__c.trim() && 
                      o.Opportunity_PS_Sub_Region__c.trim() == wp.PS_Sub_Region__c){
                           system.debug('Ps region and PS sub region');                                  
                      if(wp.Employee_Count__c != null && wp.Operator__c == 'greater than or equals to'
                         &&  o.APTS_Account_NumberOfEmployees__c >= wp.Employee_Count__c
                         && (wp.Workday_Services__c == '' || wp.Workday_Services__c == null)){
                              system.debug('Emp count greater than :--');                                      
                              Opp_ProjectHierFinalVal.put(o.id,wp);  
                      }
                      if(wp.Employee_Count__c != null && wp.Operator__c == 'less than or equals to' && 
                        o.APTS_Account_NumberOfEmployees__c <= wp.Employee_Count__c && 
                       (wp.Workday_Services__c == '' || wp.Workday_Services__c == null)){
                                                                    
                              Opp_ProjectHierFinalVal.put(o.id,wp);                                            
                     }                                        
                     if(wp.Employee_Count__c == null && wp.Operator__c == 'between' && 
                        o.APTS_Account_NumberOfEmployees__c != null &&
                        o.APTS_Account_NumberOfEmployees__c < wp.Maximum_Employee_Count__c &&
                        o.APTS_Account_NumberOfEmployees__c > wp.Minimum_Employee_Count__c ){
                                                                    
                              Opp_ProjectHierFinalVal.put(o.id,wp);        
                    }                                                            
                    if(wp.Employee_Count__c == null && (wp.Operator__c == null || wp.Operator__c == '') &&
                      (wp.Workday_Services__c == '' || wp.Workday_Services__c == null)){
                                                                    
                             Opp_ProjectHierFinalVal.put(o.id,wp); 
                    }
                    if(wp.Employee_Count__c != null && wp.Operator__c == 'greater than or equals to' 
                       &&  o.APTS_Account_NumberOfEmployees__c >= wp.Employee_Count__c 
                       && wp.Workday_Services__c != null
                       &&  o.Workday_Services__c != null 
                       && o.Workday_Services__c == wp.Workday_Services__c){
                                                                    
                             Opp_ProjectHierFinalVal.put(o.id,wp);   
                   }                                                             
                   if(wp.Employee_Count__c != null && wp.Operator__c == 'less than or equals to'
                      &&  o.APTS_Account_NumberOfEmployees__c <= wp.Employee_Count__c
                      && wp.Workday_Services__c != null
                      && o.Workday_Services__c != null
                      && o.Workday_Services__c == wp.Workday_Services__c){   
                          
                              Opp_ProjectHierFinalVal.put(o.id,wp);
                    }                                     
                  } //Ps region and Ps sub region Not null
                                                      
               }// wp.PS_Sub_Region__c null check.(Not null)
            }// null check for wp.PS_Region__c.
          }//Project Type If ends here.
        }//WSP for loop end here  //WPH for without state 
      }//else loop if Opp is EMEA and APJ
                   else{
                        system.debug('the opportunity region is blank.');
                    }
                }//Opportunity For end ghere.
   // }//state with or without
       } //hierarchyList empty check.
        if(!Opp_ProjectHierFinalVal.isEmpty() || Opp_ProjectHierFinalVal!= null){
            system.debug('final Workday hierarchy Project record:' + Opp_ProjectHierFinalVal);
            return Opp_ProjectHierFinalVal;
        }
        
        return null; // **HERE I WANT TO RETURN A STRING MESSAGE SAYING THAT 'NO VALUE FOUND'**
 }//end of method. ```
2
  • Without changing the return type to Object and requiring the caller to check the type of the response you cannot do what you want to do. I would recommend against the Object return type approach and simply have the caller understand that a null return means "no values found".
    – Phil W
    Commented Nov 11, 2020 at 19:17
  • @PhilW yeah right. However, if I change the return type of the method, then it would have large implications in the code. My requirement is to show some message like 'No value found' if any of the if conditions are not met.
    – Kumar
    Commented Nov 11, 2020 at 19:29

1 Answer 1

0

That's not the correct way to handle this situation. If there's no results, simply return the empty map, and then the method calling this method can take appropriate action:

Map<Id, Workday_Project_Hierarchy__c> results = MyClass.getWorkdayHierarchies(WorkdayOppProject, Opp_ProjectTypeVal){
if(results.isEmpty()) {
  System.debug('No results found.');
}

I do not recommend signaling "no results" as a null value, as null indicates that there was "nothing," not "no results." This is the difference between checking a box to see if it's empty, and there not being a box at all to check. Nulls tend to complicate logic unnecessarily, so I recommend that you avoid them when possible. See articles like Say No To Null for an expanded explanation, which I agree with.

1
  • Thanks sfdcfox for the solution and detailed explanation. Really appreciate it.
    – Kumar
    Commented Nov 11, 2020 at 19:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .