Clone Record in Bulk Using Flow

Flow is something which I mostly ignored till now, recently I created few flow in my project and found them very interesting and easy to develop to handle even complex functionality. So I thought to try my hands on Flow to develop some simple components which will be helpful in daily use.
One of such requirement is Create Data for Testing in sandbox or in dev org. As a developer we have many options but Admin have limited scope. If they need to do bulk testing then they have external help with data creation or they need to spend long time to clone record using CSV and then upload or in worst case need to create manually. So Today we will create a simple component using which they can Clone Record in Bulk using Flow.

I have created a basic apex class for this which will perform required DML operations.

public class GenericFlowHelper {
	@InvocableMethod(label='create records' description='Create records.')
    public static List<FlowWrapper> createRecords(List<FlowWrapper> flowData) {
        List<FlowWrapper> fwList = new List<FlowWrapper>();
        Set<ID> recordIDSet = new Set<Id>();
        for(FlowWrapper fw : flowData)
            recordIDSet.add(fw.recordId);
        String sObjectName = flowData[0].recordId.getsobjecttype().getDescribe().getName();
        Map<String,Schema.SObjectField> objfields = Schema.getGlobalDescribe().get(sObjectName).getDescribe().fields.getMap();
        String query = 'SELECT ';
        query += String.join(new List<String>(objfields.KeySet()),',');
        query += ' FROM '+sObjectName+' WHERE ID IN : recordIDSet';
        List<sObject> sobjList = new List<sObject>();
        Integer currentRecord = 0;
        
        for(Sobject sob : Database.query(query)) {
            FlowWrapper fw = new FlowWrapper();
            for(Integer co = 1 ; co<= flowData[currentRecord].count; co++) {
                Sobject newObj = sob.clone(false, true, false, false);
                sobjList.add(newObj);
            }
            fw.sObjectList = sobjList;
            fw.isSuccess = true;
            fw.recordId = sob.Id;
            fw.count = flowData[currentRecord].count;
            fwList.add(fw);
            currentRecord++;
        }
        
        insert sobjList;
        return fwList;
    }
    
    public class FlowWrapper {
        @InvocableVariable(required=true)
        Public Id recordId;
        @InvocableVariable(required=true)
        Public Integer count;
	@InvocableVariable
        Public List<sObject> sObjectList;
	@InvocableVariable
        Public boolean isSuccess;        
    }
}

Code Flow:

In this class I will get RecordId and number of records they want to create, using these details with Apex describe I have made dynamic query and clone the records using clone method.
If you have noticed I have used Generic sObject as attribute, With Spring 20 now it is supported to use Generic sObject as Parameters, so we can use this component with any suppoorted sObject type. Although I haven’t added much validation so if you want you can add more checks in code and can play with it.

Now the Flow is very simple for this.

Clone Record in Bulk Using Flow

So in first screen we are getting Inputs from users and Sending them in Apex using Action. So using just two steps we can Clone Record in Bulk Using Flow.
This is how our Final UI will look like

Clone Record in Bulk Using Flow

I have created one package, so you can easily install this in your orgs. https://login.salesforce.com/packaging/installPackage.apexp?p0=04t6F0000045Zk0&isdtp=p1
This component is fully Dynamic and can be used with any supported sObject.

Now you can officially use URL Hacks in Lightning Experience: Check here

I am exploring flow, so if you want me to implment some use case using flow, let me know in comments.In future I will share similar components using Flow. Happy Programming 🙂

Advertisements

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.