Sunday, April 13, 2014

Auto populate Primary contact role Email Id as ‘To’ and Non Primary Contact Roles Emails as ‘CC’ List.

Hello,


Scenario  : Client want to send Emails to Primary contact role as well as non primary contact roles,also in the form of Auto populate Primary contact role Email Id as ‘To’ and Non Primary Contact Roles Emails as ‘CC’ List .


Below  are the steps to follow.

Steps to Create List Button:
1) Create List Button On Task Object. 
2) Content Source type should be URL.
3) Give Url :/apex/SupportSalesOptyEmailMgmtPage?eid={!Opportunity.Id}

Visualforce Page:
<apex:page standardController="opportunity" extensions="SupportSalesOptyEmailMgmtPage" action="{!populateEmailRecipients}">
</apex:page>
Apex Class:
/** * File Name: 
* Description : Auto populate Primary contact role Email Id as ‘To’ and Non Primary Contact Roles Emails as ‘CC’ List.
* */ 
public class SupportSalesOptyEmailMgmtUtil {

    Public List<OpportunityContactRole> oppcontactrole{get;set;}
    Public List<OpportunityContactRole> con{get;set;}
    Public ID opp{get;set;}

    public SupportSalesOptyEmailMgmtUtil(ApexPages.StandardController controller) {
        List<OpportunityContactRole> oppcontactrole = new List<OpportunityContactRole>();
        
    }
    
    public PageReference populateEmailRecipients() {
       // Getting current opportunity ID
        opp = ApexPages.currentPage().getParameters().get('eid')
      //Querying for Primary Contact Role
        con  = [select ContactId from OpportunityContactRole where IsPrimary=true and OpportunityId=:opp Limit 1] ;
     // Querying for non-Primary Contact Roles Emails
        oppcontactrole = [Select Contact.Email from OpportunityContactRole where IsPrimary=false and OpportunityId=:opp];
   // If there are more than One non primary contact roles ,those email should be an comma separated.       
   String EmailCCList = '';
        for(OpportunityContactRole  opprole : oppcontactrole){
            if (opprole.Contact.Email!=null && !opprole.Contact.Email.equals(''))
            if (EmailCCList.equals('')){
                EmailCCList = opprole.Contact.Email;
            } else {
                EmailCCList = EmailCCList + ',' + opprole.Contact.Email;
            }
        }
        String primaryContactId = '';
        if (con.size()>0){
            primaryContactId = con[0].ContactId;
        }
        PageReference p = new PageReference('/_ui/core/email/author/EmailAuthor?p2_lkid='+primaryContactId+'&p3_lkid='+opp+'&rtype=003&p4='+EmailCCList+'&retURL='+opp); 
        p.setRedirect(true);
        return p;
    }   
}

1 comments: