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
Collin EdlCollin Edl 

Test Code Coverage: Add or Remove Row

Hi,

I'm writing test code for a new class that allows you to add multiple Competitors to an Opportunity. I've got the code and visualforce page working, although I'm having a bit of a tough time on the test code as I'm not really sure how to go about getting coverage for adding or removing a row. Maybe I'm overthinking the whole thing, but here's what I've got so far. Any help would be greatly appreciated. I'm more admin-based than developer so it's a little out of my wheelhouse.

Class
 
public with sharing class OpportunityCompetitor{

    public List<Opportunity_Competitor__c> oppComp {get; set;}
    private final Opportunity opp;
    public OpportunityCompetitor(ApexPages.StandardController cstmController) {
        Opp=(Opportunity)cstmController.getrecord();
        oppComp = new List<Opportunity_Competitor__c>();
        Opportunity_Competitor__c OpptyComp = new Opportunity_Competitor__c();
        OpptyComp.Opportunity__c = opp.id;
        oppComp.add(OpptyComp);
        } 
    
    public void addrow(){
        Opportunity_Competitor__c OpptyComp = new Opportunity_Competitor__c();
        OpptyComp.Opportunity__c = opp.id;
        oppComp.add(OpptyComp);}
    
     public void removerow(){
        Integer i = oppComp.size();
        OppComp.remove(i-1);}
    
    public PageReference save(){
        
   //error handling //   
   try{
      insert OppComp;
    }
      catch(Exception e){
           ApexPages.addMessages(e);
       return null;
    }
      
     
    // redirect the user back to the Opportunity record //        
        PageReference opprec = new PageReference('/'+opp.id);
        opprec.setRedirect(true);
        return opprec;
    }
}

Visualforce Page
<apex:page extensions="OpportunityCompetitor" tabStyle="Opportunity" standardController="Opportunity">
    <apex:form >
      <apex:sectionheader title="Opportunity Competitors" subtitle="{!Opportunity.Name}" />
            <apex:pageBlock >
        <p>
          Add one or more Competitors for this Opportunity. You can find competitors by using the Competitor lookup
           or by typing a name in the Competitor field. Click Add Row to enter multiple records or Remove Row to delete rows.  
       </p>
       <p>
        Click Save to add all entered Competitors to the Opportunity. Click Cancel to return to the Opportunity 
        record without saving. All data entered will be lost if you click Cancel.
       </p>
        <br />
        <apex:pageMessages />
        
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="Save" action="{!save}" />
                <apex:commandButton value="Cancel" action="{!cancel}" />
            </apex:pageBlockButtons>
        
             <apex:pageblocktable value="{!OppComp}" var="OC" Title="Opportunity Competitors" id="table">
                      
         <!-- prevent users changing the Opportunity -->
         <apex:column headervalue="Oppportunity" rendered="false">
            <apex:inputfield value="{!OC.Opportunity__c}" style="width:250px;" />
        </apex:column>
         
         <apex:column headervalue="Competitor" >
           <apex:inputfield value="{!OC.Competitor__c}" style="width:290px;" />
        </apex:column>
                 
        <apex:column headervalue="Won" >
           <apex:inputfield value="{!OC.Won__c}" style="width:290px;" />
        </apex:column>
    
            </apex:pageBlockTable>
                      
              <div style="text-align:right;margin-right:10px;font-weight:bold;">       
                     <apex:commandLink value="Add Row" action="{!addRow}" rerender="table,error" immediate="true" /> &nbsp;|&nbsp;
                    <apex:commandLink value="Remove Row" action="{!removeRow}" rerender="table,error" immediate="true" />   
        </div>
            
        </apex:pageBlock>
    </apex:form>
</apex:page>
Test Code
@isTest
private class testOpportunityCompetitor{
      
       static testMethod void testOpportunityCloseExtOpp() {
        
       Id AcctRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Sales Accounts').getRecordTypeId();
       Id CompRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Competitor Accounts').getRecordTypeId();
       Id OppRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Standard').getRecordTypeId();
          
       //Create an account //
        Account acc = new Account();
        acc.Name='Account';
        acc.Type='Prospect';
        acc.recordTypeId = AcctRecordTypeId;
        insert acc;
     
        //Create an Opp //
        Opportunity Opp = new Opportunity();
        
        opp.recordTypeId = OppRecordTypeId;
        opp.name='test opp';
        opp.AccountId=acc.Id;
        opp.Type='NewBusiness';
        opp.Opportunity_Segment__c='Test';
        opp.closeDate=date.today();
        opp.StageName='New';
        opp.LeadSource='Cold Call';
        insert opp;          
       
        //Create a competitor account //
        Account acd = new Account();
        acd.Name='Competitor';
        acd.Type='Competitor';
        acd.recordTypeId = CompRecordTypeId;
        insert acd;
           
        List<Account> Comptr = [SELECT id from Account WHERE Id = :acd.id];
        List<Opportunity> oppList = [SELECT id from Opportunity WHERE Id = :opp.id];
    
        Opportunity_Competitor__c oppComp = new Opportunity_Competitor__c();
        oppComp.Opportunity__c=opp.Id;
        oppComp.Competitor__c=acd.ID;
        insert oppComp;
              
        Test.startTest();
        PageReference pageRef = Page.OpportunityCompetitor;
        Test.setCurrentPageReference(pageRef);
        
        /* Standard controller for the Opportunity */
        ApexPages.StandardController stdCon = new ApexPages.StandardController(opp);
        
        /*Construct the controller extension with the standard controller arg*/
        OpportunityCompetitor Ext = new OpportunityCompetitor(stdCon);
        PageReference oppPage = Ext.save();
        System.assert(oppPage != null);             
        Test.stopTest();
        }
          
          
      
}


 
Best Answer chosen by Collin Edl
v varaprasadv varaprasad
hi Collin,

Please check once following code : 
 
@isTest
private class testOpportunityCompetitor{
      
       static testMethod void testOpportunityCloseExtOpp() {
        
       Id AcctRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Sales Accounts').getRecordTypeId();
       Id CompRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Competitor Accounts').getRecordTypeId();
       Id OppRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Standard').getRecordTypeId();
          
       //Create an account //
        Account acc = new Account();
        acc.Name='Account';
        acc.Type='Prospect';
        acc.recordTypeId = AcctRecordTypeId;
        insert acc;
     
        //Create an Opp //
        Opportunity Opp = new Opportunity();
        
        opp.recordTypeId = OppRecordTypeId;
        opp.name='test opp';
        opp.AccountId=acc.Id;
        opp.Type='NewBusiness';
        opp.Opportunity_Segment__c='Test';
        opp.closeDate=date.today();
        opp.StageName='New';
        opp.LeadSource='Cold Call';
        insert opp;          
       
        //Create a competitor account //
        Account acd = new Account();
        acd.Name='Competitor';
        acd.Type='Competitor';
        acd.recordTypeId = CompRecordTypeId;
        insert acd;
           
        List<Account> Comptr = [SELECT id from Account WHERE Id = :acd.id];
        List<Opportunity> oppList = [SELECT id from Opportunity WHERE Id = :opp.id];
    
        Opportunity_Competitor__c oppComp = new Opportunity_Competitor__c();
        oppComp.Opportunity__c=opp.Id;
        oppComp.Competitor__c=acd.ID;
        insert oppComp;
              
        Test.startTest();
        PageReference pageRef = Page.OpportunityCompetitor;
        Test.setCurrentPageReference(pageRef);
        
        /* Standard controller for the Opportunity */
        ApexPages.StandardController stdCon = new ApexPages.StandardController(opp);        
        
        OpportunityCompetitor Ext = new OpportunityCompetitor(stdCon);
        PageReference oppPage = Ext.save();
		Ext.addrow();
		Ext.removerow();
		
        System.assert(oppPage != null);             
        Test.stopTest();
        }
          
          
      
}

Hope this helps you!

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com