+ Start a Discussion
Ola BamideleOla Bamidele 

How to Link the Save button to a URL

Hi All,

How can i link my VF Save button to a URL so that when a record in saved in my form, the user is redirectd to "www.google.com". Ive tried looking on different forms but other situations is always different to mine. Thanks for your help!

his is a snippet of my code.




<apex:page standardController="Turndown__c" sidebar="false" showHeader="true" extensions="pagereference" >
    <apex:form >
           <apex:pageBlock title="Documented Turndowns" id="turndown_list">
            
             <apex:pageBlockSection columns="1" >
                    <!-- to remove the header, but keep the fields' help texts -->
                         <style type="text/css">
                             .bPageHeader {display: none;}
                             </style>
                    
                                            <!-- Inputfields --> 
                                <apex:inputfield value="{! Turndown__c.Account__c }"/>
                                <apex:inputfield value="{! Turndown__c.Phone_Number__c }"/>
                                <apex:inputfield value="{! Turndown__c.Region__c }"/>

              
                 
                   </apex:pageBlockSection>
                        <apex:pageBlockButtons >  
                  
                                        <apex:commandButton action="{!Save}" value="Save"  />
 
                        </apex:pageBlockButtons>       


             
            </apex:pageBlock>
    </apex:form>
</apex:page>            

     
             
                                            
Thanks again!             
 
Best Answer chosen by Ola Bamidele
Amol ChAmol Ch
Hi Ola,
Replace update turnd; by Insert turnd;

Updated code;
public with sharing class extencntrl{
	
	Turndown__c turnd;
    public extencntrl(ApexPages.StandardController controller) {
        this.turnd= (Turndown__c)controller.getRecord();
    }
    
    public pageReference save()
		{
			Insert turnd; // steps to save your record.
			Pagereference pgref = New PageReference('https://www.google.com');
			return pgref;
		}
}



 

All Answers

Neetu_BansalNeetu_Bansal
Hello Ola,

First of all you need to create a extension ( apex class ) for your page and in that you can create one method like:
public pageReference save()
{
    // Perform the steps to save your record.
    return new PageReference( 'https://www.google.com' );
}
and call this method in your vf page save method like action="{!save}"

Thanks,
Neetu
Ola BamideleOla Bamidele
Hi Neetu,

Thanks for your reply. I created an apex class called "pageReference" however when i add the Save and brackets to that line, an error occurs. (Invalid constructor name: save). 

Also, could you explain a bit more what you mean by Perform the steps to the save record? 

Again thanks very much!
Amol ChAmol Ch
Hi ola,

Pagereferece is a defined kayword in apex. you need to change your class name.

your extension will be like this.
public with sharing class Controllerclass {
	
	Turndown__c turnd;
    public Controllerclass(ApexPages.StandardController controller) {
        this.turnd= (Turndown__c)controller.getRecord();
    }
    
    public pageReference save()
		{
			update turnd; // steps to save your record.
			Pagereference pgref = New PageReference('https://www.google.com');
			return pgref;
		}
}
Ola BamideleOla Bamidele
Hi Amol,

Thanks for the information, i've now changed the class name to "RDT" and ive added an extension to the <apex:page>.with the new class name in my visualforce page.

However now on the VF page, its saying "Apex class  RDT does not exist." Did  i do something wrong because it shouldnt be saying that as i have created the apex class page successfully? 

Thanks very much for the help!
Amol ChAmol Ch
Hi Ola,

When you use the extensions="extencntrl" then you must need to create the apex class of Name extencntrl.

Please check below code.

VF Page:
<apex:page standardController="Turndown__c" sidebar="false" showHeader="true" extensions="extencntrl" >
    <apex:form>
           <apex:pageBlock title="Documented Turndowns" id="turndown_list">
                     <apex:pageBlockSection columns="1" >
                    <!-- to remove the header, but keep the fields' help texts -->
                         <style type="text/css">
                             .bPageHeader {display: none;}
                             </style>
                                           <!-- Inputfields --> 
                                <apex:inputfield value="{! Turndown__c.Account__c }"/>
                                <apex:inputfield value="{! Turndown__c.Phone_Number__c }"/>
                                <apex:inputfield value="{! Turndown__c.Region__c }"/>
                      </apex:pageBlockSection>
                        <apex:pageBlockButtons >  
                               <apex:commandButton action="{!Save}" value="Save"  />
                        </apex:pageBlockButtons>       
            </apex:pageBlock>
    </apex:form>
</apex:page>
Extension:
public with sharing class extencntrl{
	
	Turndown__c turnd;
    public extencntrl(ApexPages.StandardController controller) {
        this.turnd= (Turndown__c)controller.getRecord();
    }
    
    public pageReference save()
		{
			update turnd; // steps to save your record.
			Pagereference pgref = New PageReference('https://www.google.com');
			return pgref;
		}
}


 
Ola BamideleOla Bamidele
Hi Amol,

Thanks that makes sense, I made the changes. However, i'm getting this error after I click the save the button on the form:

"Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []
Error is in expression '{! Save}' in component <apex:commandButton> in page visualforceturndowntesting: Class.extencntrl.save: line 10, column 1
"

So can i ask what you mean by "steps to save record" in the apex page?  Because I know this is the problem why its not saving. 

Thanks very much!
Amol ChAmol Ch
Hi Ola,
Replace update turnd; by Insert turnd;

Updated code;
public with sharing class extencntrl{
	
	Turndown__c turnd;
    public extencntrl(ApexPages.StandardController controller) {
        this.turnd= (Turndown__c)controller.getRecord();
    }
    
    public pageReference save()
		{
			Insert turnd; // steps to save your record.
			Pagereference pgref = New PageReference('https://www.google.com');
			return pgref;
		}
}



 
This was selected as the best answer
Ola BamideleOla Bamidele
Hi Amol,

It worked like a charm! Thanks very much for your help, much appreciated! 

Thanks,
Ola