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
Akash v 6Akash v 6 

How to reuse the below code in with helper class

Hi Folks,

Probem Description -
I have two difrrent class and diffrent visualforce. Earch class has findaddress() method.. and in the method I am trying to find the address based on user input..check the below class with method.

Question -
How to put the findaddress() logic in the helper method so that I can call in the both class without reating the same code..

something like this
findaddress(string streeaddress, string city, string zip ){

//?

}



Class A:
public PageReference Findaddress(){ 
        myaddress request = new myaddress (); //another class for address parameter
        String strSplittedAddress;
        //assign data
        if(project!=null && myproject.Street_Address__c!=''){  //myproject is an object instance
            strSplittedAddress=myProject.Street_Address__c;
         
            if(strSplittedAddress.contains(' ')){
                integer spaceIndex= strSplittedAddress.indexof(' ');
              
                String strHno=strSplittedAddress.subString(0,spaceIndex);
                              String strStreet=strSplittedAddress.subString(spaceIndex+1,strSplittedAddress.length());
               
                request.houseNumber = new List<String>{strHno};
               
            } else{
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Please correct Number along with Street'));
            }
        }
        else{
         
            return null;
        }
        request.locality= new List<String>{myProject.City__c};
        request.province = new List<String>{myProject.State__c};
        request.postalCode = new List<String>{myProject.Zip__c};
        request.country = new List<String>{'United States'};

       //some other logic contiune 


Class B
 
public PageReference Findaddress(){ 
        myaddress request = new myaddress (); //another class for address parameter
        String strSplittedAddress;
        //assign data
        if(project!=null && anotherProject.Street_Address__c!=''){  //anotherProject is an object instance
            strSplittedAddress=anotherProject.Street_Address__c;
         
            if(strSplittedAddress.contains(' ')){
                integer spaceIndex= strSplittedAddress.indexof(' ');
              
                String strHno=strSplittedAddress.subString(0,spaceIndex);
                              String strStreet=strSplittedAddress.subString(spaceIndex+1,strSplittedAddress.length());
               
                request.houseNumber = new List<String>{strHno};
               
            } else{
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Please correct Number along with Street'));
            }
        }
        else{
         
            return null;
        }
        request.locality= new List<String>{anotherProject.City__c};
        request.province = new List<String>{anotherProject.State__c};
        request.postalCode = new List<String>{anotherProject.Zip__c};
        request.country = new List<String>{'United States'};

       //some other logic contiune



 
Suresh Dupada pdIISuresh Dupada pdII

public class AddressUtility{
    public static myaddress getAddress(myaddress request, Another_Project__c projectObj){
        if(request!=null && projectObj!=null){
            String strSplittedAddress = projectObj.Street_Address__c;
            if(String.isNotBlank(strSplittedAddress)){                
                list<String> addressData = strSplittedAddress.split(' ', 2);
                if(addressData!=null && addressData.size()>0){
                    String strHno;
                    String strStreet;
                    if(addressData.size() == 2){
                        //Yes strSplittedAddress contains House number along with additional street information
                        strHno = addressData.get(0);
                        strStreet = addressData.get(1);
                    }else{
                        //strSplittedAddress contains only House number and No additional street information
                        strHno = addressData.get(0);
                    }
                    request.houseNumber = new List<String>{strHno};
                    
                }
                request.locality= new List<String>{projectObj.City__c};
                request.province = new List<String>{projectObj.State__c};
                request.postalCode = new List<String>{projectObj.Zip__c};
                request.country = new List<String>{'United States'};
            }else{
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Please correct Number along with Street'));
            }
        }
        return request;
    }
}


Class A:

public PageReference Findaddress(){
    public PageReference Findaddress(){
        myaddress request = new myaddress ();
        request = AddressUtility.getAddress(request, myproject);
    }
}

Class B:

public PageReference Findaddress(){
    public PageReference Findaddress(){
        myaddress request = new myaddress ();
        request = AddressUtility.getAddress(request, anotherProject);
    }
}
Akash v 6Akash v 6
Hi Suresh,

Thanks for reply..in addresUtlity you are passing only one object instance not sure it will work for another object..could you plz check once again

--
Thanks