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

Standard

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:actionFunction>
 </apex:SelectList>
 <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:pageblocktable></apex:pageBlock>
 </apex:form>
 </apex:page>

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();
 sortThis.addAll(lstKeys);
 sortThis.sort();

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>();
 if(mapObjects.containskey(selectedObject)){
 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>();
 fieldList.addAll(fieldNames);
 fieldList.sort();
 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);
 lstFieldResult.add(field);
 }
 }
 }
 return null;
 }

private boolean hasReferences(string fieldName){
 Boolean bRet = false;
 for(ApexClass cls:lstClasses){
 if(cls.body.contains(fieldName)){
 bRet = true;
 break;
 }
 }

if(!bRet){
 for(ApexPage pg:lstPage){
 if(pg.Markup.contains(fieldName)){
 bRet = true;
 break;
 }
 }
 }

if(!bRet){
 for(ApexTrigger trg:lstTrigger){
 if(trg.Body.contains(fieldName)){
 bRet = true;
 break;
 }
 }
 }

if(!bRet){
 for(ApexComponent com:lstComponent){
 if(com.Markup.contains(fieldName)){
 bRet = true;
 break;
 }
 }
 }

return bRet;
 }

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

}

Hope that’s helpful

Advertisements