+ Start a Discussion
ForceRookieForceRookie 

How to get all fields dynamically based on Object selected?

Help me. What's wrong with the code? I'm trying to get All the Fields of Object.
@AuraEnabled
    public static List<SelectOption> getAllObjectFields(String objectName) {
        List<SelectOption> fieldList = new List<SelectOption>();
        Map <String, Schema.SObjectField> fieldMap = Schema.getGlobalDescribe().get(objectName).getDescribe().fields.getMap();
        for(Schema.SObjectField sfield : fieldMap.values()) {
            if (sfield.getDescribe().isAccessible()) {
                fieldList .add(new SelectOption(sfield.getDescribe().getName(),sfield.getDescribe().getLabel()));
            }
        }
        return fieldList ;
    }

 
Best Answer chosen by ForceRookie
Raj VakatiRaj Vakati
Change apex class as below 
 
@AuraEnabled
    public static List<String> getAllObjectFields(String objectName) {
        List<String> fieldList = new List<String>();
        Map <String, Schema.SObjectField> fieldMap = Schema.getGlobalDescribe().get(objectName).getDescribe().fields.getMap();
        for(Schema.SObjectField sfield : fieldMap.values()) {
            if (sfield.getDescribe().isAccessible()) {
                fieldList.add(sfield.getDescribe().getName()));
            }
        }
        return fieldList ;
    }

Use this js code
 
handleObjChange: function (component, event) {
        var selected = event.getParam("value");
        component.set("v.selectedObj", selected);
       // var objValue = component.get("v.selectedObj");
        //console.log('Selected Obj: ' + objValue);
        var action = component.get("c.getAllObjectFields");
        if (selected != null && selected!='' && selected!=undefined) {
            action.setParams({
                "objectName" : selected  
            });
            action.setCallback(this, function(response){
                var state = response.getState();
                console.log('State: ' + state); // Error here
                if (state === "SUCCESS") {
                    var responseVal = response.getReturnValue();
                    console.log('Field Response: ' + responseVal);
                    component.find('field').set("v.fieldList", responseVal);
                }
            });
            $A.enqueueAction(action);
        }
    },

 

All Answers

Andrew GAndrew G
Looks ok, where is it failing?
Are you using a namespace ?  If so, you will need the namespace in teh objectname e.g. MyDomain__Account
ForceRookieForceRookie
I'm getting an error here
handleObjChange: function (component, event) {
        var selected = event.getParam("value");
        component.set("v.selectedObj", selected);
        var objValue = component.get("v.selectedObj");
        console.log('Selected Obj: ' + objValue);
        var action = component.get("c.getAllObjectFields");
        if (objValue != null && objValue!='' && objValue!=undefined) {
            action.setParams({
                "objectName" : objValue  
            });
            action.setCallback(this, function(response){
                var state = response.getState();
                console.log('State: ' + state); // Error here
                if (state === "SUCCESS") {
                    var responseVal = response.getReturnValue();
                    console.log('Field Response: ' + responseVal);
                    component.find('field').set("v.fieldList", responseVal);
                }
            });
            $A.enqueueAction(action);
        }
    },

 
Raj VakatiRaj Vakati
Change apex class as below 
 
@AuraEnabled
    public static List<String> getAllObjectFields(String objectName) {
        List<String> fieldList = new List<String>();
        Map <String, Schema.SObjectField> fieldMap = Schema.getGlobalDescribe().get(objectName).getDescribe().fields.getMap();
        for(Schema.SObjectField sfield : fieldMap.values()) {
            if (sfield.getDescribe().isAccessible()) {
                fieldList.add(sfield.getDescribe().getName()));
            }
        }
        return fieldList ;
    }

Use this js code
 
handleObjChange: function (component, event) {
        var selected = event.getParam("value");
        component.set("v.selectedObj", selected);
       // var objValue = component.get("v.selectedObj");
        //console.log('Selected Obj: ' + objValue);
        var action = component.get("c.getAllObjectFields");
        if (selected != null && selected!='' && selected!=undefined) {
            action.setParams({
                "objectName" : selected  
            });
            action.setCallback(this, function(response){
                var state = response.getState();
                console.log('State: ' + state); // Error here
                if (state === "SUCCESS") {
                    var responseVal = response.getReturnValue();
                    console.log('Field Response: ' + responseVal);
                    component.find('field').set("v.fieldList", responseVal);
                }
            });
            $A.enqueueAction(action);
        }
    },

 
This was selected as the best answer
Ajay K DubediAjay K Dubedi
Hi ForceRookie,
Try the below code it works for me and it may be helpful for you:
@AuraEnabled
    public static List<String> getAllObjectFields(String objectName){
        List<String> accNameList=new List<String>();
        Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
        Map <String, Schema.SObjectField> fieldMap = schemaMap.get(objectName).getDescribe().fields.getMap();
        for(Schema.SObjectField sfield : fieldMap.Values())
            {
                schema.describefieldresult dfield = sfield.getDescribe();
                if(dfield.isUpdateable()) {
                        accNameList.add(dfield.getLabel ());
                }
            }
        return accNameList;
    }
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
ForceRookieForceRookie
Hi Raj and Ajay, thank you so much! I got it now. I just made a little changes for your answer on Aura Controller.