0

First time post. I've been looking at this for nearly 2 days and can't figure it out.

Objective: Allow users to upload a CSV file with a single column of Account Ids (1). When file is uploaded successfully, display in the same window a list of the Account Names and Ids that were in the file (2). Finally, create a custom record using the Account Ids (3).

Components used: My main component is a Flow Screen. In my Screen component, I've created a LWC that is using the Lightning-file-upload component to achieve (1). In my LWC, I'm importing an Apex Class to extract and format the Blob to List. The returned List is then returned to the Flow for creating the custom records (3).

Problem: When I try to retrieve the Account Names via Apex Class or GetRecords method, the String Array of Ids I pass only returns the last Account record in the array.

What I've tried: GetRecords has been the most work since I'm dynamically setting the RecordIds parameter. I've tried creating and using a ParameterObject. Apex Class, seems to be pretty straight forward so nothing tricky done here. To try and narrow down the problem, I tried setting static Account Ids in an String array and it works both by Apex Class and GetRecords. I suspected that it may be the data type of the List export from my Blob to String conversion (1) but when I execute the javascript TypeOf, I see that it is an 'Object' type which seems consistent with an array so it's not just some long String.

Javascript

import { LightningElement,api,wire } from 'lwc';
import { getRecords } from 'lightning/uiRecordApi';
import NAME_FIELD from '@salesforce/schema/Account.Name';
import getContentVersion from '@salesforce/apex/GetContentVersionDataController.getContentVersion';

export default class MyAccountPlanAssignmentCSVUploader_lwc extends LightningElement {
    @api contentVersionId;
    itemCount;
    listItems=[];
    displayList=false;
    listLabel;
    accountIds=[];
    paramObj;


    handleUpload(event) {
        const uploadFiles = event.detail.files;
        const file = uploadFiles[0];
        this.contentVersionId = file.contentVersionId;
    }


    @wire(getContentVersion, {contentVersionId: '$contentVersionId'})
    wiredContentVersion({error,data}) {
        if (data) {
            let fileContent
            fileContent = [];

            fileContent = data;

            this.listLabel = fileContent[0]; \\Extract Header from file to display in HTML
            this.accountIds = fileContent.slice(1); \\Extract Ids from file
            this.itemCount = this.accountIds.length; \\Count Ids to display in HTML
            
            console.log('Account Id count: ' + this.itemCount);
            console.log('Account Ids: ' + this.accountIds);

            this.displayList = true;

            this.paramObj = []; \\Creating parameter object for getRecords
            this.accountIds.forEach((i) => {
                this.paramObj.push({
                    recordIds: [i],
                    fields: [NAME_FIELD],
                });
            });

            this.paramObj = [...this.paramObj];

            } else if (error) {
                console.error('Error: ', error);
            }
        }
    

    @wire(getRecords, {records: '$paramObj'})
    wiredAccounts({error,data}) {
        if (data) {
            console.log('wiredAccount count: ' + data.results.length);
            data.results.forEach(acct => {
                this.listItems.push({
                    id: acct.result.id,
                    name: acct.result.fields.Name.value
                });
            });

            this.listItems = [...this.listItems];

        } else if (error) {
            console.log('getRecords error: ' + error);
            this.listItems = undefined;
        }
    }
}

EDIT Forgot to add my Apex Class that extracts the strings from the uploaded CSV file.

public with sharing class GetContentVersionDataController {
    @AuraEnabled(cacheable=true)

    public static List<String> getContentVersion(String contentVersionId){

        ContentVersion cv = [
            SELECT VersionData
            FROM ContentVersion
            WHERE Id = :contentVersionId
        ];

        // Converts VersionData (Blob) into String
        String csvAsString = cv.VersionData.toString();

        // Split the CSV content into lines
        List<String> lines = csvAsString.split('\n');

        // Checks for at least one line
        if (!lines.isEmpty()) {
            
            // Considers the first line as the header
            String headerRow = lines[0];

            // Split header line by comma to get individual column names
            List<String> headerColumns = headerRow.split(',');
            
            // Checks to see if there is only one column
            if (headerColumns.size() != 1) {
                system.debug('File Error: CSV file has more than one column.');
            }
        }
        return lines;
    }
}

Question Why is there only 1 record returned when I'm giving 20? I have tried Apex and getRecords with the same results. In the sample I provided I've given the getRecords attempt I made because that is the preferred method I want to pursue and I don't think it's the method I use to retrieve the data. enter image description here

New contributor
com_user is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
3
  • N.B. For future Apex CSV parsing, you can use either Dataweave namespace classes or a more robust CSV parser that can handle embedded line breaks and double quotes (google for examples). Does your input actually have \n as line breaks in the file? Commented 11 hours ago
  • @cropredy I'm not sure if this answers your question. my Apex Class is given the Content Version Id and then I retrieve the Content Version record and convert the Blob to String. I then extract the String to the LWC. Commented 8 hours ago
  • Are you sure your LWC is deploying? I see you using double backslash instead of // for your comments, and that is not valid. Commented 4 hours ago

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.