ads

Friday, 24 March 2017

Fetching Profile Id by Passing Profile Name From Utility Class





Utility Class:-


public class Utility{   
    
   private static Map<String, Id> profileIdByName = new Map<String, Id>();
    
   public static Id getProfileIdByName(String profileName) {
        
        if (!String.isBlank(profileName)) {
        
          if (profileIdByName.containsKey(profileName)) {
                return profileIdByName.get(profileName);
            }
         else{        
            for(Profile pfInstance:[SELECT Name,Id FROM profile
                                              WHERE Name='System Admin'
                                              OR Name='Read Only' 
                                              OR Name='Marketing User']){
                profileIdByName.put(pfInstance.Name,pfInstance.Id);            
            } 
            
            return profileIdByName.get(profileName);
          }               
       }
       
        return Null;
    } 
  }


Fetching Profile Id Using Profile Name by Map :-


if(ProfileId == Utility.getProfileIdByName('Marketing User')){

//Your Logic

}

No comments:

Post a Comment