function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Raksha NarayanRaksha Narayan 

Dynamic query stored as list<sobject> not containing lookup fields

I have a dynamic query which contains the following query:
SELECT Id,Name,AccountId,Account.Name,StageName from opportunity id='XXXXXXXXXXXXXXX'
The outcome of the dynamic query is stored in a list<sobject>as below:
queryresult :(Opportunity:{Id=0060c00001USHd3AAH, Name=Test1, AccountId=0010c000025aqcnAAA, StageName=win}, Opportunity:{Id=006a000001ObaQkAAJ, Name="Test2, AccountId=001a000001OsvEGAAZ, StageName=win})
 
Queryresult is a list<sobject> and it is holding 2 records. But the queryresult is not storing Account.Name field which is a lookup field(except id).
I need the lookup field to be stored somewhere as I need them for some comparison purpose.
Could you please tell me how it can be done?
 
David Zhu 🔥David Zhu 🔥
In your case, the query result contains Account.Name field. The only issue is it does not show in the debug log.

If you loop throught the list, you can verfiy the account name field presenting in the query result by adding a line:
system.debug(o.account.Name);
Raksha NarayanRaksha Narayan
Hi David,
Hi below is the sample code:
public static List <Sobject> mymethod() {
    General_Object__c gsetting=new General_Object__c();
    List<SObject> queryresult=new List<SObject>();
    map<string,object> fieldtovalue = new map<string,object>(); 
    String dyquery='';   
  
    dyquery=genvalue+ ' \''+recordid + '\'';
    queryresult=Database.query(dyquery);
    System.debug('dyquery'+dyquery); // output is: SELECT Id,Name,AccountId,Account.Name,StageName from opportunity id='XXXXXXXXXXXXXXX' 
    system.debug('query results :'+queryresult); // output is: queryresult :(Opportunity:{Id=0000c00001USHd3AAH, Name=Test1, AccountId=0010c000025aqcnAAA, StageName=win}, Opportunity:{Id=006a000001ObaQkAAJ, Name="Test2, AccountId=001a000001OsvEGAAZ, StageName=win})
    
    
    for(sObject record :queryresult){
      system.debug('record :'+record); 
        Schema.sObjectType sObjectType = record.getSObjectType();
        system.debug('sObjectType'+sObjectType); // output is: Opportunity
         fieldtovalue = record.getPopulatedFieldsAsMap();
         system.debug('fieldtovalue'+fieldtovalue); // output for first forloop is: {Account=Account:{Name=Account1, Id=0000c0000000qcnAAA}, AccountId=0000c0000000qcnAAA, Id=0000c0000199993AAH, Name=Test1, StageName=win}
    }       
}
can you please guide me here?
David Zhu 🔥David Zhu 🔥
You can refer to the code below and enhance the solution.

    List<SObject> queryresult=new List<SObject>();
    map<string,object> fieldtovalue = new map<string,object>(); 
    String dyquery='';   
  
    dyquery='SELECT Id,Name,AccountId,Account.Name,StageName from opportunity';
   String objectName = 'Account';   //parse dyquery to get object used in this soql, in this sample, I assign 'Account' to objectName

    queryresult=Database.query(dyquery);
    System.debug('dyquery'+dyquery); // output is: SELECT Id,Name,AccountId,Account.Name,StageName from opportunity id='XXXXXXXXXXXXXXX' 
    system.debug('query results :'+queryresult); // output is: queryresult :(Opportunity:{Id=0000c00001USHd3AAH, Name=Test1, AccountId=0010c000025aqcnAAA, StageName=win}, Opportunity:{Id=006a000001ObaQkAAJ, Name="Test2, AccountId=001a000001OsvEGAAZ, StageName=win})
    
    
    for(sObject record :queryresult){
      system.debug('record :'+record); 
        Schema.sObjectType sObjectType = record.getSObjectType();
        system.debug('sObjectType'+sObjectType); // output is: Opportunity
         fieldtovalue = record.getPopulatedFieldsAsMap();
         system.debug('fieldtovalue'+fieldtovalue); // output for first forloop is: {Account=Account:{Name=Account1, Id=0000c0000000qcnAAA}, AccountId=0000c0000000qcnAAA, Id=0000c0000199993AAH, Name=Test1, StageName=win}
        
       
        sObject b = (sObject)fieldtovalue.get(objectName);   //get account object data
        system.debug('b:' + b);

        

    }   
Raksha NarayanRaksha Narayan
Hi David,
 String objectName = 'Account'; - Here the fields are dynamic. For example: there can be multiple lookup fields like Account.Name or demo__r.Name and so on. So can you please tell me how we can handle it dynamically
David Zhu 🔥David Zhu 🔥
I don't think we can get the related object dynamically.
In your case, when the dyquery is built, you have already known which related objects will be in the query.  You can treat string objectName as part of dyquery.