Skip to content

Instantly share code, notes, and snippets.

@choudharymanish8585
Created November 18, 2018 09:12
Show Gist options
  • Save choudharymanish8585/7c5ee996c8d12d6de156f86a492b8d07 to your computer and use it in GitHub Desktop.
Save choudharymanish8585/7c5ee996c8d12d6de156f86a492b8d07 to your computer and use it in GitHub Desktop.
/**
* Controller class for GenericRecordHandler Lightning Component
* Contains method to retrieve objects and records
* @author - Manish Choudhari
* */
public class GenericRecordHandler {
// This method retrieves all object list from Salesforce Org
// You can filter out object list here, as per you use case
@AuraEnabled(cacheable=true)
public static List<ObjectWrapper> getObjects(){
List<ObjectWrapper> objectList = new List<ObjectWrapper>();
for ( Schema.SObjectType sObjType : Schema.getGlobalDescribe().values() ) {
Schema.DescribeSObjectResult objResult = sObjType.getDescribe();
objectList.add(new ObjectWrapper(objResult.getName(), objResult.getLabel(), objResult.isCustom()));
}
return objectList;
}
// Get record from a specific object
// Here I am simply retrieving 200 records without any filteration
// but you may want to put some filter in your query
@AuraEnabled(cacheable=true)
public static List<sObject> getRecords(String objectName){
return Database.query('SELECT Id, Name FROM '+objectName+' LIMIT 200');
}
//Object wrapper class having object properties like name, label and isCustom
public class ObjectWrapper{
@AuraEnabled public String objectName;
@AuraEnabled public String objectLabel;
@AuraEnabled public Boolean isCustom;
ObjectWrapper(String objectName, String objectLabel, Boolean isCustom){
this.objectName = objectName;
this.objectLabel = objectLabel;
this.isCustom = isCustom;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment