+ Start a Discussion
Minhaj Arifin 5Minhaj 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! 

Hemant_SoniHemant_Soni
Hi,
Please try like below code.
// Constructs the Tooling API wrapper (default constructor uses user session Id)
ToolingAPI toolingAPI = new ToolingAPI();
 
// Query CustomObject object by DeveloperName (note no __c suffix required)
List<ToolingAPI.CustomObject> customObjects = (List<ToolingAPI.CustomObject>)
     toolingAPI.query('Select Id, DeveloperName, NamespacePrefix From CustomObject Where DeveloperName = \'Test\'').records;
 
// Query CustomField object by TableEnumOrId (use CustomObject Id not name for Custom Objects)
ToolingAPI.CustomObject customObject = customObjects[0];
Id customObjectId = customObject.Id;
List<ToolingAPI.CustomField> customFields = (List<ToolingAPI.CustomField>)
     toolingAPI.query('Select Id, DeveloperName, NamespacePrefix, TableEnumOrId From CustomField Where TableEnumOrId = \'' + customObjectId + '\'').records;
 
// Dump field names (reapply the __c suffix) and their Id's
System.debug(customObject.DeveloperName + '__c : ' + customObject.Id);
for(ToolingAPI.CustomField customField : customFields)
     System.debug(
          customObject.DeveloperName + '__c.' +
          customField.DeveloperName + '__c : ' +
          customField.Id);
If this helps you mark it solved.
Thanks
Hemant
 
Minhaj Arifin 5Minhaj Arifin 5
Thanks Hemant, I will try this and update you. 
Kyle E. HandleyKyle E. Handley
Execute Anonymous Error
Line: 2, Column: 1
Invalid type: ToolingAPI
Ajay K DubediAjay K Dubedi
Hi Minhaj,
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