3

I'm very new with Salesforce. I've been reading this page trying to figure out how to do joins. All the objects were created before I jumped on this project.

I have one custom object called : Person_Project_Junction__c and another one called Contact.

My query looks like this:

 SELECT
      Person_Project_Junction__c.Id,
      Person_Project_Junction__c.Person_First_Name__c,
      Person_Project_Junction__c.Person_Last_Name__c
    FROM
      Person_Project_Junction__c

I need to access a field in the Contact object called Industry__c . I tried doing this query:

  SELECT
          Person_Project_Junction__c.Id,
          Person_Project_Junction__c.Person_First_Name__c,
          Person_Project_Junction__c.Person_Last_Name__c,
          (SELECT Industry__c FROM Contact)
        FROM
          Person_Project_Junction__c

But of course, it doesnt work. How would I be able to do this join? Thanks!

1 Answer 1

5

You don't need to prefix fields with the Object's API Name.

Try this query to make sure it works fine:

list<Person_Project_Junction__c> myList = [SELECT Id,
                                                 Person_First_Name__c,
                                                 Person_Last_Name__c,
                                           FROM Person_Project_Junction__c];

Now, my question is this: Is there a lookup field to Contact on the Person_Project_Junction__c object? Assuming yes, there is, and the lookup field is called Contact__c, try this:

list<Person_Project_Junction__c> myList = [SELECT Id,Person_First_Name__c,
                                                  Person_Last_Name__c,
                                                  Contact__r.Industry__c
                                           FROM Person_Project_Junction__c];

Example: Let's say on the Custom Object myCustomObject__c you have a lookup field called MyLookupField__c to another sObject, say AnotherObject__c. If you want to query AnotherObject__c's field AnImportantField__c, do the following query:

list<myCustomObject__c> myCustomObjList=[SELECT id, MyLookupField__r.AnImportantField__c
                                         FROM myCustomObject__c
                                         WHERE ...];

Update:

Since you only need the SOQL query, here it is:

SELECT Id,Person_First_Name__c,
       Person_Last_Name__c,
       Contact__r.Industry__c
FROM Person_Project_Junction__c
8
  • Hi. Thanks for answering. It gives me this error: MALFORMED_QUERY: unexpected token: list
    – raygo
    Commented Dec 13, 2013 at 15:28
  • i edited my answer Commented Dec 13, 2013 at 15:30
  • Hi. I'm not sure if I have a lookup field, but I'm still getting MALFORMED_QUERY: unexpected token: list. I'm going to check about the lookup field
    – raygo
    Commented Dec 13, 2013 at 15:37
  • Are you using my updated SOQL queries? I think you error is because you may have (SELECT Industry__c FROM Contact) in your SELECT statement. I don't normally use sub-queries in my SELECT statements (so I'm not an expert on it) though I know it's possible & useful. Commented Dec 13, 2013 at 15:40
  • Yes. I copy pasted them. I think my the problem has to do with the word list in front of the query. BTW. I'm using PHP and SOAP to get the data.
    – raygo
    Commented Dec 13, 2013 at 15:44

You must log in to answer this question.

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