+ Start a Discussion
Anto HotelbedsAnto Hotelbeds 

Calling a custom button from visualforce

Hi developers,

 

I am new into Salesforce but already got something! I created a custom view lead page and custom edit lead page.

 

Now I want to connect this two pages.

 

1)I created a custom button, that when the user will click on it, will redirect him to the edit visualforce page. The name of the button is "Edit_Fiscal_Info".

 

2) And here is my question: I want to add this button to the custom visualforce page of the lead, but I get lost in here.

 

Could someone please provide me with the necessary steps? I think i need to create a standard controller to add this button, but I dont know how to do it....

 

Thanks a lot.

 

Antonio

Best Answer chosen by Admin (Salesforce Developers) 
Anto HotelbedsAnto Hotelbeds

I really appreciate all your help!

 

I paste the final solution in case it might be useful for someone else.

 

Visualforce:

 

<apex:page standardController="Lead" showHeader="true" showChat="true" extensions="LeadExtension">

//Code//

<tr>
                            <td class="labelCol"><b>License </b></td>
                            <td class="dataCol col02">  {!Lead.License__c}</td>                            
                            <td class="labelCol"><b> License Number</b></td>
                            <td class="dataCol col02"> {!Lead.License_Number__c}</td>
                        </tr>
                    </table>
//Calling to the action in the class
                    <apex:commandButton action="{!EditFiscalInfo}" value="Edit fiscal Information" />
        </apex:pageBlock>
     </apex:form>

 

ApexClass:

 

public class LeadExtension {
	private Lead l1;
	
	 public LeadExtension(ApexPages.StandardController stdController) {
     	this.l1 = (Lead)stdController.getRecord();
     }
	
	public PageReference EditFiscalInfo()
	{
		PageReference pageRef= new PageReference('/apex/EditFiscalInfoLead?id='+l1.Id);
        pageRef.setredirect(true);
        return pageRef;
	}
}

 Apex class test:

 

@isTest
public class EditFiscalInfoTest {
	static testMethod void myUnitTest(){
		//Test converage for the myPage visualforce page
		PageReference pageRef = Page.CustomLead;
		Test.setCurrentPageReference(pageRef);
		Lead newLead = new Lead (Company='XYZ Organization',
								Commercial_brand__c='Hotelbeds',
								Status='Lead to be checked',
								Customer_Type__c='Tour Operator',
								Customer_Subtype__c='Specialist',
								Division__c='B2B Division',
								Commercial_Area__c='TO Business',
								LastName='tejado',
								Email='a@a.es');
		insert newLead;
		ApexPages.StandardController sc = new ApexPages.standardController(newLead);
		// create an instance of the controller
		LeadExtension myPageCon = new LeadExtension(sc);
		//try calling methods/properties of the controller in all possible scenarios
		// to get the best coverage.
		myPageCon.EditFiscalInfo();
		//sc.EditFiscalInfo();
	}
}

 

Hope this will help someone!

 

Thanks a lot!

 

Antonio

All Answers

yvk431yvk431

not possible, as since we have separate button components for visual force page we cannot cal the custom buttons.

 

--yvk

Navatar_DbSupNavatar_DbSup

Hi,

Try the below code snippet as reference:

Page

 

<apex:commandButton value=" Edit_Fiscal_Info " action="{!login}"/>

 

Controller

 

public PageReference login()

{

PageReference pageRef= new PageReference('/apex/EditLeadPage);

              pageRef.setredirect(true);

              return pageRef;

}

 

Note: EditLeadPage is the name of the Edit Lead VF page.

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

Anto HotelbedsAnto Hotelbeds

Not even with an extension?

 

Is not possible to create a custom button and add it instead of the standar edit in this part of the code?

 

<apex:form >
        <apex:pageBlock title="{!Lead.Fiscal_Name__c}" mode="Read">
            <table class="detailList" border="0" cellSpacing="0" cellPadding="0">
                        <tr>
                            <td class="labelCol"><b>Fiscal Address </b></td>
                            <td class="dataCol col02">  {!Lead.Street_Fiscal__c} <br></br>
                                                    {!Lead.City_Fiscal__c}, {!Lead.Post_Code_fiscal__c} <br></br>
                                                    {!Lead.Country_Fiscal__c}</td>
                        </tr>
                        <tr>
                            <td class="labelCol"><b>{!$Label.Fiscal_Number_1} </b></td>
                            <td class="dataCol col02">  {!Lead.Fiscal_Number_1__c}</td>                            
                            <td class="labelCol"><b> Fiscal Number 2</b></td>
                            <td class="dataCol col02"> {!Lead.Fiscal_Number_2__c}</td>
                        </tr>
                        <tr>
                            <td class="labelCol"><b>License </b></td>
                            <td class="dataCol col02">  {!Lead.License__c}</td>                            
                            <td class="labelCol"><b> License Number</b></td>
                            <td class="dataCol col02"> {!Lead.License_Number__c}</td>
                        </tr>
                    </table>
                    <apex:commandButton action="{!Edit}" value="Edit fiscal Information" />
        </apex:pageBlock>
     </apex:form>

  Instead of the line 

<apex:commandButton action="{!Edit}" value="Edit fiscal Information" />

I want to add my custom button for edit....

 

Thanks

Anto HotelbedsAnto Hotelbeds

Hi,

 

Thanks a lot for your idea but as I said I am new into visualforce and controllers and I am completely stucked. I tried to implement your idea but this is all the far I got:

 

I created and apex class:

 

public class LeadExtension 
{
	public PageReference EditFiscalInfo()
	{
		PageReference pageRef= new PageReference('/apex/EditFiscalInfo');
        pageRef.setredirect(true);
        return pageRef;
	}
}

 But I cant deploy it because I need to test it, and I dont know how to test it. I did the following test

 

@isTest
private class EditFiscalInfoTest {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test
     LeadExtension e1=new LeadExtension();
	//HOW TO CONTINUE????
    
    }
}

 But I dont know how to continue....

 

Could you please guide me a little bit more?

 

Reaally appreciate it. Thanks

 

Antonio

shra1_devshra1_dev
@isTest
private class EditFiscalInfoTest {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test
     LeadExtension e1=new LeadExtension();
	el.EditFiscalInfo();
    
    }
}

 

Thats It...!!!

 

Regards,
Shravan 

Anto HotelbedsAnto Hotelbeds

Hi,

 

Already got to deploy the class and its test as follows:

 

public class LeadExtension {
	public PageReference EditFiscalInfo()
	{
		PageReference pageRef= new PageReference('/apex/EditFiscalInfoLead');
        pageRef.setredirect(true);
        return pageRef;
	}
}
@isTest
private class EditFiscalInfoTest {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test
     LeadExtension e1=new LeadExtension();
     e1.EditFiscalInfo();
    }
}

 

Now, when I try to call the method from my visualforce page as follows:

 

<apex:page standardController="Lead" showHeader="true" showChat="true" extensions="LeadExtension">

//////MORE CODE/////////7

//PART WHERE I CALL THE FUNCTION IN THE CLASS//
<table>
//FIELDS
</table>
<apex:commandButton action="{!EditFiscalInfo()}" value="Edit fiscal Information" />
        </apex:pageBlock>
     </apex:form>

 And I keep getting de error 

Error

Error: Unknown constructor 'LeadExtension.LeadExtension(ApexPages.StandardController controller)'

 

How can I fix this?

 

Thanks a lot!

shra1_devshra1_dev

Here is the Mistake...!!!

 

Dont use brackets in the action.

 

<apex:commandButton action="{!EditFiscalInfo}" value="Edit fiscal Information" />
shra1_devshra1_dev

and also put this code in your class

 

public LeadExtension(ApexPages.standardController con){

 

}

 

 

as you are using StandardController="Lead" in Your page

 

Regards,

Shravan

Anto HotelbedsAnto Hotelbeds

I really appreciate all your help!

 

I paste the final solution in case it might be useful for someone else.

 

Visualforce:

 

<apex:page standardController="Lead" showHeader="true" showChat="true" extensions="LeadExtension">

//Code//

<tr>
                            <td class="labelCol"><b>License </b></td>
                            <td class="dataCol col02">  {!Lead.License__c}</td>                            
                            <td class="labelCol"><b> License Number</b></td>
                            <td class="dataCol col02"> {!Lead.License_Number__c}</td>
                        </tr>
                    </table>
//Calling to the action in the class
                    <apex:commandButton action="{!EditFiscalInfo}" value="Edit fiscal Information" />
        </apex:pageBlock>
     </apex:form>

 

ApexClass:

 

public class LeadExtension {
	private Lead l1;
	
	 public LeadExtension(ApexPages.StandardController stdController) {
     	this.l1 = (Lead)stdController.getRecord();
     }
	
	public PageReference EditFiscalInfo()
	{
		PageReference pageRef= new PageReference('/apex/EditFiscalInfoLead?id='+l1.Id);
        pageRef.setredirect(true);
        return pageRef;
	}
}

 Apex class test:

 

@isTest
public class EditFiscalInfoTest {
	static testMethod void myUnitTest(){
		//Test converage for the myPage visualforce page
		PageReference pageRef = Page.CustomLead;
		Test.setCurrentPageReference(pageRef);
		Lead newLead = new Lead (Company='XYZ Organization',
								Commercial_brand__c='Hotelbeds',
								Status='Lead to be checked',
								Customer_Type__c='Tour Operator',
								Customer_Subtype__c='Specialist',
								Division__c='B2B Division',
								Commercial_Area__c='TO Business',
								LastName='tejado',
								Email='a@a.es');
		insert newLead;
		ApexPages.StandardController sc = new ApexPages.standardController(newLead);
		// create an instance of the controller
		LeadExtension myPageCon = new LeadExtension(sc);
		//try calling methods/properties of the controller in all possible scenarios
		// to get the best coverage.
		myPageCon.EditFiscalInfo();
		//sc.EditFiscalInfo();
	}
}

 

Hope this will help someone!

 

Thanks a lot!

 

Antonio

This was selected as the best answer