7

how to check thru code, if a field is referred / used anywhere in any of our apex code / vf page / component ?

Assume I have a field called ApplicationNumber__c

Say I have a list of apex classes :-

a,b,c,d,e,f

out of which a,b,c are referring/using the field ApplicationNumber__c and d,e,f are not.

same way, say I have a list of pages (probably using standard controller) :-

w,x,y,z

out of which w,x is using this field and y,z is not.

I know if I try to delete this field, it tells me where all the field is referred. Is there any way to fetch this information thru apex code?

I know that this could be done through metadata api and eclipse. But it would be great if we could do that through apex. any suggestions?

I also came across an appexchange tool called FieldTrip which does this.

But I want to build something custom, like a visualforce page, where I will just choose my application name and it should list all fields of that app, that are unused and this has to be done through apex.

So am just asking for a hint as to how I can get the fields referenced in classes,triggers and other components through apex.

2
  • 1
    It's possible to query each Apex Class and apply a regular expression against a list of known fields, which might generically tell you if a field may be used, but that doesn't cover Visualforce pages, components, custom links, and so on. The metadata API is the most appropriate way to do this, and that means that pure Apex Code is out of the question. It is entirely possible, though, to do this in JavaScript, if you felt like torturing yourself.
    – sfdcfox
    Commented Jul 24, 2013 at 19:21
  • If you want access to the Metadata API from Apex, take a look at this, github.com/financialforcedev/apex-mdapi. What your wanting to do is quite extensive though, you maybe better doing this in Java perhaps in Heroku as a REST service that can be called from your VF page. Commented Jul 25, 2013 at 10:26

3 Answers 3

7

In Eclipse, go to your workspace and press Control + H, then search for the field in the workspace. If it's referenced somewhere it should come up in the results.

4
  • Actually I need a list of all fields that are not used in code. so I can't search each field one by one through eclipse. thats the reason I was asking for the approach to get the referenced information through apex code.
    – Sathya
    Commented Jul 23, 2013 at 22:58
  • 1
    I see, I would do that using the metadata API. I would create a script that downloads classes,triggers,vf pages and list of fields per object, then look for each field in those components. salesforce.com/us/developer/docs/daas/…
    – PepeFloyd
    Commented Jul 24, 2013 at 9:57
  • Thanks for the info but it would be great if we could do that in apex.
    – Sathya
    Commented Jul 24, 2013 at 19:00
  • @Sathya So did you find any solution? If yes, please do share :)
    – MnZ
    Commented Jun 23, 2014 at 6:06
4

Its possible to query all the apex class body and Vf page body and then look for the required Field using some string method . This is how you find that if its referred and then to delete it its with Metadata API I believe.This is the snapshot of how i did it

13
  • 1
    How did you run that query?
    – Mike Chale
    Commented Aug 27, 2013 at 14:48
  • private List<apexClass> lstClasses = [select Name, Body from ApexClass]; like this i fetch class body. then i get the field name using Schema.DescribeSObjectResult call i get field details. then search that field (say ApplicationNumber__c) like lstClasses[0].body.contains('ApplicationNumber__c'). This will say if its referred or not . then delete with metadata API if required.
    – Soberano
    Commented Aug 27, 2013 at 14:55
  • Would you add that to your answer? You could also package that up with a VF UI; it'd be a useful utility for many people.
    – Mike Chale
    Commented Aug 27, 2013 at 14:56
  • @KeerthanTantry: Quick doubt, how would you know if the field is from this object?? There could be a field with the same API name from other object as well..
    – Sam
    Commented Aug 27, 2013 at 14:58
  • 1
    Consider the Tooling API, instead, if you're going to build it right. This new API actually tells you what's being referenced. The Developer Console uses this to build the "references" links when you click on an item to open.
    – sfdcfox
    Commented Aug 27, 2013 at 18:43
2

Pretty old thread but adding another option. I have a free and open source app that scans the fields in your Org and returns the components it's used in (Workflows, Processes, Page Layouts, Apex etc).

Keep in mind that returning fields in Apex and VF is not 100% accurate, as a field with the same API name on different objects would return as being found in a class, even though it might not (as others have mentioned).

Keep it mind it can also take quite a while to run on large Orgs.

App: http://schemalister.herokuapp.com/

Source Code: https://github.com/benedwards44/schemalister

You must log in to answer this question.

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