+ Start a Discussion
aswanijagadesh1.397398535585991E12aswanijagadesh1.397398535585991E12 

query the records from different objects?

Hi,
i have an objects Like A,A1,A2........and so on.(No Relation Ship Between Objects).

Now i want to  get id,name fields objects where object name like '%A%'.(get all records where object name contains A)
how can i please tell me.
Avidev9Avidev9
Have you heard about SOSL ? they are for the same purpose, you can try to read more about them here http://www.salesforce.com/us/developer/docs/officetoolkit/Content/sforce_api_calls_sosl.htm
aswanijagadesh1.397398535585991E12aswanijagadesh1.397398535585991E12
i have little idea about sosl.(to get records from different objects at a time we use sosl) . 
Shyam BhundiaShyam Bhundia
Hi,

The from clause in a query must be concrete (i.e you cannot do From %A%).

If you can give a bit more context on what you are trying achieve, we may be able to suggest another approach.
aswanijagadesh1.397398535585991E12aswanijagadesh1.397398535585991E12
my context is simple that is i have objects like A,A1.....
i want to query the records in batch class from all objects where object name contains A .then i will pass that list to execute method
aswanijagadesh1.397398535585991E12aswanijagadesh1.397398535585991E12
if any one help for this must be appreciated.
Nilesh Jagtap (NJ)Nilesh Jagtap (NJ)
hi,

You can use SOSL as mentioned by Avidev. It is not possible to query like 'select id from %A%' in salesforce.
Other approach is to query as list<sObject> where you can dynamically pass object name one at a time.
e.g.

public list<sObject> fetchReferenceData(String objectName) {
     String query = 'SELECT id, Name FROM '+objectName+' LIMIT 1000' ;
     list<sObject> sobjectList = Database.query(query);
     return sObjectList;
}

aswanijagadesh1.397398535585991E12aswanijagadesh1.397398535585991E12
then how can i get list of objects where object Name Like 'A'
aswanijagadesh1.397398535585991E12aswanijagadesh1.397398535585991E12
public class TestObject {
   Public string ObjectSelected{get;set;}
    Public Map<String, Schema.SObjectType> AllObjmap;
   
    Public TestObject(){
        AllObjmap = New Map<String, Schema.SObjectType>();
        AllObjmap = Schema.getGlobalDescribe();
        System.debug('******All object Names :'+ AllObjmap.keyset());
    }
   
  
    public void testrecords()
    {
         List<selectoption> objList = new List<selectoption>();
         List<String> testSobj=new List<String>();
         list<sObject> sobjectList=new list<sObject>();
         list<sObject> sobjectList1=new list<sObject>();
        
        for(string s:AllObjmap.keyset()){
            if(s.startsWith('test')&&s.endsWith('__c') ) {
                objList.add(new selectoption(s,s));
            }
        }
        for(selectoption test:objList)
        {
        testSobj.add(test.getLabel());
        system.debug('33333333'+test.getLabel() );
        }
       
        for(String s:testSobj)
        {
             String query = 'SELECT id FROM '+s+' Where Name LIKE\'%test%\' LIMIT 1000' ;
             sobjectList = Database.query(query);
              system.debug('ssssssssss'+sobjectList);
              for(Sobject s1:sobjectList)
              {
              sobjectList1.add(s1);
              }
        }
        system.debug('ssssssssss'+sobjectList1);       
    }
}

in Page:
<apex:page controller="TestObject" action="{!testrecords}">
</apex:page>

like this will get records.