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
Matthias KimmigMatthias Kimmig 

How can I define a Button that directly executes a link on a VisualForce page?

Hey,

I have created the following Apex class and the Visualforce page:

Apex-Class:

public class MailOppController{

    public List<OpportunityContactRole>cRole{get; set;}
    public List<OpportunityContactRole>pRole{get; set;}
    public String oppID;

    public MailOppController(ApexPages.StandardController controller){

        Opportunity currentobject = (Opportunity) controller.getRecord();
        oppID = currentobject.Id;
        
        cRole = [SELECT ContactId, OpportunityId, IsPrimary FROM OpportunityContactRole WHERE OpportunityId = :oppID AND IsPrimary=false];
		pRole = [SELECT ContactId, OpportunityId, IsPrimary FROM OpportunityContactRole WHERE OpportunityId = :oppID AND IsPrimary=true];
    }
    
    public String getCarbonCopy(){
        
        String carbonCopy;       
        
        for (Integer i=0; i<crole.size();i++) {
            
            Contact mail_temp = [SELECT Email FROM Contact WHERE Id = :cRole.get(i).ContactId];
            
            if(i==0){
                carbonCopy = mail_temp.Email;
            } else {
            	carbonCopy = carbonCopy +', '+ mail_temp.Email;
            }
    	}
        return carbonCopy;
    }
    
    public String getPrimary(){
        
        String primary;       
        
		Contact mail_temp = [SELECT Email FROM Contact WHERE Id = :pRole.get(0).ContactId];
        primary = mail_temp.Email;
        
        return primary;
    }
}

VisualForce Page
<apex:page standardController="Opportunity" extensions="MailOppController" tabStyle="Opportunity"> 
    <apex:outputLink value="https://mail.google.com/mail/u/0/?view=cm&fs=1&tf=1&source=mailto&to={!primary}&cc={!carbonCopy}" target="_blank" id="theLink">GMail</apex:outputLink>   
</apex:page>

The link in the Visualforce page should be immediately executed when the user clicks on the button. How can I implement it?
It is important that the parameters of the Apex class passed the link previously.

Regards,
Matthias
Best Answer chosen by Matthias Kimmig
logontokartiklogontokartik
I think I understand what you are looking for. Is the button on the Opportunity Detail Page?

I made few changes to the Controller and visualforce page. 


The controller has an action method sendEmail that is called from Visualforce Page's action. So as soon as page load it calls the method and redirects to the gmail. Basically you dont need getters anymore, but I left them (and calling from constructor). in case you are using for something else. 
public class MailOppController{

    public List<OpportunityContactRole>cRole{get; set;}
    public List<OpportunityContactRole>pRole{get; set;}
    public String oppID;
    
    private String primary;
	private String carbonCopy;
    
    public MailOppController(ApexPages.StandardController controller){

        Opportunity currentobject = (Opportunity) controller.getRecord();
        oppID = currentobject.Id;
        
        cRole = [SELECT ContactId, OpportunityId, IsPrimary FROM OpportunityContactRole WHERE OpportunityId = :oppID AND IsPrimary=false];
		pRole = [SELECT ContactId, OpportunityId, IsPrimary FROM OpportunityContactRole WHERE OpportunityId = :oppID AND IsPrimary=true];
    	
        getCarbonCopy();
        getPrimary();
    
    }
    
    public Pagereference sendEmail(){
        
        Pagereference emailPage;
        
        emailPage = new Pagereference('https://mail.google.com/mail/u/0/?view=cm&fs=1&tf=1&source=mailto&to='+primary+'&cc='+carbonCopy);
        
        return emailPage;
    }
    
    public String getCarbonCopy(){
        
        for (Integer i=0; i<crole.size();i++) {
            
            Contact mail_temp = [SELECT Email FROM Contact WHERE Id = :cRole.get(i).ContactId];
            
            if(i==0){
                carbonCopy = mail_temp.Email;
            } else {
            	carbonCopy = carbonCopy +', '+ mail_temp.Email;
            }
    	}
        return carbonCopy;
    }
    
    public String getPrimary(){
        
       
        
		Contact mail_temp = [SELECT Email FROM Contact WHERE Id = :pRole.get(0).ContactId];
        primary = mail_temp.Email;
        
        return primary;
    }
}

The visualforce page is much simplified now with just one <apex:page> tag

<apex:page standardController="Opportunity" extensions="MailOppController" tabStyle="Opportunity" action="{!sendEmail}"> 
    <!-- You dont need this anymore
    <apex:outputLink value="https://mail.google.com/mail/u/0/?view=cm&fs=1&tf=1&source=mailto&to={!primary}&cc={!carbonCopy}" target="_blank" id="theLink">GMail</apex:outputLink>   
    -->
</apex:page>

When you add this as standard button, please make sure you choose open in new window property.


Hope this helps.

Thank you


All Answers

logontokartiklogontokartik
I think I understand what you are looking for. Is the button on the Opportunity Detail Page?

I made few changes to the Controller and visualforce page. 


The controller has an action method sendEmail that is called from Visualforce Page's action. So as soon as page load it calls the method and redirects to the gmail. Basically you dont need getters anymore, but I left them (and calling from constructor). in case you are using for something else. 
public class MailOppController{

    public List<OpportunityContactRole>cRole{get; set;}
    public List<OpportunityContactRole>pRole{get; set;}
    public String oppID;
    
    private String primary;
	private String carbonCopy;
    
    public MailOppController(ApexPages.StandardController controller){

        Opportunity currentobject = (Opportunity) controller.getRecord();
        oppID = currentobject.Id;
        
        cRole = [SELECT ContactId, OpportunityId, IsPrimary FROM OpportunityContactRole WHERE OpportunityId = :oppID AND IsPrimary=false];
		pRole = [SELECT ContactId, OpportunityId, IsPrimary FROM OpportunityContactRole WHERE OpportunityId = :oppID AND IsPrimary=true];
    	
        getCarbonCopy();
        getPrimary();
    
    }
    
    public Pagereference sendEmail(){
        
        Pagereference emailPage;
        
        emailPage = new Pagereference('https://mail.google.com/mail/u/0/?view=cm&fs=1&tf=1&source=mailto&to='+primary+'&cc='+carbonCopy);
        
        return emailPage;
    }
    
    public String getCarbonCopy(){
        
        for (Integer i=0; i<crole.size();i++) {
            
            Contact mail_temp = [SELECT Email FROM Contact WHERE Id = :cRole.get(i).ContactId];
            
            if(i==0){
                carbonCopy = mail_temp.Email;
            } else {
            	carbonCopy = carbonCopy +', '+ mail_temp.Email;
            }
    	}
        return carbonCopy;
    }
    
    public String getPrimary(){
        
       
        
		Contact mail_temp = [SELECT Email FROM Contact WHERE Id = :pRole.get(0).ContactId];
        primary = mail_temp.Email;
        
        return primary;
    }
}

The visualforce page is much simplified now with just one <apex:page> tag

<apex:page standardController="Opportunity" extensions="MailOppController" tabStyle="Opportunity" action="{!sendEmail}"> 
    <!-- You dont need this anymore
    <apex:outputLink value="https://mail.google.com/mail/u/0/?view=cm&fs=1&tf=1&source=mailto&to={!primary}&cc={!carbonCopy}" target="_blank" id="theLink">GMail</apex:outputLink>   
    -->
</apex:page>

When you add this as standard button, please make sure you choose open in new window property.


Hope this helps.

Thank you


This was selected as the best answer
Matthias KimmigMatthias Kimmig
Hi,

thanks for your help. It seems to be in the right direction.
However, the e-mail addresses do not arrive in the form. There is "null" output. 

Do you have any idea what might to be the reason for this?

Regards,
Matthias
Matthias KimmigMatthias Kimmig
thx. It works!