[SalesForce] SOQL join two tables – all contacts from lead

I have a Lead object and would like to get all contacts for this Lead. I was not able to find an appropriate method in apex and decided to make a query. Unfortunately I don't have much experience with SOQL therefore could you please advise how to adapt the following query in SOQL. Here is the query:

select contact.* from lead, contact where lead.id = contact.Company_Name__c and lead.id = '00Q560000019qCB'

Please advise how to transform the above query in SOQL?

Regards,

Dilyan

Best Answer

There is no * in the SOQL query so you will have to specify the fields you want to pull.

I am going to ignore your need for the Company to be the same for now until you clarify your question.

to get the data from converted leads you can do the following:

Note You should ALWAYS limit your query by using selective filters as you cannot return more than 50K rows in a transaction now should you need to.

Set<ID> SetOdIds = New Set<ID>();

//Populate the set using results from another query, trigger, or something that identifies the contact IDs you are looking for. Maybe `[Select Id From Contact Where Company = 'test Company'`

[Select ConvertedContact.LastName, ConvertedContact.firstName From Lead Where isConverted = true and convertedcontactid IN :setOfIds]

You should read up and be familiar with the Force.com SOQL and SOSL Reference

Related Topic