Skip to content

August 13, 2015

Salesforce: Writing a Test class for Opportunity Product (OpportunityLineItem)

by Joe Kuan

A note on writing a test class for Opportunity Product (OpportunityLineItem), when assigning a product to an OpportunityLineItem, we need to make sure the product is active. Otherwise, we will get integrity exception. To do that, we use isActive property to filter all the current active products.

    Account a = new Account(Name = 'Test Account Joe ');
    PricebookEntry pbID = [select Id, name from PricebookEntry where isActive = True limit 1];
    Opportunity o = new Opportunity();
    OpportunityLineItem ol = new OpportunityLineItem();
        
    insert a;

    o.AccountId = a.Id;
    o.Name = 'Test_Joe_123';
    o.StageName = 'Prospecting';
    o.CloseDate = date.today();
    o.Type = 'New Business';
    insert o;
        
    ol.OpportunityId = o.Id;
    ol.Quantity = 1;
    ol.UnitPrice = 2.00;
    ol.PricebookEntryId = pbId.Id;
        
    insert ol;
Advertisements
Read more from Apex, Programming, Salesforce

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Note: HTML is allowed. Your email address will never be published.

Subscribe to comments

%d bloggers like this: