+ Start a Discussion
s_macs_mac 

Dynamic Soql Query

I have an object,which have an Many to many Relationships to itself using a junction object.Like..
Object 'A'
Object 'JunctionA' this has two lookups to object 'A' parent(lookup) and Child(lookup) to A.

Now Rec1
         |
       Rec 2 --------|_____Rec3,Rec4
          |
       Rec 5
          |
     Rec 6

Rec1,Rec2 will be related from Junction object and the same way Rec 2 and Rec 5 and so on

Now I have to write a dynamic soql query,which checks whether Rec1 has any childs if yes,query their childs and then check Rec 2 has childs and if Yes, then query them.. and so on.. as the levels of hierarchy is dynamic here..

Thanks in Advance
 

Amit Chaudhary 8Amit Chaudhary 8
Please check below post for dynamic SOQL
1) http://amitsalesforce.blogspot.com/2015/04/pagination-using-standardsetcontroller.html
 
public PageReference Search()
    {
        String query= '';
        String strFilter = '';
        if(acc.Name != null && (acc.Name ).trim() !='')
        {
           strFilter  = strFilter  +  ' where Name Like \''+acc.Name+'%\'' ;
        }
        if(acc.Phone != null && (acc.Phone).trim() !='' )
        {
           if(strFilter == '')
           { 
               strFilter  = strFilter  +  ' where Phone like \''+acc.Phone+'%\'' ;
           }
           else
           {
               strFilter  = strFilter  +  ' And Phone like \''+acc.Phone+'%\'' ;
           }
        }
        if(strFilter != '')
        {
            query = 'Select name ,id, phone from Account '+strFilter+ ' limit 1000';
            System.debug('Query ---->'+ query );
            con = new ApexPages.StandardSetController(Database.getQueryLocator(query)); 
            con.setPageSize(2);
        }
        else
        {
        }
       

Let us know if this will help you