In this blog, we will develop a generic record handler lightning component, which will be helpful in viewing and editing any record of any object.
This component is developed using very helpful base lightning components, lightning:datatable and lightning:recordForm. It will be helpful in:
- Viewing any object in your Salesforce Org.
- Viewing and editing records of the selected object from the same page. This works for both standard and custom object.
You can replace the datatable used in this example with a paginated datatable, which I have explained in below blogs.
- Lightning Pagination with page number navigation – Using Client Side Controller
- Lightning Pagination – Using Server Side Controller And Client Side Cache
GenericRecordHandler.cmp – Lightning Component
https://gist.github.com/61d24854b73dc7317674ac60d079447b
GenericRecordHandlerController.js – Controller
https://gist.github.com/c582decb9a09f7dd2fddf0729ff3b687
GenericRecordHandlerHelper.js – Helper
https://gist.github.com/27f326ae18f90baad55546edbaed0d61
GenericRecordHandler.apxc – Apex Controller
https://gist.github.com/7c5ee996c8d12d6de156f86a492b8d07
There are lot of modification can be done in this component as per your use case. Like having a paginated data-table, suppying filter in SOQL instead of fetching 200 records etc. Its totally upto you how you would use this component. Paste your query in the comments.
Pratik
20 Nov 2018“Generic Record Handler Lightning Component” that you created is really awesome.
Thank you for all your useful blogs related to lightning.
Rama
20 Nov 2018Hi,
Nice!!. Is there any way to update multiple records at a time?
Thanks,
Rama
Manish Choudhari
21 Nov 2018Hi Rama, with this approach, you cannot edit multiple record at a time, use my other example for the same: http://sfdcfacts.com/lightning/editable-lightningdatatable-summer18-feature/
Hitesh
26 Nov 2018Manish – I am new to SF and ligntning (no Java / CCS). I seen you few lightning vdios and they look good but can you please let me who how to start from begining so that I can learh more objectively and also when I can download the classes material to cpmpare
hitesh.patel.vab@gmail.com
Hare Krishan
Manish Choudhari
26 Nov 2018Hi Hitesh,
I am starting a new series called “Salesforce For Absolute Beginners” where I will explain everything from basics. RSVP here: http://sfdcfacts.com/rsvp-for-live-sessions-salesforce-for-absolute-beginners/
Anji
3 Dec 2018Hi ,
Nice! . Is there any way to delete the record?
Manish Choudhari
4 Dec 2018That is good idea. In this example you cannot delete the record this way, but this can be modified to include delete functionality as well.
lavanya
17 Dec 2018i am new to lightning can i get the example to display the selected record detail page from list of records of any object
Carl Frechette
17 Mar 2019Hi Manish. I love your blog. I’ve been wanting to do this one for a while. Here’s some modifications I just tried to improve upon.
1. It now sorts the Object list by Object name by pulling a List and a Map. The list is sorted first then iterated over to build your objectWrapper list.
2. Sorting by most lastmodifieddate desc, although viewed/referenced could be a better idea.
3. Added a 4th filtering field to filter by record Name containing that String. It filters as you type though.
Modified methods/functions below :
CMP :
<!– Carl : Adding Filter By with LIKE '%%’ on name –>
CMP CTRL :
onObjectSelectionChange : function(component, event, helper) {
var selectedObject = component.find(“objectList”).get(“v.value”);
var nameFilter = component.get(‘v.filter’);
//hide show details component
component.set(“v.showDetails”, false);
//fetch records
helper.getRecordList(component, selectedObject, nameFilter);
},
CMP HELPER :
getRecordList : function(component, selectedObject, nameFilter) {
var action = component.get(“c.getRecords”);
action.setParams({
‘objectName’ : selectedObject,
‘nameFilter’ : nameFilter
});
action.setCallback(this,function(response) {
var state = response.getState();
if (state === “SUCCESS”) {
component.set(“v.records”, response.getReturnValue());
}
});
$A.enqueueAction(action);
}
APX CTRL :
public static List getRecords(String objectName, String nameFilter){
return Database.query(‘SELECT Id, Name FROM ‘+objectName+’ WHERE ‘+’Name’+’ LIKE \’%’+nameFilter+’%\”+’ ORDER BY ‘+’LastModifiedDate’+’ DESC ‘+’LIMIT 200’);
}
public static List getObjects(){
List objectList = new List();
Map sortingWrapperMap = new Map(); // Carl : added for sort
List objResultSort = new List(); // Carl : added for sort
for ( Schema.SObjectType sObjType : Schema.getGlobalDescribe().values() ) {
Schema.DescribeSObjectResult objResult = sObjType.getDescribe();
// objectList.add(new ObjectWrapper(objResult.getName(), objResult.getLabel(), objResult.isCustom()));
sortingWrapperMap.put(objResult.getName(), new ObjectWrapper(objResult.getName(), objResult.getLabel(), objResult.isCustom())); // Carl : added for sort
objResultSort.add(objResult.getName()); // Carl : added for sort. Using name as label could be non-unique for Map
}
objResultSort.sort(); // Carl : added for sort. This Mapping-sort strategy could be generalized, such as for picklist choices per another usage-map
for(String objName : objResultSort){
objectList.add(sortingWrapperMap.get(objName)); // Carl : added for sort
}
return objectList;
}
Manish Choudhari
4 Apr 2019This is great Carl!!
Pingback: MultiSelect Dropdown Aura Component - SFDCFacts