+ Start a Discussion
bbanks1981bbanks1981 

SOQL Error in Apex Code - Beginner

Good Morning,

 

I am new to Apex coding and any help would be appreciated.  I have a customer object orders and opportunity line items are related to an order.  I am trying to write a trigger that will rollup the total price of products on an order.  I get an error on the SOQL query and I cannot figure out why or work it out.  Below is my code.  Thanks in advance.

 

Trigger SumTotalPriceofProductsonOrder on OpportunityLineItem (after delete, after insert, after undelete, 
after update) 
{

	set<id> order_ids = new set<id>();
	
	if(trigger.isInsert || trigger.isupdate || trigger.isundelete)
		{for (opportunitylineitem t : Trigger.new)
			{if( trigger.isinsert || trigger.isundelete)
				{if(t.Order__c != null && t.totalprice > 0)
					{order_ids.add(t.Order__c);
					}
				}
		
			else if (trigger.isupdate)
				{if (trigger.oldmap.get(t.id).Order__c != null)
					order_ids.add(trigger.oldmap.get(t.id).Order__c);
				 if (trigger.newmap.get(t.id).Order__c != null)
				 	order_ids.add(trigger.newmap.get(t.id).Order__c);
				}			
			}
		}
	else if (trigger.isdelete)
		{for(opportunitylineitem t : Trigger.old)
			{if (t.Order__c != null && t.TotalPrice > 0)
				{order_ids.add(t.Order__c);
				}	
			}
		}

	Orders__c[] ord = new Orders__c[]{};
	
	for (Orders__c o : [Select Id, (Select Id, TotalPrice from OpportunityLineItem) From Orders__c where id in :order_ids])
		{
			double totalPrice = 0;
			
			for (OpportunityLineItem t : o.OpportunityLineItem)
				OrderTotalPrice =+ t.totalprice;
				
			o.Total_Price_of_Products__c = ordertotalPrice;
			ord.add(o);
		}
	update ord;
}

 

rvkrvk

Are u sure that the child relation ship name is OpportunityLineItem in the query.If you could post the error message that would make easy to figure out the issue. 

Rahul SharmaRahul Sharma

Is the child relationship name = Opportunity_Product__r.

if yes then try this:

 

for (Orders__c o : [Select Id, (Select Id, TotalPrice from Opportunity_Product__r) From Orders__c where id in :order_ids])

 

bbanks1981bbanks1981

Thanks I think that may be it.  CS3 is down right now, but I will post back once I am able to check.

bbanks1981bbanks1981

CS3 is finally back up and this did not correct my problem.  I am getting the same error message.  "Didn't understand the relationship Opportunity_Product__r in the from part of query call."  Thanks again for all the help.

Rahul SharmaRahul Sharma

What is child relationship name of OpportunityLineItem?

bbanks1981bbanks1981

Best I can tell it is OpportunityLineItem.  See example below.  It is exactly what I am trying to do, but with a customer object orders__c instead of Opportunities.

 

 

 

 

 

Using the nested query, we're specifying that for each opportunity we want the respective set of OpportunityLineItem records that are related through the OpportunityLineItems child relationship.

Basic Parent-to-Child (Aggregate) Traversal To traverse a relationship from a parent to a set of children, use a nested query. For example, this SOQL query retrieves opportunities and the opportunity products associated with each opportunity:

 
1SELECT Id, Name, Amount,
2       (SELECT Quantity, UnitPrice, TotalPrice
3        FROM OpportunityLineItems)
4FROM Opportunity
 

Using the nested query, we're specifying that for each opportunity we want the respective set of OpportunityLineItem records that are related through the OpportunityLineItems child relationship.