0

I want to make a custom relation between Opportunity and Account.

I want to retrieve the account who makes the opportunity

So I wrote this:

"SELECT Id, RecordType.Name, Name, Description, StageName, CloseDate, Type, IsClosed, IsWon,HasOpportunityLineItem, Pricebook2.Name , Owner.Sales_representative_code__c
           , Owner.FirstName, Owner.LastName, Shipping_method__c, Payment_method_code__c, Payment_terms_code__c, Consumables__c, End_date__c, Start_date__c, Expected_delivery_date__c
           , Pricing_model__c, Customer_insights__c,Shipping_cost_at_our_expense__c,
    (
      SELECT  Customer_code__c,Name,Payment_method_code__c,Payment_terms_code__c, Blocked__c, VAT_Code__c, TIN__c
                    ,Owner.Sales_representative_code__c, Owner.FirstName, Owner.LastName, Primary_contact__r.Contact_person_code__c
                   , Primary_contact__r.FirstName, Primary_contact__r.LastName 
     FROM Account            
    )
     FROM Opportunity
    WHERE Opportunity.IsClosed = true
          AND Opportunity.IsWon = true
          AND Opportunity.HasOpportunityLineItem = true
"

But I got this error in output:

Didn't understand relationship 'Account__r' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names.'

What's wrong?

thanks in advance.

5
  • @AlexanderBerehovskiy same error.
    – OiRc
    Commented Jan 26, 2018 at 9:29
  • can you please update your question with more details: what custom relationship do you have. what are API names of that relationship Commented Jan 26, 2018 at 9:39
  • @AlexanderBerehovskiy I updated the question. The api name are declared with "__c" the others are standard fields.
    – OiRc
    Commented Jan 26, 2018 at 9:41
  • you are saying "I want to make a custom relation between Opportunity and Account." have you already created this field on Opportunity? Commented Jan 26, 2018 at 9:46
  • @AlexanderBerehovskiy Yes. I created an opportunity related with an account ,in salesforce.
    – OiRc
    Commented Jan 26, 2018 at 9:48

1 Answer 1

1

You're mistaken the relationship between the Opportunity and the Account.

In fact, the opportunity has a relationship to an Account.

The query you're using tries to query all the accounts related to the opportunity. But there is only one.

Here's the right query:

SELECT Id, RecordType.Name, Name, Description, StageName, CloseDate, Type, IsClosed, IsWon,HasOpportunityLineItem, Pricebook2.Name , Owner.Sales_representative_code__c
           , Owner.FirstName, Owner.LastName, Shipping_method__c, Payment_method_code__c, Payment_terms_code__c, Consumables__c, End_date__c, Start_date__c, Expected_delivery_date__c
           , Pricing_model__c, Customer_insights__c,Shipping_cost_at_our_expense__c,
Account.Customer_code__c,Account.Name,Account.Payment_method_code__c,Account.Payment_terms_code__c, Account.Blocked__c, Account.VAT_Code__c, Account.TIN__c
                    ,Account.Owner.Sales_representative_code__c, Account.Owner.FirstName, Account.Owner.LastName, Account.Primary_contact__r.Contact_person_code__c
                   , Account.Primary_contact__r.FirstName, Account.Primary_contact__r.LastName 
     FROM Opportunity
    WHERE Opportunity.IsClosed = true
          AND Opportunity.IsWon = true
          AND Opportunity.HasOpportunityLineItem = true
4
  • Thanks for the answer, I tried your query. It works, but in correspondant of the account field i see null values. How is it possible?
    – OiRc
    Commented Jan 26, 2018 at 9:54
  • How are you using the opportunities returned by the query after? Commented Jan 26, 2018 at 10:42
  • i want only to retrieve the fields. I don't want to make 3 separate queries for achieve that. is it possible to retrieve Account and owner fields by a giving opportunity?
    – OiRc
    Commented Jan 26, 2018 at 10:58
  • Of course this is possible. This is what you're doing in your query. Commented Jan 26, 2018 at 15:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .