Below code will help you to select one sObject and by clicking on get fields button ,it will display
field level and API name and the data type of it .
By clicking on the link you can export detail into an excel sheet .
public class FetchSelectedObjectFields {
public String selectedObject {get; set;}
public List <InrPair> InrField {get; set;}
public List <SelectOption> objectNames {get; set;}
public Boolean showTable {get; set;}
public Boolean showButton {get; set;}
public Boolean displayPB2 {get; set;}
public Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
public FetchSelectedObjectFields() {
objectNames = initObjNames();
InrField = new List<InrPair>();
showTable = false;
showButton = false;
displayPB2 = false;
}
private List<SelectOption> initObjNames() {
List<SelectOption> objNames = new List<SelectOption>();
objNames.add(new SelectOption('blank', '--Select Object--'));
List<Schema.SobjectType> obj = Schema.getGlobalDescribe().Values();
for(Schema.SobjectType ss:obj) {
objNames.add(new SelectOption(ss.getDescribe().getName(), ss.getDescribe().getName()));
objNames.sort();
}
return objNames;
}
// Find the InrField for the selected object
public void showFields() {
InrField.clear();
if(selectedObject != 'blank') {
Map <String, Schema.SObjectField> fieldMap = schemaMap.get(selectedObject).getDescribe().fields.getMap();
for(Schema.SObjectField sfield : fieldMap.Values()) {
schema.DescribeFieldResult dfield = sfield.getDescribe();
InrPair fld = new InrPair();
fld.key = dfield.getName();
fld.val = dfield.getLabel();
fld.type = string.valueof(dfield.getType());
InrField.add(fld);
}
showTable = true;
showButton = true;
displayPB2 = true;
}
else {
showTable = false;
showButton = false;
displayPB2 = false;
}
}
public PageReference exportData() {
PageReference pf = new PageReference('/apex/fetchObjData');
pf.getParameters().put('objVal', selectedObject);
pf.setRedirect(true);
return pf;
}
public class InrPair {
public String key {get; set;}
public String val {get; set;}
public string type {get; set;}
}
}
///////////////////////////////////////
<apex:page controller="FetchSelectedObjectFields" sidebar="false" >
<apex:form >
<apex:pageBlock id="pb1" >
<apex:outputLabel ><b>Objects :</b></apex:outputLabel>
<apex:selectList value="{!selectedObject}" size="1">
<apex:actionSupport event="onchange" reRender="detail"/>
<apex:selectOption itemLabel="--Select Object--" itemValue="blank" />
<apex:selectOptions value="{!objectNames}"/>
</apex:selectList>
<apex:commandButton value="Get Fields" action="{!showFields}" reRender="pb2" status="fetchStatus" />
<apex:actionStatus id="fetchStatus" >
<apex:facet name="start">
<img src="/img/loading.gif" class="waitingImage" title="Please Wait..."/>
</apex:facet>
</apex:actionStatus>
</apex:pageBlock>
<apex:pageBlock id="pb2" >
<apex:outputPanel rendered="{!displayPB2}" >
<apex:commandLink value="Export To Excel" action="{!exportData}" rendered="{!showButton}" target="_blank" style="font-weight:bold; font-size:15px; color:red" /> <br/><br/>
<apex:pageBlockTable value="{!InrField}" var="fls" rendered="{!showTable}" >
<apex:column headerValue="Field Value" value="{!fls.val}" />
<apex:column headerValue="Field Name" value="{!fls.key}" />
<apex:column headerValue="Field Type" value="{!fls.type}" />
</apex:pageBlockTable> <br/><br/>
</apex:outputPanel>
</apex:pageBlock>
</apex:form>
</apex:page>
/////////////////////////////////////////////////
public class ExportFieldDetailInExcel {
public List<InrPair> InrField {get; set;}
public string str;
public Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
public ExportFieldDetailInExcel() {
str = ApexPages.CurrentPage().getParameters().get('objVal');
try{
InrField = new List<InrPair>();
if(str != 'blank') {
Map <String, Schema.SObjectField> fieldMap = schemaMap.get(str).getDescribe().fields.getMap();
for(Schema.SObjectField sfield : fieldMap.Values()) {
schema.DescribeFieldResult dfield = sfield.getDescribe();
InrPair fld = new InrPair();
fld.key = dfield.getName();
fld.val = dfield.getLabel();
fld.type = string.valueof(dfield.getType());
InrField.add(fld);
}
}
}catch(Exception e) {}
}
public class InrPair {
public String key {get; set;}
public String val {get; set;}
public string type {get; set;}
}
}
/////////////////////////////////////////////////////
<apex:page controller="ExportFieldDetailInExcel" contentType="application/vnd.ms-excel#{!$CurrentPage.Parameters.objVal}.xls" cache="true" standardStylesheets="false" sidebar="false" showHeader="false" >
<apex:pageBlock >
<apex:dataTable value="{!InrField}" var="fls" >
<apex:column headerValue="Field Value" value="{!fls.val}" />
<apex:column headerValue="Field Name" value="{!fls.key}" />
<apex:column headerValue="Field Type" value="{!fls.type}" />
</apex:dataTable>
</apex:pageBlock>
</apex:page>
nice one sir
ReplyDeleteIt seems more research has been done and time spent to create this website as the information is very good in all the posts. To this I attending salesforce online training helping me get extra knowledge on the subject.
ReplyDeleteThanks Santosh ,Glad to see your commet
Deleteso will this export a report to external non SF user?
ReplyDeleteThank You and I have a tremendous offer you: How Long Does House Renovation Take top home renovation companies
ReplyDeleteGreat post! An advanced advanced excel course
ReplyDeletehelps learners build strong skills in complex formulas, PivotTables, dashboards, data analysis, and automation techniques through practical, hands-on training for real-world business and job-ready applications.
I found this article very helpful. Tableau Developer Training equips learners with advanced skills in dashboard design, data integration, and business intelligence reporting. It is an excellent pathway for those seeking rewarding opportunities in analytics and data visualization.online tableau course
ReplyDelete