Access sObject's (Salesforce Object Type) Fields and Its Record | Apex Developer Guide
There are multiple cases where you need to show sObjects records and its field in Lightning. For example: Consider a use case scenario where you need to have a dropdown and there are all sObjects of your organization, you have to select the sObject and show the selected sObject’s fields.
For this, we have to select all the sObject in Apex class.
public static List <sObject> getsAllObjects() { List <sObject> sobjectList = [SELECT QualifiedApiName FROM EntityDefinition Order by QualifiedApiName]; return sobjectList; }
In order to access, the fields of the selected sObject, you have to pass the sObject as the parameter.
public static List<FieldDefinition> getFieldOfSobj(String selectedObject){ List<FieldDefinition> fieldDefinitionList = new List<FieldDefinition>(); fieldDefinitionList = [SELECT Label, DataType, QualifiedApiName FROM FieldDefinition Where EntityDefinition.QualifiedApiName=:selectedObject ]; return fieldDefinitionList; }
Don't forget to check out: How Does a Post Install Script Work? - Salesforce Developer Guide
Lightning Component:
<aura:component controller="selectObjectClass"> <form> <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> Action </th> <th> Name </th> </tr> </thead> <aura:iteration items="{!v.objField}" var="ob"> <tbody> <tr class="slds-hint-parent"> <td> <ui:inputCheckbox aura:id="checkFields" value="" text="{!ob.QualifiedApiName}"/> </td> <td> {!ob.Label} </td> </tr> </tbody> </aura:iteration> <thead> <tr class="slds-line-height_reset"> <th> Action </th> <th> Name </th> </tr> </thead> <aura:iteration items="{!v.objFieldRecords}" var="ob"> <tbody> <tr class="slds-hint-parent"> <td> <ui:inputCheckbox aura:id="checkFields" value="" text="{!ob.QualifiedApiName}"/> </td> <td> {!ob} </td> </tr> </tbody> </table> </form> </aura:component>
Controller:
({ doInit: function(component, event, helper) { helper.selectSobjectt(component, event, helper); }, getSobjField: function(component, event, helper) { helper.Fields(component, event, helper); }, handleCheckBoxClick : function(component, event, helper) { var selectedFields = []; var checkvalue = component.find("checkFields"); //alert("checkvalue"+checkvalue); if(!Array.isArray(checkvalue)){ if (checkvalue.get("v.value") == true) { selectedFields.push(checkvalue.get("v.text")); } }else{ for (var i = 0; i < checkvalue.length; i++) { if (checkvalue[i].get("v.value") == true) { selectedFields.push(checkvalue[i].get("v.text")); } } } var objectName = component.find("auraId").get("v.value"); component.set("v.selectedFieldValues",selectedFields); alert("selectedFields "+selectedFields); alert(component.get("v.selectedFieldValues")); helper.getFieldsRecords(component,event, selectedFields,objectName); }, })
Helper:
selectSobjectt: function(component, event, helper) { var action = component.get("c.getsAllObjects"); 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); }, Fields: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); console.log(returnValue); } else{ console.log('error'+returnResponse.message); } }); $A.enqueueAction(action); } catch(ex){ console.log('ex.message------->'+ex.message); } }, getFieldsRecords:function(component, event, selectedFields,objectName){ var action = component.get("c.getDefaultFieldsRecord"); action.setParams({ "objectName":objectName, "fieldObjects":selectedFields }); alert("selectedFields"+selectedFields); alert("objectName"+objectName); action.setCallback(this,function(response){ var state = response.getState(); if(state == "SUCCESS"){ //alert("1"); // alert("SUCCESS"); console.log(response.getReturnValue()); // Set the value in Contact datatable component.set("v.objFieldRecords", response.getReturnValue()); alert(response.getReturnValue()); console.log(response.getReturnValue()); if(response.getReturnValue().length == 0 && response.getReturnValue()!=null){ alert('There are no records'); } }else{ // alert("ERROR"); console.log("Failed with state: " + state); } }); $A.enqueueAction(action); } })
Check out another amazing blog by Krati here: Salesforce lightning:treegrid - The Developer Guide
Apex Class:
public class selectObjectClass{ @AuraEnabled public static List <sObject> getsAllObjects() { List <sObject> sobjectList = [SELECT QualifiedApiName FROM EntityDefinition Order by QualifiedApiName]; return sobjectList; } @AuraEnabled public static List<FieldDefinition> getFieldOfSobj(String selectedObject){ List<FieldDefinition> fieldDefinitionList = new List<FieldDefinition>(); fieldDefinitionList = [SELECT Label, DataType, QualifiedApiName FROM FieldDefinition Where EntityDefinition.QualifiedApiName=:selectedObject ]; return fieldDefinitionList; } @AuraEnabled public static List<String> getDefaultFieldsRecord(String objectName, list<String> fieldObjList){ String strQuery = ''; String strFieldToQuery = ''; if( fieldObjList!=null){ for(Integer i=0; i < fieldObjList.size()-1;i++){ strFieldToQuery += fieldObjList[i] + ','; } strFieldToQuery += fieldObjList[fieldObjList.size()-1]; strQuery = 'Select ' + strFieldToQuery + ' FROM '+ objectName; } System.debug('strQuery'+strQuery); List<sObject> listOfFieldRecords = Database.query(strQuery); System.debug('listOfFieldRecords'+listOfFieldRecords); List<String> strData = new List<String>(); Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe(); Schema.SobjectType oType = gd.get(ObjectName); for(sObject a: listOfFieldRecords){ sobject object_instance = oType.newSObject(); object_instance = a; for(String singleField:fieldObjList){ strData.add((string)object_instance.get(singleField)); } } System.debug('strData: = '+strData); return strData; } }