Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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.

share|improve this question
    
Are you looking for ApexPage ? What is the problem? Are you getting any errors or it's not returning any records? –  highfive Jul 24 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 Jul 24 at 11:12

2 Answers 2

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.

share|improve this answer
    
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 Jul 25 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' –  Phil Hawthorn Jul 25 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 Jul 25 at 8:30

You wont be able to do a query on mutiple objects if they are not related through 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 .

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.