1

I'm a new in this salesforce world.

I have to show Owner | Account Name | Opportunity Name | Last Meeting Date | Amount

I created an Apex Class and VisualForce Page. This is my query

public List<Opportunity> getOpportunity() {
    List<Opportunity> opp = [SELECT o.id, o.ownerid, o.name, o.AccountId, o.Type, o.Amount from Opportunity o, o.Account a where ownerid =: UserInfo.getUserId()];

    return opp;
}

With this query I have a problem, I don't know where to get a Last Meeting Date.

I think I should create a join between Opportunity and Account. Can someone help me with this...

2
  • Last Meeting Date sounds like a custom field, is this on the Account or the Opportunity? Also, what is the API Name of that field?
    – cmmoutes13
    Commented Aug 2, 2016 at 17:15
  • I think it is this: Last_Meeting_Date__c into Account Object.
    – Josué H.
    Commented Aug 2, 2016 at 17:19

3 Answers 3

4

You will want to add a join to your current query. It should look something like the following.

public List<Opportunity> getOpportunity() {
List<Opportunity> opp = [SELECT o.id, o.ownerid, o.name, o.AccountId, o.Type, o.Amount o.Account.Last_Meeting_Date__c FROM Opportunity o WHERE ownerid =: UserInfo.getUserId()];

return opp;
}
1
  • You are welcome. Can you please mark this answer as the best answer so other users can find the solution quickly. @JosuéH.
    – cmmoutes13
    Commented Aug 2, 2016 at 17:35
3

Queries in Salesforce are a bit weird if you have experience with standard SQL (or pretty much any rdbms that isn't Salesforce, for that matter).

Salesforce's SOQL doesn't have joins per se. That is, there is no JOIN keyword (and you can only specify a single object in the WHERE clause). If you're querying the Opportunity object, and need a field on the Account object, you simply follow the relationship using dot notation. You also don't need to alias the object that you're querying for.

[SELECT Id, Account.Name FROM Opportunity WHERE <condition>]

In your case, the full query would likely be

[SELECT Id, OwnerId, Account.Name, Account.Last_Meeting_Date__c, Name, Amount
FROM Opportunity 
WHERE OwnerId = :UserInfo.getUserId()]

The only sticky bit here is to recognize that the relationship name that you use in the dot-notation in the SELECT clause depends on whether the relationship is a standard field (which is the case between Opportunity and Account) or a custom field (which ends in __c).

For most standard relationships, the API name of the field is <related object name>Id such as AccountId on Opportunity. When fetching Account fields in an Opportunity query, you drop the 'Id' from the field's API name.

For custom relationships, where the field name ends in __c, you simply change that to __r.

That's for traversing upwards in a relationship hierarchy (ie. from a child record to it's parent record), and you can traverse upwards up to 5 levels.

Traversing down a hierarchy is another matter, and you can only traverse one level down.

The developer documentation on relationship queries goes into more detail, and should be helpful.

0

If you want to access a field of the account, it´s not need to perform a join, you can acces fields of a lookup:

SELECT o.Account.Last_Meeting_Date__c  FROM Opportunity o WHERE ownerid =: UserInfo.getUserId()

You must log in to answer this question.

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