+ Start a Discussion
bhanu_prakashbhanu_prakash 

test class coverage issue on

Hi,
Iam trying to cover a class facing issue on code facing issue.
Error Message	System.QueryException: List has no rows for assignment to SObject
Stack Trace	Class.OppsendEmail.uploadPDF: line 77, column 1
Class.OppsendEmail_Test.setupTestData: line 106, column 1
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 OpportunityContactRole sendToContact {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;}
     public String currentUserEmail1 {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){     
        customLabelValue = System.Label.New_Project_Survey_ID; 
        
        
        New_Project_Survey_ID__c PId =  New_Project_Survey_ID__c.getInstance('New Project Survey ID');
     //   contentRecord = [select id, Title, VersionData from ContentVersion where ContentDocumentId =: PId.New_Project_Survey_ID__c  LImit 1];
        
        //content = contentRecord.Title;  
        content = 'Test Title';         
        
        // Opportu = new Opportunity();
        
         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')];
       // sendTo   = [SELECT Id, Email, Name From Contact WHERE id =: contactRoleArray[0].ContactID ].Email;
        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 PageReference uploadPDF(){
        
        String CurrentLoginUserId = UserInfo.getUserId();
        Opportu = (Opportunity)controller.getRecord();
        Id ContactID = Opportu.Contact__c;
        system.debug('Opportu'+Opportu.Contact__c+'   '+'ContactID   '+ContactID);           
      //  sendToContact =[select ContactID, Contact.Email, isPrimary  from OpportunityContactRole WHERE Id =: ContactID].Contact.id;
       sendToContact =[select ContactID, Contact.Email, isPrimary  from OpportunityContactRole WHERE Id =: ContactID];
        //system.debug('sendTo Line 75 >>>'+sendTo);


        try {
            //  EmailTemplate template = [SELECT Id FROM EmailTemplate WHERE Id = '00XC0000001kH4SMAU' LIMIT 1];
            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 SendTo = sendToContact.Contact.id;
            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();
                       newTask.WhatId = op.Id;
                    //   WhatId = CurrentLoginUserId ,
                       newTask.OwnerId = CurrentLoginUserId;
                    //   OwnerId = op.OwnerId,
                       newTask.ActivityDate = Date.today();
                       newTask.Subject = 'New Project Survey Sent';
                       newTask.Description = ' ';
                       newTask.WhoId = sendToContact.Contact.id;
                       newTask.RecordType = [SELECT Id, Name, DeveloperName FROM RecordType WHERE Name = 'Standard Task' LIMIT 1];
                       newTask.Status = 'In Progress';
                       newTask.Type  = 'Email';
                       newTask.Priority = 'Normal';
                       
                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;
    }
 
    
}

Test Class:
 
@isTest
private class OppsendEmail_Test{
    public string acc;

    @istest
   public  static void setupTestData(){
        test.startTest();    
        
        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'+ a);
        
        Contact c = new Contact();
        c.RecordType = [SELECT Id, Name, DeveloperName FROM RecordType WHERE Name = 'External Contact' LIMIT 1]; 
        c.Job_Level__c = 'Consultant';
        c.Job_Function__c = 'Marketing';
        c.FirstName = 'Paul';
        c.LastName  = 'Test';
        c.AccountId = a.id;
        c.Email='test@gmail.com';       
        insert c;
        
        Contact c1 = new Contact();
        c1.RecordType = [SELECT Id, Name, DeveloperName FROM RecordType WHERE Name = 'External Contact' LIMIT 1]; 
        c1.Job_Level__c = 'Consultant';
        c1.Job_Function__c = 'Marketing';
        c1.FirstName = 'Paul1';
        c1.LastName  = 'Test1';
        c1.AccountId = a.id;
        c1.Email='test1@gmail.com';
       
        insert c1;
        System.debug('created Contact'+ c);
        
        Opportunity opp = new Opportunity();
        opp.RecordType = [SELECT Id, Name, DeveloperName FROM RecordType WHERE Name = 'Catalent Clinical Services Opportunity' LIMIT 1]; 
        opp.Name = 'New Record';
        opp.StageName = 'Posted';
        opp.CloseDate = Date.today();
        opp.Description = 'Test Record';  
        opp.Contact__c = C.id;
        insert opp;
        System.debug('created opportunity'+ opp);
        
       
        ContentVersion cv = new ContentVersion();
        cv.Title = 'Catalent';
        cv.PathOnClient = 'Catalent.pdf';
        cv.VersionData = Blob.valueOf('Test Content');
        cv.IsMajorVersion = true;    
        insert cv; 
        System.debug('created content version'+ cv); 
    
       New_Project_Survey_ID__c PId = new  New_Project_Survey_ID__c();
       PId.Name ='Test Custom setting';
       PId.New_Project_Survey_ID__c = cv.Id;
       Insert PId;
       system.debug('PId'+PId);
       
       
        OppsendEmail oo = new OppsendEmail(new ApexPages.StandardController(opp));
       
       
       
        PageReference pageRef2 = Page.CustomAccountLookup;
        pageRef2.getParameters().put('OpportunityId',opp.id);
        system.debug('pagereference:'+pageRef2);
        CustomAccountLookupController cc=new CustomAccountLookupController();
        cc.OpportunityId=opp.id; 
        cc.searchString=c.Id;
        
         OpportunityContactRole opportunitycontactrole_Obj = new OpportunityContactRole(OpportunityId = opp.id, ContactId = opp.Contact__c, Role = 'Business User', IsPrimary = true);
        Insert opportunitycontactrole_Obj; 
        System.debug('created opportunity contact role'+ opportunitycontactrole_Obj );
        
       OpportunityContactRole opportunitycontactrole_Obj1 = new OpportunityContactRole(OpportunityId = opp.id, ContactId = opp.Contact__c, Role = 'Business User', IsPrimary = false);
        System.debug('created opportunity contact role'+ opportunitycontactrole_Obj1 );
        
        Insert opportunitycontactrole_Obj1; 

       
       // OppsendEmail obj01 = new OppsendEmail(new ApexPages.StandardController(opp[0]));    
       /* opp.IsPrivate=false;
        opp.Name='Name593';
        opp.StageName='Prospecting';
        opp.CloseDate = Date.today();
        Update opp; 
*/
        System.debug('created opportunity'+ opp);
        opportunitycontactrole_Obj.Role='Business User';
        opportunitycontactrole_Obj.IsPrimary=false;
        Update opportunitycontactrole_Obj;
       //oo.sendTo=c.Email;
        PageReference pageRef = Page.SendEmailFromOpp;
        pageRef.getParameters().put('id',opp.id);
       Test.setCurrentPage(pageRef);
       
       oo.uploadPDF();
      test.stopTest();
       
    }
    
    
}

​​​​​​​