Created
December 13, 2016 03:24
-
-
Save ohadios/4c2eea25d662dc8deb72e0904f2ad66b to your computer and use it in GitHub Desktop.
Webinar 11 - VisualForce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<apex:page controller="ContactQueryController" showHeader="true"> | |
<apex:form > | |
<apex:pageBlock title="Query Objects"> | |
<apex:pageBlockSection columns="1"> | |
<apex:selectList title="" size="1" value="{!selectedObject}" > | |
<apex:selectOptions value="{!objectNames}"/> | |
<apex:actionSupport event="onchange" reRender="fieldSelection"/> | |
<apex:outputLabel >Select the object you would like to query</apex:outputLabel> | |
</apex:selectList> | |
</apex:pageBlockSection> | |
<apex:pageBlockSection columns="2" title="Select fields to query" id="fieldSelection"> | |
<apex:selectList size="1" value="{!selectedField}"> | |
<apex:selectOptions value="{!objFields}"/> | |
<apex:outputLabel >Select Fields to Query:</apex:outputLabel> | |
</apex:selectList> | |
<!-- Button performs a query we have the results and we have results Id. --> | |
<apex:commandButton action="{!btnPerformQuery}" reRender="rPanel" value="Query!"/> | |
<apex:inputText value="{!criteria}"> | |
<apex:outputLabel value="Query Value"/> | |
</apex:inputText> | |
</apex:pageBlockSection> | |
<apex:outputPanel id="rPanel"> | |
<apex:pageBlockSection title="Results" id="results" rendered="{!queried}"> | |
<apex:pageBlockTable value="{!results}" var="r"> | |
<apex:column value="{!r.id}"/> | |
<apex:column value="{!r['Name']}"/> | |
<apex:column value="{!r['createdBy.name']}"/> | |
<apex:column value="{!r['createdDate']}"/> | |
<apex:column value="{!r['lastModifiedBy.name']}"/> | |
<apex:column value="{!r['lastModifiedDate']}"/> | |
<apex:column value="{!r[selectedFieldAPI]}"/> | |
</apex:pageBlockTable> | |
</apex:pageBlockSection> | |
</apex:outputPanel> | |
</apex:pageBlock> | |
</apex:form> | |
</apex:page> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public with sharing class ContactQueryController { | |
public Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe(); | |
private Map<String, String> fldAPINameMap = new Map<String,String>(); | |
public List<SelectOption> objectNames{public get; private set;} | |
public String selectedObject {get; set;} | |
public String selectedField {get;set;} | |
public String selectedFieldAPI {get;set;} | |
private List<SelectOption> fields; | |
Public string criteria{get;set;} | |
public List<sObject> results{get;set;} | |
public boolean queried {get;set;} | |
public List<SelectOption> getObjFields() { | |
fields.clear(); | |
if(queried == null){queried = false;} | |
system.debug('Page loading, Queried is: '+queried); | |
// If an object was not selected yet, just populate the text 'Select object first' | |
if(selectedObject == NULL){ | |
fields.add(new selectOption('Select Object First', 'Select Object First')); | |
} | |
// if object was selected, create a list of the object's fields | |
else { | |
system.debug('$$$$$' + selectedObject); | |
Map <String, Schema.SObjectField> fieldMap = schemaMap.get(selectedObject).getDescribe().fields.getMap(); | |
for(Schema.SObjectField sfield : fieldMap.Values()) | |
{ | |
schema.describefieldresult dfield = sfield.getDescribe(); | |
String fieldName = dfield.getLabel(); | |
fields.add(new SelectOption(fieldName, fieldName)); | |
fldAPINameMap.put(fieldName, dfield.getname()); | |
} | |
} | |
return fields; | |
} | |
// Constructor | |
public ContactQueryController(){ | |
objectNames = initObjNames(); | |
fields = new List<SelectOption>(); | |
List<sObject> results = new List<sObject>(); | |
String Criteria; | |
} | |
private List<SelectOption> initObjNames() { | |
List<SelectOption> objNames = new List<SelectOption>(); | |
List<String> entities = new List<String>(schemaMap.keySet()); | |
entities.sort(); | |
for(String name : entities) | |
objNames.add(new SelectOption(name,name)); | |
return objNames; | |
} | |
public void btnPerformQuery(){ | |
string queryStr = 'SELECT ID, NAME, createdBy.Name, createdDate, lastModifiedBy.Name, lastModifiedDate, '+fldAPINameMap.get(selectedField)+' FROM '+selectedObject+' WHERE '+fldAPINameMap.get(selectedField)+' like \'%'+criteria+'%\''; | |
selectedFieldAPI = fldAPINameMap.get(selectedField); | |
system.Debug(queryStr); | |
results = database.query(queryStr); | |
System.Debug(results.size()); | |
queried = true; | |
system.debug('Query performed, Queried is: '+queried); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment