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
Neha@SfdcNeha@Sfdc 

can generic SObject hold data of 2 different objects?

Hi All,
  • i have a scenario where i want to access List<List<SObject>> lst=[FIND :inputText RETURNING Account(name),Product2(name)]
  • lst contains list of two different types and i want to anyhow show this list values on  a visual force page
  • I assume that it is possible to do so by the following
for(List<sObject> obj:showContent)
 {
    for(sObject accSobj:obj)
     {   
        
        contentData.add(accSobj);
       
   }
}

But i am unable to access contentData on my vf

Any help would be appreciated

Thanks,
Neha 
kiranmutturukiranmutturu
Yes you can use collections of sObjects as one generic List of sObjects to perform a single DML operation...When it comes to vf page you will refer the respective object field. In this case both object should have same field names if possible or convert all the lsit of sobjects in to wrapper class list to access the value from the respective object field value....
Neha@SfdcNeha@Sfdc
hi kiran
Thanks for the reply...Now i have changed my code as under


public class ContentSearch_VFC {

    public String inputText { get; set; }
        
    public List<sObject> contentData{get;set;}
    public List<String>  fieldsLst{get;set;}
    public List<List<sObject>> showContent=new List<List<sObject>>(); 
    
    public ContentSearch_VFC()
    {
        contentData=new List<sObject>();
        fieldsLst=new List<String>();
        fieldsLst.add('Name');
    }
      
    public void goSearch()
     {
         if(inputText!=null)
         {
             showContent=[FIND :inputText RETURNING Account(name),Product2(name)];
             System.debug('********** list is'+showContent);
             System.debug('***** list[0]'+showContent[0]);
         }
         if(showContent!=null)
         {
            for(List<sObject> obj:showContent)
            {
                for(sObject accSobj:obj)
                {  
                     contentData.add(accSobj);
                }
            }
         }    
    }
 }

and my VF as under:
<apex:pageBlockSection >
  <apex:outputLabel value="Search for:"></apex:outputLabel>
  <apex:inputText value="{!inputText}"/>

  <apex:commandButton action="{!goSearch}" image="/img/func_icons/util/search16.png" id="btl"/>

  <apex:pageBlockTable value="{!contentData}" var="item" rendered="{!NOT(contentData==NULL)}">
   <apex:repeat value="{!fieldsLst}" var="field">
   <apex:column value="{!item[field]}"/>
   </apex:repeat>
  </apex:pageBlockTable>
  </apex:pageBlockSection>

Although now my code is working as expected,but i feel something is incorrect...i have hardcoded the field's list...as i had to display a field that would be in both the objects...
  • Problem:Is there any way to get the fields apart from using the abv method i have mentioned?
Thanks,
Neha
kiranmutturukiranmutturu
In general you need to specify the field at some level ... I hope we can't create that much dynamic ....Your input should know the type of the field that you are going to use.  One more thing you can do is define the custom settings with the set of fields that you want to make use in your app process. In future if you want add/remove fields form there....
Neha@SfdcNeha@Sfdc
By define a custom setting you mean that i should create data sets with suppose 2 fields that would store the api names of any 2 salesforce objects and then using them by querying them in controller,putting it in a list of String and using the list in repeat tag...Is that what you mean?
kiranmutturukiranmutturu
Yes..I mean it...
Neha@SfdcNeha@Sfdc
ok Thanks kiran