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
Kumar Saurav 45Kumar Saurav 45 

need to return a string instead of null from my map method

My method is now returning null. However, I want to return a String saying that 'No value found' instead of null. 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. 










 
vishal-negandhivishal-negandhi

Hi Kumar, 

Since your return type is a map, that's not possible without changing the method return type. 

But the easier appraoch is to use the string "No Value Found" whenever you receive a null in the logic where you're calling this method.

Example:

// some code

Map<Id, Workday_Project_Hierarchy__c> captureResponse = yourclassname.getWorkdayHierarchies(parameters);

if(captureResponse == null){
   return 'NO VALUE FOUND';
}


It's just an example, you may not return that string or do something else with it.

 

Hope this helps.