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
SFDC n12SFDC n12 

SOQL Query help needed

Hi,


I am trying to write a script as a apex class which should check the following req for which i need help on it


1) I am having a custom object called as "Employee HR Hierarchy" where i have the datas populated for all the    fields to it

2) My req is i am having 2 fields like First name and supervisor name (manager's name) present n the employee HR Hierachy

I want to check wheather the supervisor name  present in the record is the manager for the first name present in the custom object record and this applies only  for "ACCOUNT EXECUTIVE" Profile records alone

Help me how to achieve it with the SOQL Query code please

Thanks in Advance
gautam_singhgautam_singh
Hi, 

Please look into the following Snippet of Code. Visualforce Page Takes Supervisor Name & First Name. Once clicked on ValidateManager() Method, the First Name & Manager Name is checked, if a match [a user with specific manager/supervisor] is found Success is delivered in the Debug Console. 

Remember, You need to handle multiple manager & multiple user outcomes. The FirstName of Manager may be of Many Users and same with the set of User's.

<!--
* 
* @version - 10-13-2014 1.0
* @author  - Gautam Singh
*               
-->

<apex:page controller="QueryCriteria">
  
  
  <apex:form >
 
  <apex:pageMessages />
  
      <apex:pageblock title="Query Criteria" >
      
          <apex:pageblockSection>
              <apex:inputField value="{!eHR.Name}"/>
              <apex:inputField value="{!eHR.First_Name__c}"/>
              <apex:inputField value="{!eHR.Supervisor_Name__c}"/>
          </apex:pageblockSection>
          <apex:commandButton value="Check Manager" action="{!ValidateManager}"/>
      
      </apex:pageblock>
  
  
  </apex:form>
  
</apex:page>
/*
* @DevForum__1_QueryCriteria    
* @Class   - to check whether the supervisor name  present in the record is
*            the manager for the first name present in the custom object record for Account Executive Profile
*
* @version - 10-13-2014 1.0
* @author  - Gautam Singh
*               
*/


public class QueryCriteria{

    public Employee_HR__c eHR {get;set;}
    
    public QueryCriteria(){
        
        eHR = new Employee_HR__c  ();
        
        //Profile myProfile = [SELECT Id, Name, Description FROM Profile WHERE Name = 'ACCOUNT EXECUTIVE'];
        
    }
    
    public void ValidateManager(){
    
        
        if(eHR.Supervisor_Name__c != null && eHR.First_Name__c != null){
        
            List<User> selectedUser = [Select Id,FirstName, ManagerID FROM User WHERE FirstName =: eHR.First_Name__c];
            List<User> managerUser = [Select Id,FirstName FROM User WHERE FirstName =: eHR.Supervisor_Name__c];
            
            if(!selectedUser.isEmpty()  && !managerUser.isEmpty() ){
                if(selectedUser[0].ManagerID == managerUser[0].Id){
                    system.debug('****Success Manager Matches****');
                }else{
                    system.debug('****Error Manager Not Match****' + '**Manager ID**' + selectedUser[0].ManagerID + '**Supervisor ID**' + managerUser[0].Id );
                }
                
            
            }else{
               ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Invaild User.');
               ApexPages.addMessage(myMsg);
               
            }
                
        
        }
    
    
    }
    


}


Important :

If this is what you were looking for then please mark it as a "SOLUTION" orYou can Click on the "Like" Button if this was beneficial for you.

Thank You