function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Anas AlamourAnas Alamour 

Import CSV file in Flow and make some logic

I have requirement to upload csv file by salesforce Admin , the data in this csv file is not realted to one object on sakesfroce and coming from another system, read this file row by row , make some logic for each row , as an example , search of records by email address , if there is lead or contact record with the same email address , create new multiple standard and custom objects (parents and childs) , if no , just update that lead record. 
Can this requirement be implemented by salesforce Flow , if so , please help me how to design that flow ? if not , can that be implemented by salesforce Apex ? 
Prateek Prasoon 25Prateek Prasoon 25
Yes, the requirement to upload a CSV file, read it row by row, and perform logic on each row can be implemented using Salesforce Flow or Apex.
Using Salesforce Flow:
Create a new flow in Salesforce Flow Builder.
Add a "Record Create" element to create new parent and child objects for standard and custom objects as needed.
Add a "Record Update" element to update the existing Lead record if no matching records are found.
Add a "Record Lookup" element to search for Lead or Contact records based on email address.
Use the "Loop" element to iterate through each row in the CSV file.
Add decision elements to check if a Lead or Contact record exists with the same email address, and based on the result, perform the desired logic.
Use the "Import CSV" element to read and parse the CSV file.
Map the fields from the CSV file to the corresponding fields in the objects being created or updated.
Save and activate the flow.
Using Salesforce Apex:
Create an Apex class that implements the logic to read and process the CSV file.
Use the Salesforce Apex CSV Parser library to parse the CSV file and retrieve the data.
Loop through each row in the CSV file and perform the desired logic, such as querying for existing Lead or Contact records, creating new parent and child objects, and updating existing Lead records.
Upload the CSV file using a Salesforce Visualforce page or a Salesforce Lightning Component.
Invoke the Apex class from the Visualforce page or Lightning Component to start the processing of the CSV file.
Save and deploy the Apex class and Visualforce page or Lightning Component.
Create an Apex class that implements the logic to read and process the CSV file. You can use the Salesforce Apex CSV Parser library to parse the CSV file and retrieve the data.
 
public class CSVUploadController {
    public static void processCSVFile(Id parentId, String csvFileBody) {
        // Parse the CSV file and retrieve the data
        List<Map<String, String>> csvData = CSVParser.parseCSV(csvFileBody);
        
        // Loop through each row in the CSV file
        for (Map<String, String> row : csvData) {
            // Retrieve email address from the row
            String email = row.get('Email');
            
            // Query for existing Lead or Contact records with the same email address
            List<Lead> leads = [SELECT Id, Email FROM Lead WHERE Email = :email];
            List<Contact> contacts = [SELECT Id, Email FROM Contact WHERE Email = :email];
            
            if (!leads.isEmpty()) {
                // Update existing Lead record
                Lead lead = leads[0];
                // Perform the desired logic for updating Lead record
                // ...
            } else if (!contacts.isEmpty()) {
                // Create new parent and child objects for custom objects
                // Perform the desired logic for creating custom objects
                // ...
            } else {
                // Create new parent and child objects for standard objects
                // Perform the desired logic for creating standard objects
                // ...
            }
        }
    }
}

Upload the CSV file using a Salesforce Visualforce page or a Salesforce Lightning Component. You can create a Visualforce page with an HTML form to allow Salesforce Admins to upload the CSV file, or you can create a Lightning Component with a file upload component for the same purpose.
Invoke the Apex class from the Visualforce page or Lightning Component to start the processing of the CSV file. Call the processCSVFile method from the Apex class and pass in the parent object's Id and the body of the CSV file as parameters.
<!-- Visualforce Page Example -->
<apex:page controller="CSVUploadController">
    <apex:form enctype="multipart/form-data">
        <apex:inputFile value="{!csvFile}" />
        <apex:commandButton value="Upload CSV" action="{!uploadCSV}" />
    </apex:form>
</apex:page>
<!-- Lightning Component Example -->
<!-- File upload component to upload the CSV file -->
<lightning:input type="file" label="CSV File" accept=".csv" onchange="{!c.handleFileUpload}" />
<!-- Controller method to handle file upload -->
handleFileUpload: function(component, event, helper) {
    var fileInput = component.find("fileInput").getElement();
    var file = fileInput.files[0];
    var reader = new FileReader();
    reader.onload = function() {
        var csvFileBody = reader.result;
        var parentId = // Retrieve the parent object's Id;
        // Call the Apex class to process the CSV file
        var action = component.get("c.processCSVFile");
        action.setParams({ parentId: parentId, csvFileBody: csvFileBody });
        action.setCallback(this, function(response) {
            // Handle response
        });
        $A.enqueueAction(action);
    };
    reader.readAsText(file);
}

If you find my answer helpful, please mark it as the best answer. Thanks!