+ Start a Discussion
Matthew Harris 40Matthew Harris 40 

How can you call the results of a method within another method?

I'm making a contact search page. The search criteria are Name, Phone Number and Email. I'm trying to attempt the following logic.
  • SearchContacts is pressed
  • Checks for Blank Fields
  • Validate input(Name,Phone,Email)
  • Search Contacts for matches
  • Generate Contact table with results
My current problem is that I have 3 Input Validation methods and not sure how to call their results into my "Search Contacts" function. Could someone provide me a solution on how to do this?


Here are the validation functions: 

/* Name Input Validation Regex*/
   public static Boolean ValidationName (String name)
            {
                Boolean NameIsValid = false;
                
              
                String nameRegex = '/^[a-zA-Z][a-zA-Z0-9]*(_[a-zA-Z0-9]+)*(__[cC])?$/';
                Pattern PatternName = Pattern.compile(nameRegex);
                Matcher nameMatcher = PatternName.matcher(name);
                
                if (!nameMatcher.matches()) 
                    
                NameIsValid = true;
                return NameIsValid; 
            }
    
     
    /* Phone Input Validation Regex*/
    public static Boolean ValidationPhone (String phone)
            {
                Boolean PhoneIsValid = false;
                
               
                String phoneRegex = '//^\\(?([0-9]{3})\\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$//';
                Pattern PatternPhone = Pattern.compile(phoneRegex);
                Matcher phoneMatcher = PatternPhone.matcher(phone);
                
                if (!phoneMatcher.matches()) 
                   
                PhoneIsValid = true;
                return PhoneIsValid;        
            }          
    
   
     /* Email Input Validation Regex*/
     public static Boolean ValidationEmail (String email)
            {
                Boolean EmailIsValid = false;
                
               
                String emailRegex = '/^\\w+([\\.-]?\\w+)*@\\w+([\\.-]?\\w+ (\\.\\w{2,3})+$//';
                Pattern PatternEmail = Pattern.compile(emailRegex);
                Matcher emailMatcher = PatternEmail.matcher(email);
                
                if (!emailMatcher.matches()) 
                    
                EmailIsValid = true;
                return EmailIsValid;    
            } 


And here is the method I want to call those results into
public PageReference searchContacts()
        {
                           
            
            
              /* Checks if input fields are empty*/
            if (name == '' || phone == '' || email == '')
                {
                    Error_FieldsEmpty = true;   
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'One or more of your fields are blank. Please enter your information in the remainding fields to proceed'));
                }         
            else 
            {
                /* Check Validations*/
                
                
                
                
            }
Best Answer chosen by Matthew Harris 40
David Zhu 🔥David Zhu 🔥
Would the following code work for you?

public PageReference searchContacts()
        {
             /* Checks if input fields are empty*/
            if (name == '' || phone == '' || email == '')
                {
                    Error_FieldsEmpty = true;   
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'One or more of your fields are blank. Please enter your information in the remainding fields to proceed'));
                    return null;
                }         
            else 
            {
                /* Check Validations*/
                Boolean isValid = true;
                if (ValidationName(name)) 
                 {
                      isValid  = false;
                       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Name is invalid. Please put a valid name'));
                  }
                if (ValidationPhone(Phone)) 
                 {
                      isValid  = false;
                       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Phone is invalid. Please put a valid phone'));
                  }
                if (ValidationPhone(email)) 
                 {
                      isValid  = false;
                       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Name is email. Please put a valid email'));
                  }
                
                  if (!isValid) 
                  {
                        return null;
                  }
       
                   // other processes
            }

All Answers

David Zhu 🔥David Zhu 🔥
Would the following code work for you?

public PageReference searchContacts()
        {
             /* Checks if input fields are empty*/
            if (name == '' || phone == '' || email == '')
                {
                    Error_FieldsEmpty = true;   
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'One or more of your fields are blank. Please enter your information in the remainding fields to proceed'));
                    return null;
                }         
            else 
            {
                /* Check Validations*/
                Boolean isValid = true;
                if (ValidationName(name)) 
                 {
                      isValid  = false;
                       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Name is invalid. Please put a valid name'));
                  }
                if (ValidationPhone(Phone)) 
                 {
                      isValid  = false;
                       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Phone is invalid. Please put a valid phone'));
                  }
                if (ValidationPhone(email)) 
                 {
                      isValid  = false;
                       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Name is email. Please put a valid email'));
                  }
                
                  if (!isValid) 
                  {
                        return null;
                  }
       
                   // other processes
            }
This was selected as the best answer
Matthew Harris 40Matthew Harris 40
I want to call the results of ValidationName,ValidationPhone and ValidationEmail in my searchContacts() method. Each one returns a boolean. I need those booleans to check if all validations are in place as a prerequisite to the query using that data. 
David Zhu 🔥David Zhu 🔥
The code snippet does what you want.