How to find out, if custom fields are referenced in your code? by using Apex & VF


How to find out, if custom fields are referenced in your code?

There could be many ways of doing it. Some common ones I keep hearing are:

  • Try deleting a field from salesforce ui, you will know if its referenced anywhere?
  • Do a text search across your code from eclipse ide.
  • etc…

Well, here is my attempt. I will take help of Apex and Visual Force to answer above question.

Many of us may or may not be aware that salesforce provides few important standard objects like:

  • ApexClass
  • ApexPage
  • ApexComponent
  • ApexTrigger

You might be thinking, when do we use these objects? what are the use cases? well, that’s what this blog post attempts to do. To get you start thinking in this direction.

I used little vf and couple of classes to perform a text search across code and tell you if a custom field is referenced anywhere in your code. I tried keeping the code simple, you may tweak it as needed.

So here goes the code..enjoy..!

fields used

VF Page

<apex:page controller="SmartWays" >
 <apex:form >
 <apex:SelectList value="{!selectedObject}" size="1" onchange="initFieldsScript()">
 <apex:selectOptions value="{!Objects}"></apex:selectOptions>
 <apex:actionFunction action="{!initFields}" name="initFieldsScript" rerender="pbtFields">
 <apex:pageBlock >
 <apex:pageblocktable value="{!lstFieldResult}" var="field" id="pbtFields">
 <apex:column headervalue="Field Name">{!field.FieldApiName}</apex:column>
 <apex:column headervalue="Is Used">{!field.IsUsed}</apex:column>

Apex Class

public class SmartWays{

public String selectedObject {set; get;}
 private List<ApexClass>     lstClasses = new List<ApexClass>();
 private List<ApexComponent> lstComponent = new List<ApexComponent>();
 private List<ApexPage>         lstPage = new List<ApexPage>();
 private List<ApexTrigger>     lstTrigger = new List<ApexTrigger>();
 Map<String, Schema.SObjectType> mapObjects = Schema.getGlobalDescribe();
 public List<FieldsWrapper> lstFieldResult {get; set;}

public SmartWays(){
 lstClasses = [select Name, Body from ApexClass];
 lstPage = [select Name, Markup from ApexPage];
 lstComponent = [select Name, Markup from ApexComponent];
 lstTrigger = [select Name, Body from ApexTrigger];
 selectedObject = '';

public List<SelectOption> getObjects(){
 List<String> sortThis = new List<String>();

List<Schema.SObjectType> gd = mapObjects.Values();
 Set<String> lstKeys = mapObjects.keySet();

List<SelectOption> options = new List<SelectOption>();
 for(String s : sortThis){
 options.add(new SelectOption(s,s));

return options;

public PageReference initFields(){
 lstFieldResult = new List<FieldsWrapper>();
 Schema.SObjectType s = mapObjects.get(selectedObject);
 Schema.DescribeSObjectResult r = s.getDescribe();

Map<String, Schema.SObjectField> fields = r.fields.getMap() ;
 Set<String> fieldnames = fields.keySet();
 List<String> fieldList = new List<String>();
 String fName = '';
 for(integer i=0; i<fieldList.size(); i++){
 fName = fieldList[i];
 if(fName.endswith('__c')){ //only include the custom fields
 FieldsWrapper field = new FieldsWrapper();
 field.FieldApiName = fName;
 field.isUsed = hasReferences(fName);
 return null;

private boolean hasReferences(string fieldName){
 Boolean bRet = false;
 for(ApexClass cls:lstClasses){
 bRet = true;

 for(ApexPage pg:lstPage){
 bRet = true;

 for(ApexTrigger trg:lstTrigger){
 bRet = true;

 for(ApexComponent com:lstComponent){
 bRet = true;

return bRet;

public class FieldsWrapper{
 public string FieldApiName {set; get;}
 public string FieldLabel {set; get;}
 public boolean isUsed {set; get;}


Hope that’s helpful

Get your Visitor’s IP address via sites


Have you ever ran into a situation, where you need to figure out your site’s visitor’s IP address? Well, many of us may or may not have. But the fact is, while working with sites, sooner or later you will bump into this requirement.

So, like every requirement, even this one can be dealt with, in many different ways. Like

1. Use an existing program like or
2. Write a program (which will return visitor ip) in your choice of language (like php, asp, jsp, etc..) and put it on a web server.
3. Create a public vf page and use apex in the background, to get your visitor’s ip address.

I wouldn’t suggest option # 1 and 2 as they may or may not be available when your visitors come to your site. But if you go with option 3
then its almost certain that your visitor ip address can be traced. Because both your site and ip tracing vf page will be on sfdc servers. So, if a visitor is able to see your site that mean you can trace their ip address, for sure.

Here is the sample code:

VF page: getip

<apex:page controller="getipController" showheader="false" contenttype="application/json">{!ipAddress}</apex:page>


Apex class getipController

public class getipController {
 public String ipAddress {get; set;}
 public getipController(){
 ipAddress = ApexPages.currentPage().getHeaders().get('X-Salesforce-SIP');


Now that you can have code in place. Go ahead and make this page public from your site.

That’s it. Your ip tracing page is now ready to be used in your site.

Using jQuery, you could make a JSON call:

$.getJSON("your page url", function(data){alert(data);});


Hope that’s helpful.