7

I'm trying to create a query where I want to search Apexclass and ApexPages . At present I'm able to search by Apex class,I'm trying to search for Apex class but did not worked.The object for class is ApexClass.

 **Query:** String qStr = 'Select Name from ApexPage where Name like \'%'+searchText+'%\'';

How do I go with class in the same query ? Thanks.

2
  • Are you looking for ApexPage ? What is the problem? Are you getting any errors or it's not returning any records?
    – highfive
    Commented Jul 24, 2014 at 11:07
  • Would like to include ApexClass object as well in this query.There are two columns 1) ApexClass and 2) ApexPage . I want that searching on both should work . Example, if i'm searching for "Task_Assign" page then it will search for all the same search . Likewise now if i want to search with the same query for ApexClass ,then how do i go with same query ?
    – JJoseph
    Commented Jul 24, 2014 at 11:12

3 Answers 3

7

If I understand correctly, you're looking for one Query to query Apex Classes and Apex Pages in one Query? That being the case, ApexPage and ApexClass objects are not related in anyway so you won't be able to do it in one SOQL query.

You will have to query one, and then the other - it is conceivable that you will have a Page and a Class with the same name.

String searchTerm = '%itemname%';
List<ApexPage> pages = [Select Name From ApexPage Where Name Like :searchTerm];
List<ApexClass> classes = [Select Name From ApexClass Where Name Like :searchTerm];

If you're looking for the Controller for a given page, you can include the ControllerKey column in the query on ApexPage:

List<ApexPage> pages = [Select Name, ControllerKey From ApexPage Where Name Like :searchTerm];

Which would at least give you the apex class name for a Controller on the page.

3
  • This worked . Also ,if i want to search for Objects,which is also a column, how will this go ? Because for Pages and Classes , i went for 'Name' in Select. Now i tried like this : 'SELECT SobjectType FROM ObjectPermissions' ,it does give me results ,but it is not filtering.Any pointers ?
    – JJoseph
    Commented Jul 25, 2014 at 7:24
  • You're searching the Object Permissions object rather than an object that simply lists all your objects, thus you'll be getting repeating rows. You should really be using Describe but you can try 'SELECT SobjectType FROM ObjectPermissions group by sobjecttype' Commented Jul 25, 2014 at 8:09
  • But would be the criteria for search ? Like i have for Page, which is, Name like \'%'+searchText+'%\'..I'm doing like this : SELECT SobjectType FROM ObjectPermissions where SobjectType like \'%'+searchText+'%\' group by sobjecttype',,but its not fetching me records..
    – JJoseph
    Commented Jul 25, 2014 at 8:30
2

If your aim is to do something manually as a once-off, you can use the "Query Editor" of the "Developer Console" to run these queries (adding where and order by as required): for classes and triggers. But I don't think many/any other components are exposed in this way, so you will have to use the metadata API for a general solution.

Select a.SystemModstamp, a.Status, a.NamespacePrefix, a.Name,
        a.LengthWithoutComments, a.LastModifiedDate, a.LastModifiedById,
        a.IsValid, a.Id, a.CreatedDate, a.CreatedById, a.BodyCrc, a.Body,
        a.ApiVersion
From ApexClass a

Select a.UsageIsBulk, a.UsageBeforeUpdate, a.UsageBeforeInsert,
        a.UsageBeforeDelete, a.UsageAfterUpdate, a.UsageAfterUndelete,
        a.UsageAfterInsert, a.UsageAfterDelete, a.TableEnumOrId,
        a.SystemModstamp, a.Status, a.NamespacePrefix, a.Name,
        a.LengthWithoutComments, a.LastModifiedDate, a.LastModifiedById,
        a.IsValid, a.Id, a.CreatedDate, a.CreatedById, a.BodyCrc, a.Body,
        a.ApiVersion
From ApexTrigger a
1
  • 1
    This answer doesn't seem relevant to the question at hand.
    – Adrian Larson
    Commented Oct 30, 2017 at 14:19
1

You won't be able to do a query on multiple objects if they are not related through a single query.

You will need 2 different queries and then you can wrap all this in a wrapper and show on the page.

SOSL can be applied but unfortunately, These objects are not searchable and hence you will need two different queries as Phil as answered.

You must log in to answer this question.

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