+ Start a Discussion
Sammy7Sammy7 

invalid relationship key in opportunity

I want to create an invoice so when my opportunity is closedwon and my custom field QuotetoInvoice  on Quote object (is checked) is true
But Im having problem with the conditinal statement.... it says "Invalid foreign key relationship: Opporutnity.quote__r "
for(Opportunity o : trigger.new)
    {
   if (o.stagename=='Closed Won'&& o.quote__r.QuotetoInvoice__c== TRUE))
        {
            oppIdSet.add(o.id);
        }
    }
Please guide me.
Best Answer chosen by Sammy7
SalesFORCE_enFORCErSalesFORCE_enFORCEr
Have you created any custom relationship to Quotes on Opportunity? Generally, Opportunity does not have any direct relationship to Quotes but Quotes have a field called OpportunityId which gets populated when you create quotes on an opportunity. 
So, if you don't have any custom relationship then you have to query thr Quotes object using the opportunity id and then check if the QuotetoInvoice__c is true.

All Answers

Sammy7Sammy7
I think this is what I want...but not sure...I can filter those items in the SOQL query:
// We only care about opportunities that are closed won
	for(Opportunity o : trigger.new)
	{
        if (o.stagename=='Closed Won')
        {
            oppIdSet.add(o.id);
        }
	}

	// Need to grab all the quotes and their subsequent quotelineitems under all the opportunities that fired the trigger, but select quotes that has QuotetoInvoice=True
	List<Quote> quoteList = [SELECT id, quotenumber, Opportunityid, name, QuotetoInvoice__c, 
				(SELECT id, ListPrice, PriceBookEntry.Product2Id, Subtotal, TotalPrice FROM quotelineitems) 
				FROM Quote WHERE Opportunityid in: oppIdSet AND QuotetoInvoice__c=TRUE];

 
SalesFORCE_enFORCErSalesFORCE_enFORCEr
Have you created any custom relationship to Quotes on Opportunity? Generally, Opportunity does not have any direct relationship to Quotes but Quotes have a field called OpportunityId which gets populated when you create quotes on an opportunity. 
So, if you don't have any custom relationship then you have to query thr Quotes object using the opportunity id and then check if the QuotetoInvoice__c is true.
This was selected as the best answer
Sammy7Sammy7
Thats what I thought. Thanks.