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 which opens a GMail window with certain recipients?

Hey,

I would like to define a button in the opportunity which opens a GMail window (Mailto). The recipient of this mail should be the primary contact of the opportunity. At the same time, all other contacts of the opportunity should get the mail as a carbon copy. It is important that this happens automatically.

How can I accomplish that?

Regards,
Matthias
Best Answer chosen by Matthias Kimmig
Matthias KimmigMatthias Kimmig
Thanks for your help.

I 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 a button. How can I implement it?
It is important that the parameters of the Apex class passed the link previously.

Regards,
Matthias

All Answers

Sonam_SFDCSonam_SFDC
One approach you can follow if you do not wish to make many changes/code much:
I believe Gmail buttons and links feature explained in the doc below can help get the mail directly open in Gmail with the email address of the primary contact -

The tricky part will be to have the email adress of the primary contact of the account on the opportunity (and the list of all contacts for that specific account) which you can accomplish using a custom email field.
 https://help.salesforce.com/HTViewHelpDoc?id=google_gmail_activate.htm&language=en_US#GmailButtonsLinks 
Vinit_KumarVinit_Kumar
You may want to check this :-

http://www.ebsta.com/

This allows a gmail to salesforce integration for Free..

Hope this helps !!
Matthias KimmigMatthias Kimmig
Thanks for your help.

I 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 a button. How can I implement it?
It is important that the parameters of the Apex class passed the link previously.

Regards,
Matthias

This was selected as the best answer