You need to sign in to do that
Don't have an account?
Minhaj Arifin 5
SOQL Query to Display Field Name, API Name, for all custom fields in the Org
I am trying to write a SOQL query to return the Field Names, API Names for all custom fields in my org.
I have checked the box for Use Tooling API. When I write this:
Select Description, Field Name, Id, from customfield
I get this message: 'only aggregate expressions use field aliasing'
Can you please tell me how I can write this SOQL query correctly?
THanks!
Please try like below code. If this helps you mark it solved.
Thanks
Hemant
Line: 2, Column: 1
Invalid type: ToolingAPI
Try this code:
Component: (GetCObjWithFields.cmp)
<aura:component controller = "GetCustomRecord">
<aura:attribute name = "sObject" type = "EntityDefinition"/>
<aura:attribute name = "objField" type = "List" />
<aura:handler name = "init" value = "{!this}" action = "{!c.doInit}"/>
<div class="slds-form-element">
<div class = "slds-select" >
<lightning:select class = "slds-select" aura:id = "auraId" value = "{!v.sObject.QualifiedApiName}" label = "Select a sObject:" onchange = "{!c.getSobjField}">
<aura:iteration items = "{!v.sObject}" var = "obj">
<option aura:id="sObj">{!obj.QualifiedApiName}</option>
</aura:iteration>
</lightning:select><br/><br/><br/>
<Table class="slds-table slds-table_cell-buffer slds-table_bordered">
<thead>
<tr class="slds-line-height_reset">
<th>
Name
</th>
<th>
Data Type
</th>
<th>
Api Name
</th>
</tr>
</thead>
<aura:iteration items="{!v.objField}" var="ob">
<tbody>
<tr class="slds-hint-parent">
<td>
{!ob.Label}
</td>
<td>
{!ob.DataType}
</td>
<td>
{!ob.QualifiedApiName}
</td>
</tr>
</tbody>
</aura:iteration>
</Table>
</div>
</div>
</aura:component>
Controller: (GetCObjWithFieldsController.js)
({
doInit: function(component, event, helper) {
helper.sObjPickList(component, event, helper);
},
getSobjField: function(component, event, helper) {
helper.sObjFields(component, event, helper);
},
})
Helper: (GetCObjWithFieldsHelper.js)
({
sObjPickList: function(component, event, helper) {
var action = component.get("c.getsObjOptions");
action.setCallback(this, function(response) {
if (response.getState() == "SUCCESS") {
var value = response.getReturnValue();
component.set("v.sObject", value);
}
else{
console.log('error'+response.message);
}
});
$A.enqueueAction(action);
},
sObjFields:function (component,event,helper) {
try{
var action = component.get("c.getFieldOfSobj");
action.setParams({
"strs":component.find("auraId").get("v.value")
});
action.setCallback(this ,function (returnResponse) {
if(returnResponse.getState() == "SUCCESS"){
var returnValue = returnResponse.getReturnValue();
component.set("v.objField",returnValue);
}
else{
console.log('error'+returnResponse.message);
}
});
$A.enqueueAction(action);
}
catch(ex){
console.log('ex.message------->'+ex.message);
}
},
})
ApexClass: (GetCustomRecord.apxc)
public class GetCustomRecord{
@AuraEnabled
public static List <EntityDefinition> getsObjOptions() {
List <EntityDefinition> OldsobjectList = new list <EntityDefinition>();
List <EntityDefinition> sobjectList = new list <EntityDefinition>();
try {
OldsobjectList = [SELECT QualifiedApiName FROM EntityDefinition Where QualifiedApiName LIKE '%_c' Order by QualifiedApiName];
for(EntityDefinition e : OldsobjectList) {
if(string.valueOf(e.QualifiedApiName).endsWith('__c')) {
sobjectList.add(e);
}
}
}
catch(Exception e){
system.debug('e.getLineNumber()------>'+e.getLineNumber()+'msg------>'+e.getMessage());
}
return sobjectList;
}
@AuraEnabled
public static List<FieldDefinition> getFieldOfSobj(String strs){
List<FieldDefinition> fieldDefinitionList = new List<FieldDefinition>();
try{
fieldDefinitionList = [SELECT Label,
DataType,
QualifiedApiName
FROM FieldDefinition
Where EntityDefinition.QualifiedApiName=:strs
];
}
catch(Exception e){
system.debug('e.getLineNumber------->'+e.getLineNumber()+'msg------->'+e.getMessage());
}
return fieldDefinitionList;
}
}
Application: (GetCObjWithFieldsApp.app)
<aura:application extends="force:slds" access="global">
<c:GetCObjWithFields/>
</aura:application>
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi