Lightning Web Component for Merge Records




In previous post I examine the usability of the flow with all its new features by building flow that allow to merge 2 accounts. The main conclusion was that the flow is very powerful tool and can be used to build complex processes. It does missing flexibility when working with sObject or when want to apply enhance styling to your application.

In this post I decided to try and build similar process with web component-  that is component can be embedded on any record page and allow to merge other record into the current record. 

The full code can be found in this repository

How it will work? The component - mergeRecordsComp - can be placed on any record page. It will show search input where the user can type and search other record. For this purpose I'm using additional custom component - customLookupComp - as Salesforce still doesn't have build-in input for that. 

After the input record was selected popup is opened that shows in table the values from the main record (record were we started the process) and the values from the second record (record that was selected in the search) in the second column.

By default all the values for the merge will be taken from the main record, but user can click on specific fields from the second record that he want to set in the merged record.

At the bottom, user can choose if he want to merge also the child records. 

*For this demo I just added 1 checkbox that used  as merge all child data all  none, but can quite easily add higher flexibility, like getting dynamically all the sObject  related list using ChildRelationship class and allowing  the  user  to choose which  he want merge.  in  apex you can find object related list with the following code:


Schema.DescribeSObjectResult dsr = Schema.getGlobalDescribe().get('Account').getDescribe();

for(Schema.ChildRelationship childRelation : dsr.getChildRelationships()){
    
    System.debug('getChildSObject:: ' + childRelation.getChildSObject());
    System.debug('getRelationshipName:: ' + childRelation.getRelationshipName());
    System.debug('getField:: ' + childRelation.getField());
}

Also note that in the current implementation there is loop on the relation object and query/update on each relevant entity, which is in general bad practice. If you except to have many relation entities you might need to limit the merge relation objects, or call batch process to split the work..


What about merging more than 2 records? In the flow it seems that this will be very cumbersome, as it require too manage flow variables and decisions. In the component it is not that  complicate, just need o provide a way for selecting additional record (can show the search input again or provide button 'add more') and after getting the input record, query the record data and add additional column in the table that compare the data.

Conclusion? 
As expected, writing web component require more skills and efforts compare to the flow, but it does provide higher flexibility and reusability.


No comments:

Post a Comment

Retire of Permission on Profiles

If you are working as a Salesforce admin/developer you've probably heard somewhere that Salesforce is planning to make a significant cha...