Search code examples
javascriptsalesforcesoql

Salesforce Java script join or reference queries


I am trying to select data from a couple objects at the same time using Javascript.

I want to select the LOB__C, risk_state__c FROM Opportunity and the Contact's firstname and lastname that is related to that Opportunity. I can't figure out how to do that.

Thanks guys.


Solution

  • If you need to access a single Contact, you would need to add a reference field on the Opportunity to the Contact (using either a Lookup or Master-Detail relationship field).

    From there, you can query like this:

    Select LOB__c, risk_state__c, 
        Contact__c, Contact__r.FirstName, Contact__r.LastName From Opportunity 
    

    If you're looking at accessing all/multiple contacts for an Opportunity, store them in the OpportunityContactRole object and use a query like this:

    Select LOB__c, risk_state__c,
        (Select ContactId, 
            FirstName, LastName From OpportunityContactRoles) From Opportunity