You need to sign in to do that
Don't have an account?
baboonworksfine
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?
Put these at the bottom of your method:
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:
All Answers
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.
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.
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
Thank you @Naidu, what is interesting is in my controller, I have this code which actually does retrieve the related records:
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...
Put these at the bottom of your method:
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: