+ Start a Discussion
bhanu Prakash 310bhanu Prakash 310 

opportunity contact roles

Hi Guys,

I have designed class to send email to opprotunity contact roles. i need to write test class for below class
 
public class OppsendEmail {   
    
    public String subject {get; set;}
    public String body {get; set;}
    public blob attbody {get; set;}
    public String attname {get; set;}
    public String sendTo {get; set;}
    public list<string> toAddresses {get; set;}
    
   // public String Opportunity.Contact__c {get; set;}
    public String oppList {get; set;}
    public list<document> docList {get; set;}
    public List<Opportunity> Opp = new list<Opportunity>();
    public Map<Id, Opportunity> relatedOppMap = new Map<Id, Opportunity>();
    List<OpportunityContactRole> contactRoleArray = new List<OpportunityContactRole>();
    List<Contact> ContactList = new List<Contact>();
    public List<AttchCls> attchLst {set;get;}
    public String currentUserEmail {get; set;}
    // Custom Label
    public string customLabelValue{get;set;}   
    public String accountnumber { get; set; }
    public String accountid { get; set; }
    public Opportunity Opportu {get;set;}
    ApexPages.StandardController controller;
  
    
    /* Content */
    public string content { get; set; }
    public transient  ContentVersion contentRecord { get; set; } 
    
    // Constructor to get Opportunity data
    public OppsendEmail(ApexPages.StandardController controller){     
        
        contentRecord = [select id, Title, VersionData, PathOnClient, FileExtension from ContentVersion 
                         where ContentDocumentId =: system.Label.New_Project_Survey_ID LImit 1];
        
        content = contentRecord.Title;      
        this.controller = controller;
         Opportu = (Opportunity)controller.getRecord();
         system.debug('Opportu'+Opportu.Contact__c);

        
        
        
        opp = [Select OwnerId, (Select OpportunityId, Contact.Email From OpportunityContactRoles) From Opportunity 
                                where id in (Select OpportunityId From OpportunityContactRole where ContactId != '') 
                                AND Id =: ApexPages.currentPage().getParameters().get('id')];
        
        system.debug('ApexPages.currentPage().getParameters().get() >>>>>. Line 19'+ApexPages.currentPage().getParameters().get('id'));
        contactRoleArray =[select ContactID, Contact.Email, isPrimary, opportunityId from OpportunityContactRole Where isPrimary = true AND opportunityId =: ApexPages.currentPage().getParameters().get('id')];
        currentUserEmail = UserInfo.getUserEmail();
        body = 'Default body';
        subject = 'Default subject';
        system.debug('contactRoleArray >>>>> Line 18  '+contactRoleArray);
        system.debug('Emaillist >>>>>. Line 34'+sendTo);
        system.debug('currentUserEmail >>>>>. Line 28'+currentUserEmail );
        
    }
    
  
    
    public void PopulateAccNumber() {
     contactRoleArray =[select ContactID, Contact.Email, isPrimary  from OpportunityContactRole Where isPrimary = true AND opportunityId =: ApexPages.currentPage().getParameters().get('id')];
       
    }
    
    public PageReference uploadPDF(){
        
        String CurrentLoginUserId = UserInfo.getUserId();
        Opportu = (Opportunity)controller.getRecord();
        Id ContactID = Opportu.Contact__c;
        system.debug('Opportu'+Opportu.Contact__c+'   '+'ContactID   '+ContactID);        
        sendTo = [SELECT Id, Email, Name From Contact WHERE Id =: ContactID].Email;
        system.debug('sendTo Line 75 >>>'+sendTo);


        try {
            Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();  
            customLabelValue = System.Label.New_Project_Survey_ID;          
            transient ContentVersion  cv_list = [select id, Title, VersionData, PathOnClient, FileExtension from ContentVersion 
                                                    where ContentDocumentId = : customLabelValue LImit 1];
            string EamilAttTitle = cv_list.Title+'.'+cv_list.FileExtension;
            transient Blob b = cv_list.versionData;
            
            // Create the email attachment
            Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
            efa.setFileName(EamilAttTitle); 
            efa.setBody(b);
            email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
            email.setSubject(subject);
            String[] toAddresses = new String[]{SendTo};
            String[] currentUserEmails = new String[]{currentUserEmail};
            email.setToAddresses( toAddresses );
            email.setccAddresses(currentUserEmails);
            email.setPlainTextBody(body);
            
            String[] ccAddresses = new String[] {currentUserEmail};
            // Sends the email  
            Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
            
            List<Task> taskList = new List<Task>();
            for(Opportunity Op : opp){
                   Op.Survey_Sent_Date__c = Date.today(); 
                   Op.Survey_Status__c = 'Sent';
                   
                       Task newTask = new Task(
                       WhatId = op.Id,                    
                       OwnerId = CurrentLoginUserId ,                    
                       ActivityDate = Date.today(),
                       Subject = 'New Project Survey Sent',
                       Description = ' ',
                       WhoId = ContactID 
                       );
                taskList.add(newTask);
                 
            }                
            update opp;
            insert taskList;
            
         Id Opport = ApexPages.currentPage().getParameters().get('id');
         system.debug('Opport 95'+Opport);
         PageReference pgref = new PageReference('/' + Opport);
         pgref.setRedirect(true);
         return pgref;
        }
        catch(exception e){
            system.debug('Error:'+e);
            apexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,'e-'+e));
        }
        finally{
            
        }
        return null;
    }
    public class AttchCls{
        public boolean isSel {set;get;}
        public boolean ishd {set;get;} 
        public id cvId {set;get;}
        public Attachment att {set;get;}
    }
    
     
    
}
Started test class
 
@isTest
public class OppsendEmailTest{
    static testMethod void createAccount(){
        Account a = new Account();
        a.Name = 'Test Co.';
        a.BillingStreet = '4332 Holden Street';
        a.BillingCity = 'San Diego';
        a.BillingState = 'California';
        a.BillingPostalCode = '92101';
        a.BillingCountry = 'United States';
        a.Phone = '501-555-5555';
        a.Website = 'www.testco.com';
        insert a;
        System.debug('created account');
            
        //Then create a primary contact
        Contact c = new Contact();
        c.FirstName = 'Paul';
        c.LastName  = 'Test';
        c.AccountId = a.id;
        c.MailingStreet = '298 S. Ringo Street';
        c.MailingCity = 'San Diego';
        c.MailingState = 'California';
        c.MailingPostalCode = '92101';  
        c.MailingCountry = 'United States';       
        insert c;
        System.debug('created primary contact');
            
        //Then create another non-primary contact
        Contact ci = new Contact();
        ci.FirstName = 'Bob';
        ci.LastName  = 'Test';
        ci.AccountId = a.id;
        ci.MailingStreet = '298 S. Ringo Street';
        ci.MailingCity = 'San Diego';
        ci.MailingState = 'California';
        ci.MailingPostalCode = '92101'; 
        ci.MailingCountry = 'United States';
        insert ci;
        System.debug('created primary contact');
            
        //Now create an opportunity
        Opportunity o = new Opportunity();
        o.RecordType = [SELECT Id, Name, DeveloperName FROM RecordType WHERE Name = 'Airway Opportunity' LIMIT 1];
        o.Name = 'New Record';
        o.StageName = 'Posted';
        o.CloseDate = Date.today();
        o.Description = 'Test Record';
        insert o;
        System.debug('created opportunity');
            
        //Now update the OCR for the primary contact
        OpportunityContactRole ocr = new OpportunityContactRole();
        ocr.ContactId = c.Id;
        ocr.OpportunityId = o.Id;
        ocr.IsPrimary = TRUE;
        ocr.Role = 'Decision Maker';
        insert ocr;
        System.debug('created opportunity contact role for primary');
            
        //Now update the OCR for the non-primary contact
        OpportunityContactRole ocr1 = new OpportunityContactRole();
            
        ocr1.ContactId = ci.Id;
        ocr1.OpportunityId = o.Id;
        ocr1.IsPrimary = FALSE;
        ocr1.Role = 'Decision Maker';
        insert ocr1;
        System.debug('created opportunity contact role for non-primary contact');
        
        Update o;           
    }
}

iam unable to pass data into test class . Help me to design test class
 
Rajesh3699Rajesh3699
Hi ,

 PageReference pageRef = Page.YourPageName;
 Test.setCurrentPage(pageRef);
 pageRef.getParameters().put('Id', String.valueOf(O.Id));

O.Id is Opportunity Record Id 

https://salesforce.stackexchange.com/questions/102740/how-to-write-test-class-for-standard-controller-along-with-extensions?noredirect=1&lq=1

Try the above and let me know if you need any more help,

Thank You,
Rajesh Adiga P.
bhanu Prakash 310bhanu Prakash 310
Hi Rajesh,

I have tried it but code coverage is not running
 
PageReference pageRef = Page.SendEmailVFpage;
 Test.setCurrentPage(pageRef);
 pageRef.getParameters().put('Id', String.valueOf(opp.Id));
code coverage is not improved
 
Rajesh3699Rajesh3699
paste your updated test class and your VF page name 
bhanu Prakash 310bhanu Prakash 310
Hi Rajesh
Test class
@isTest
public class OppsendEmailTest{
    static testMethod void createAccount(){
        Account a = new Account();
        a.Name = 'Test Co.';
        a.BillingStreet = '4332 Holden Street';
        a.BillingCity = 'San Diego';
        a.BillingState = 'California';
        a.BillingPostalCode = '92101';
        a.BillingCountry = 'United States';
        a.Phone = '501-555-5555';
        a.Website = 'www.testco.com';
        insert a;
        System.debug('created account');
            
        //Then create a primary contact
        Contact c = new Contact();
        c.FirstName = 'Paul';
        c.LastName  = 'Test';
        c.AccountId = a.id;
        c.MailingStreet = '298 S. Ringo Street';
        c.MailingCity = 'San Diego';
        c.MailingState = 'California';
        c.MailingPostalCode = '92101';  
        c.MailingCountry = 'United States';       
        insert c;
        System.debug('created primary contact');
            
        //Then create another non-primary contact
        Contact ci = new Contact();
        ci.FirstName = 'Bob';
        ci.LastName  = 'Test';
        ci.AccountId = a.id;
        ci.MailingStreet = '298 S. Ringo Street';
        ci.MailingCity = 'San Diego';
        ci.MailingState = 'California';
        ci.MailingPostalCode = '92101'; 
        ci.MailingCountry = 'United States';
        insert ci;
        System.debug('created primary contact');
            
        //Now create an opportunity
        Opportunity o = new Opportunity();
        o.RecordType = [SELECT Id, Name, DeveloperName FROM RecordType WHERE Name = 'Airway Opportunity' LIMIT 1];
        o.Name = 'New Record';
        o.StageName = 'Posted';
        o.CloseDate = Date.today();
        o.Description = 'Test Record';
        insert o;
        System.debug('created opportunity');
            
        //Now update the OCR for the primary contact
        OpportunityContactRole ocr = new OpportunityContactRole();
        ocr.ContactId = c.Id;
        ocr.OpportunityId = o.Id;
        ocr.IsPrimary = TRUE;
        ocr.Role = 'Decision Maker';
        insert ocr;
        System.debug('created opportunity contact role for primary');
            
        //Now update the OCR for the non-primary contact
        OpportunityContactRole ocr1 = new OpportunityContactRole();
            
        ocr1.ContactId = ci.Id;
        ocr1.OpportunityId = o.Id;
        ocr1.IsPrimary = FALSE;
        ocr1.Role = 'Decision Maker';
        insert ocr1;
        System.debug('created opportunity contact role for non-primary contact');
        
        Update o;     
        
        PageReference pageRef = Page.SendEmailFromOpp;
        Test.setCurrentPage(pageRef);
        pageRef.getParameters().put('Id', String.valueOf(o.Id));
        ApexPages.StandardController sc = new ApexPages.StandardController(o);
       // OppsendEmail testAccPlan = new OppsendEmail(sc);      
    }
}

and Visualforce page name SendEmailFromOpp. 
can you share your skype details. Plz
Rajesh3699Rajesh3699
if this issue is still present ping me at rajeshadiga27@gmail.com, [ hangout ]