Generic Record Handler Lightning Component

Generic Record Handler Lightning Component

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.

Code Time

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

 

Output

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.

Manish Choudhari

I am a certified Salesforce Application & System Architect and Developer working on Salesforce Technology since 2014. Currently, I have 14 Salesforce certifications along with OCPJP (Oracle Certified Profession JavaSE6 Programmer) working in Salesforce.com Hyderabad as a Technical Engineer. Writing technical blogs, learning new technologies and frameworks and sharing knowledge is my hobby.

This Post Has 11 Comments

  1. Avatar

    “Generic Record Handler Lightning Component” that you created is really awesome.
    Thank you for all your useful blogs related to lightning.

  2. Avatar

    Hi,

    Nice!!. Is there any way to update multiple records at a time?
    Thanks,
    Rama

  3. Avatar

    Manish – 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

  4. Avatar

    Hi ,
    Nice! . Is there any way to delete the record?

    1. Avatar

      That is good idea. In this example you cannot delete the record this way, but this can be modified to include delete functionality as well.

  5. Avatar

    i am new to lightning can i get the example to display the selected record detail page from list of records of any object

  6. Avatar

    Hi 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;
    }

Leave a Reply

Close Menu