1

i have the register function that returns void and from there i call a function that returns pageReference. i attached the register function to the command button in the view but it not redirect me to other page. only when the register function returns pageReference it works. how can i redirect from the other function? thanks.

public class registerController {
    public String userName {get;set;}
    public String fullName {get;set;}
    public String adress {get;set;}
    public String redirectUrl {get;set;}
    private String errorMessage;

    public registerController(){
        userName = '';
        adress = '';
        fullName = '';
        redirectUrl = '';
        errorMessage = '';
    }

    public void register (){
        try {
            //create new user  
            Customer__c newCustomer = new Customer__c(
                Name = fullName,
                userName__c = userName, 
                adress__c = adress);
            //insert the user to db
            insert newCustomer;
            // redirect the user to shop page
            redirectToShop();                   
        } catch(Exception e){
             errorMessage = 'The User Name You Typed Alrady Exist, Please Choose Different User Name';
             ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL, errorMessage));
        }
    }

    public PageReference redirectToShop(){
        redirectUrl = 'https://c.eu11.visual.force.com/apex/shop';
        PageReference retURL = new PageReference(redirectUrl);
        retURL.getparameters().put('Id',userName);
        retURL.setRedirect(true);
        return retURL;      
    }
}
  • public PageReference register(){ and return redirectToShop() – martin Jan 26 '17 at 10:38
  • i tryed that but i dont want to redirect if i get exception. i just want show message. that is why i made that function as void becouse it not showing me the message – Alex Koretzki Jan 26 '17 at 10:40
  • Right, if you get an exception, you'd have to return NULL; – martin Jan 26 '17 at 10:42
2

Change register() method return type to page reference, return PageReference/Null in try catch respectively

public PageReference register() {
    try {
        //create new user  

        //insert the user to db

        // redirect the user to shop page
        return redirectToShop();                   

    } catch(Exception e){
        errorMessage = 'Your error!';
        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL, 
            errorMessage));
        return null;
    }
}

public PageReference redirectToShop(){
    // NEVER hardcode URL
    // redirectUrl = 'https://c.eu11.visual.force.com/apex/shop';
    PageReference retURL = Page.shop;
    retURL.getparameters().put('Id',userName);
    retURL.setRedirect(true);
    return retURL;      
}

Never hardcode a URL anywhere, In Visualforce, better would be to use Page.PageName which returns a Pagereference

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.