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
stollmeyerastollmeyera 

Writing test case for simple trigger

I am brand new to Apex and need a litle help writing my first test case.  The trigger to be tested is below (areas in red are what my test coverage is missing):

 

 

trigger OpportunityUpdateAccount on Opportunity(after insert, after update) {
List<Account> accsToUpdate = new List<Account>();

for(Opportunity opp : trigger.new)
{if (opp.StageName == '4 - Closed Won' && opp.Hosting__c > 0)
accsToUpdate.add(new Account(Id=opp.AccountID, Type = 'Customer'));
}
if (accsToUpdate != null && !accsToUpdate.isEmpty())
Database.update(accsToUpdate);
}

 

 

Test case below:

 

 

@isTest
private class OpportunityUpdateAccountTest{
    static testMethod void testOpportunityUpdate(){
    
    //Setup the Account record
    Account a = new Account(Name = 'Test Account');
    a.Type = 'Customer';
    insert a;
    
    // Verify that the initial state is as expected
    a = [SELECT Name, Type
        FROM Account
        Where Id = :a.Id];
    System.assertEquals ('Customer', a.Type);
    
    // setup the Opportunity record
    String opportunityName = 'Test Opportunity';
    Opportunity o = new Opportunity(AccountId=a.Id, name=opportunityName, StageName='4 - Closed Won', CloseDate = Date.today() );
                                    
    //Cause the Trigger to execute
    insert o;
    
    //Verify the results
    a = [SELECT Name, Type
        FROM Account
        WHERE Id = :a.Id];
    }
}

A key thing that I am missing from my trigger is adding a Product to the Opportunity.  This effects the  field 'Hosting__c'.  This is a rollup summary field of the total price of the Products under the Product Family "Hosting", that are or course added to the Opportunity,  How would I go about adding in a Product line item in my test case so that I can make my test meet the criteria of 'Hosting__c > 45'?

 

Best Answer chosen by Admin (Salesforce Developers) 
stollmeyerastollmeyera

Finally figured this out.  I forgot to specify the Product2Id on my Opportunity...Hence the error I was getting.  Final code:

 

 

@isTest
private class OpportunityUpdateAccountTest{
    static testMethod void testOpportunityUpdate(){
    
    //Setup the Account record
    Account a = new Account(Name = 'Test Account');
    a.Type = 'Customer';
    insert a;
    
    // Verify that the initial state is as expected
    a = [SELECT Name, Type
        FROM Account
        Where Id = :a.Id];
    System.assertEquals ('Customer', a.Type);
    
    // setup the Opportunity record
    String opportunityName = 'Test Opportunity';
    Opportunity o = new Opportunity(AccountId=a.Id, name=opportunityName, StageName='4 - Closed Won', CloseDate = Date.today(),Pricebook2Id = '01s60000000MgzS' );
    OpportunityLineItem optLI = new OpportunityLineItem(OpportunityId = o.Id, Quantity=1, TotalPrice = 1, PricebookEntryId = '01t60000002fTY0');
    
    //Insert the opportunity
    insert o;
    
    //Insert the Product
    insert optLI;
    
    //Verify the results
    a = [SELECT Name, Type, Closed_Date__c
        FROM Account
        WHERE Id = :a.Id];
        
    }
}

 

Thanks for all the help, everyone!

 

All Answers

Ritesh AswaneyRitesh Aswaney

Hey again !

 

This bit of code should do it :

 

OpportunityLineItem optLI = new OpportunityLineItem(OpportunityId = o.Id, Quantity=1, TotalPrice = 1, PriceBookEntryId = [Select Id from PriceBookEntry LIMIT 1].Id);
insert optLI;

sravusravu

Try adding the highligthed lines to your code and see if it works for you.

 

 

@isTest
private class OpportunityUpdateAccountTest{
static testMethod void testOpportunityUpdate(){

//Setup the Account record
Account a = new Account(Name = 'Test Account');
a.Type = 'Customer';
insert a;

// Verify that the initial state is as expected
a = [SELECT Name, Type
FROM Account
Where Id = :a.Id];
System.assertEquals ('Customer', a.Type);

// setup the Opportunity record
String opportunityName = 'Test Opportunity';
Opportunity o = new Opportunity(AccountId=a.Id, name=opportunityName, StageName='4 - Closed Won', CloseDate = Date.today() , Hosting__c = 10);

//Cause the Trigger to execute
insert o;

//Verify the results
a = [SELECT Name, Type
FROM Account
WHERE Id = :a.Id];
}

Opportunity opp = [select Id, Name, AccountId, StageName, Hosting__c from opportunity where Id= :o.Id];

opp.Hosting__c = 45;
update opp;

}

 

 

Hope this helps you/

stollmeyerastollmeyera

That definitely has me on the rigth track, but I think I am misunderstanding the use of the PriceBookEntryId.  We only have the default Standard Price Book.  I have seen examples of this used in other triggers, but how do I go about finding the appropriate API name to call it?

sravusravu

The best way is through eclipse IDE

stollmeyerastollmeyera

Finally figured this out.  I forgot to specify the Product2Id on my Opportunity...Hence the error I was getting.  Final code:

 

 

@isTest
private class OpportunityUpdateAccountTest{
    static testMethod void testOpportunityUpdate(){
    
    //Setup the Account record
    Account a = new Account(Name = 'Test Account');
    a.Type = 'Customer';
    insert a;
    
    // Verify that the initial state is as expected
    a = [SELECT Name, Type
        FROM Account
        Where Id = :a.Id];
    System.assertEquals ('Customer', a.Type);
    
    // setup the Opportunity record
    String opportunityName = 'Test Opportunity';
    Opportunity o = new Opportunity(AccountId=a.Id, name=opportunityName, StageName='4 - Closed Won', CloseDate = Date.today(),Pricebook2Id = '01s60000000MgzS' );
    OpportunityLineItem optLI = new OpportunityLineItem(OpportunityId = o.Id, Quantity=1, TotalPrice = 1, PricebookEntryId = '01t60000002fTY0');
    
    //Insert the opportunity
    insert o;
    
    //Insert the Product
    insert optLI;
    
    //Verify the results
    a = [SELECT Name, Type, Closed_Date__c
        FROM Account
        WHERE Id = :a.Id];
        
    }
}

 

Thanks for all the help, everyone!

 

This was selected as the best answer
backupbackup

Hi    Stollmeyera

 

Could you please tell me from where you are fetching the PricebookentryId and Pricebook2ID....The ids u have used in these two fields in the test case above is coming from Products object?

I need to implement the same in my Org...

Thanks

Shv

backupbackup

Hi   Stollmeyera

 

Could you please tell me from where you are fetching the PricebookentryId and Pricebook2ID....The ids u have used in these two fields in the test case above is coming from Products object?

I need to implement the same in my Org...

Thanks

Shv

stollmeyerastollmeyera

@backup 

 

I believe I used the Excel Connector, which allows you to access the pricebook table directly and view the ids 

 

More info here: http://www.salesforce.com/community/crm-best-practices/administrators/data-management/data-tools/excel-connector.jsp