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
baboonworksfinebaboonworksfine 

How to retrieve object from SOQL?

I have following test method which set up Opportunity OpportunityLineItem objects, but when I try to retrieve the OpportunityLineItems from opportunity, I get nothing returned.

 

@isTest(SeeAllData=true)
public class QuickEditTest {
    
    static testMethod void opportunityEditTest() {
        
        //System.debug([SELECT id FROM Pricebook2 WHERE isStandard=true].Id);
    
        // setting up data
        
        Product2 pd = new Product2(Name = 'Test product', IsActive = true);
        insert pd;
        
        Pricebook2 pb = new Pricebook2(Name = 'Test Pricebook', IsActive = true);                
        insert pb;
        
        Pricebook2 stdpb = [select id from Pricebook2 where isStandard=true];

        PricebookEntry pbe = new PricebookEntry(
            Pricebook2Id = stdpb.Id,
            Product2Id = pd.Id,
            UnitPrice = 100.00,
            IsActive = true,
            UseStandardPrice = false
        );
        insert pbe;
        
        pbe = new PricebookEntry(
            Pricebook2Id = pb.Id,
            Product2Id = pd.Id,
            UnitPrice = 100.00,
            IsActive = true
        );
        insert pbe;        
        
        // Opportunity & OpportunityLineItem
        Opportunity op = new Opportunity(
            Name = 'TestOpportunity1', 
            CloseDate = System.today(), 
            StageName = 'Prospecting'
        );
        insert op;
        
        OpportunityLineItem ol = new OpportunityLineItem(
            PricebookEntryId = pbe.Id, 
            OpportunityId = op.Id, 
            Quantity = 2,
            TotalPrice = 2 * pbe.UnitPrice
        );                
        insert ol;
        System.debug(op);
        insert ol;

       
        System.debug(op.OpportunityLineItem); /* this returns nothing! */ 
    }
}

 

System.debug(op.OpportunityLineItem) print nothing! Why?

Best Answer chosen by Admin (Salesforce Developers) 
Damien_Damien_

Put these at the bottom of your method:

 

op = [SELECT (SELECT Id FROM OpportunityLineItems) FROM Opportunity WHERE Id = op.Id];
System.debug(op.OpportunityLineItems); 

 I could be slightly off on the syntax, but this should mostly be it.  Yes you can get children from a parent.  There are pretty much 2 ways.  Above is the first way called an inner query.

 

You can also run a query on OpportunityLineItem like:

List<OpportunityLineItem> lineItems = [SELECT Id FROM OpportunityLineItem WHERE OpportunityId = op.Id];

 

All Answers

Naidu PothiniNaidu Pothini

Opportunity is parent object and Opportunity Line Item is child object, so you cannot retrieve child object from parent object.

 

Opportunity doesnt hold any opportunit ylineItem Id's where as OpportunityLineItem holds an Opportunity Id.

 

baboonworksfinebaboonworksfine

I don't get it, because it makes perfect sense to relate parent object and child objects by doing just opportunity.OpportunityLineItems. If this is truely not the case, what is the right way to find the related records using SOQL. Please give an example. Much appreciated.

Naidu PothiniNaidu Pothini

 

Use "OpportunityLineItem.opportunityId",  it will retrieve the opportunity Id on the OpportunityLineItem object.

 

Child record will hold the parentId. But parent can have more childs and cannot hold their Ids.

 

If you look at the OpportunityLineItem Object, you can find the field OpportunityId, where as you cannot find the field OpportunityLineItemId on the Opportunity Object

baboonworksfinebaboonworksfine

Thank you @Naidu, what is interesting is in my controller, I have this code which actually does retrieve the related records:

 

public class OpportunityExtension {

    private Opportunity op;
    
    public OpportunityExtension(ApexPages.StandardController controller) {
        if (controller != null) { 
            this.op = (Opportunity)controller.getRecord();
        }
    }
    // the following works
    public List<OpportunityLineItem> getLineItems() {
        List<OpportunityLineItem> lineItems = op.OpportunityLineItems;
        return lineItems;
    }
}

I believe there are some magic happening because the Opportunity object get by calling (Opportunity))controller.getRecord(), is differet from the Opportunity object op in the test method... So confused...

 

Damien_Damien_

Put these at the bottom of your method:

 

op = [SELECT (SELECT Id FROM OpportunityLineItems) FROM Opportunity WHERE Id = op.Id];
System.debug(op.OpportunityLineItems); 

 I could be slightly off on the syntax, but this should mostly be it.  Yes you can get children from a parent.  There are pretty much 2 ways.  Above is the first way called an inner query.

 

You can also run a query on OpportunityLineItem like:

List<OpportunityLineItem> lineItems = [SELECT Id FROM OpportunityLineItem WHERE OpportunityId = op.Id];

 

This was selected as the best answer