Notes
HTML
<template>
<lightning-card title="Upload JSON and Insert Accounts">
<div class="slds-p-around_medium">
<lightning-input type="file" label="Upload JSON File"
accept=".json" onchange={handleFileUpload}></lightning-input>
<template if:true={accounts.length}>
<table class="slds-table slds-table_bordered slds-m-top_medium">
<thead>
<tr>
<th>Name</th>
<th>Industry</th>
<th>Phone</th>
</tr>
</thead>
<tbody>
<template for:each={accounts} for:item="acc">
<tr key={acc.Name}>
<td>{acc.Name}</td>
<td>{acc.Industry}</td>
<td>{acc.Phone}</td>
</tr>
</template>
</tbody>
</table>
<lightning-button label="Insert Accounts"
variant="brand" class="slds-m-top_medium"
onclick={handleCreateAccounts}></lightning-button>
</template>
<p if:true={message} class="slds-p-around_medium">{message}</p>
</div>
</lightning-card>
</template>
JavaScript
import { LightningElement, track } from 'lwc';
import createAccounts from '@salesforce/apex/AccountController.createAccounts';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
export default class UploadAccounts extends LightningElement {
@track accounts = [];
@track message = '';
handleFileUpload(event) {
const file = event.target.files[0];
if (file) {
let reader = new FileReader();
reader.onload = (e) => {
try {
this.accounts = JSON.parse(e.target.result);
} catch (error) {
this.dispatchEvent(
new ShowToastEvent({
title: 'Error',
message: 'Invalid JSON file',
variant: 'error'
})
);
}
};
reader.readAsText(file);
}
}
handleCreateAccounts() {
createAccounts({ accountsData: this.accounts }) // Send as List<Map<String, Object>>
.then(result => {
this.message = result;
this.accounts = []; // Clear table after insert
this.dispatchEvent(
new ShowToastEvent({
title: 'Success',
message: result,
variant: 'success'
})
);
})
.catch(error => {
this.message = 'Error: ' + error.body.message;
this.dispatchEvent(
new ShowToastEvent({
title: 'Error',
message: error.body.message,
variant: 'error'
})
);
});
}
}
Apex
public with sharing class AccountController {
@AuraEnabled
public static String createAccounts(List<Map<String, Object>> accountsData) {
try {
List<Account> accountsToInsert = new List<Account>();
for (Map<String, Object> accData : accountsData) {
Account acc = new Account();
if (accData.containsKey('Name')) acc.Name = (String) accData.get('Name');
if (accData.containsKey('Industry')) acc.Industry = (String) accData.get('Industry');
if (accData.containsKey('Phone')) acc.Phone = (String) accData.get('Phone');
accountsToInsert.add(acc);
}
insert accountsToInsert;
return accountsToInsert.size() + ' Accounts created successfully!';
} catch (Exception e) {
throw new AuraHandledException('Error: ' + e.getMessage());
}
}
}
Meta XML
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata" fqn="createAccountForm">
<apiVersion>58.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__AppPage</target>
</targets>
</LightningComponentBundle>
JSON
[
{ "Name": "Acme Corp", "Industry": "Technology", "Phone": "1234567890" },
{ "Name": "Beta Ltd", "Industry": "Finance", "Phone": "2345678901" },
{ "Name": "Gamma Inc", "Industry": "Healthcare", "Phone": "3456789012" },
{ "Name": "Delta LLC", "Industry": "Retail", "Phone": "4567890123" },
{ "Name": "Epsilon Co", "Industry": "Education", "Phone": "5678901234" }
]