Notes
HTML
<template>
<lightning-card title="Sample Accounts">
<div class="slds-p-around_medium">
<table class="slds-table slds-table_bordered">
<thead>
<tr>
<th>Name</th>
<th>Industry</th>
<th>Phone</th>
</tr>
</thead>
<tbody>
<template for:each={sampleAccounts} for:item="acc">
<tr key={acc.Id}>
<td>{acc.Name}</td>
<td>{acc.Industry}</td>
<td>{acc.Phone}</td>
</tr>
</template>
</tbody>
</table>
<lightning-button label="Create Sample Accounts" onclick={handleCreateAccounts}
variant="brand" class="slds-m-top_medium"></lightning-button>
</div>
<p if:true={message} class="slds-p-around_medium">{message}</p>
</lightning-card>
</template>
JavaScript
import { LightningElement, track } from 'lwc';
import createAccounts from '@salesforce/apex/AccountController.createAccounts';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
export default class SampleAccounts extends LightningElement {
@track sampleAccounts = [
{ 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' }
];
@track message = '';
handleCreateAccounts() {
createAccounts({ accountsData: this.sampleAccounts }) // Sending list of maps
.then(result => {
this.message = result;
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>