I am a bit new to salesforce, so kindly help.

I have different recordsIDS like from contacts, opportunities,leads and other sections, I need to use SQL Query and return the Name and Email.

My code follows as this, kindly help how to create a custom list and return based on ID.

I am getting errors such as unexpected Name and email,so how to create a custom SOQL query or which can work here.

  public static List <Contact> getAccountDetails(String recordId){
 //  List<Contact> results = [SELECT Name, Email 
   //FROM Contact 

       If (recordId.startsWith('003')){
          return [SELECT Name, Email FROM Contact WHERE id = :recordId];
    If (recordId.startsWith('00Q')){
      return [SELECT Name, Email FROM Lead WHERE id = :recordId];
      If (recordID.startsWith('006')){
         return [SELECT 'Name', 'Email' FROM 'OpportunityContactRole' WHERE 'OpportunityId'= :recordId];
   If (recordID.startsWith('0Q0'){
      return [SELECT Name, Email FROM Quote WHERE id = :recordId];
    If (recordID.startsWith('001')){
           return [SELECT Name, Email FROM Contact WHERE id = :recordId];
    If (recordID.startsWith('003'))
         return [SELECT Name, Email FROM Contact WHERE id = :recordId];

Adding controller code as well for reference

  getCurrentPageDetails : function(component, event, helper) {
     var action  = component.get("c.getAccountDetails");
        'recordId' : component.get("v.recordId")
    action.setCallback(this, function(response){
        var status = response.getState();
        if(status === "SUCCESS"){
            component.set("v.name",response.getReturnValue()[0].Name )

1 Answer 1


For the related objects, you'll want to use the Contact relationship, etc. For example:

return [SELECT Contact.Name, Contact.Email FROM OpportunityContactRole WHERE OpportunityId = :recordId];

You'll want to check the data model and/or the Schema Builder to figure out the relationships. Also, fields are never 'Quoted' in SOQL.

  • Thanks, But getting this error Illegal conversion from List<OpportunityContactRole> to List<Contact> Oct 12, 2020 at 16:23
  • @nms You'll want to change the method to public static List<sObject>.
    – sfdcfox
    Oct 12, 2020 at 16:25

You must log in to answer this question.

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