-
Notifications
You must be signed in to change notification settings - Fork 0
/
CSVBulkUploadBatch.cls
56 lines (48 loc) · 2.19 KB
/
CSVBulkUploadBatch.cls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
global class CSVBulkUploadBatch implements Database.Batchable<sObject> {
List<Account> accountList;
public CSVBulkUploadBatch(List<Account> accounts) {
accountList = accounts;
system.debug(accounts);
}
global Database.QueryLocator start(Database.BatchableContext BC) {
// You can use a simple SOQL query here to select all the accounts you want to process.
// Modify the query based on your needs.
//return Database.getQueryLocator([SELECT Id, Name, Description FROM Account WHERE Id IN :accountList]);
return Database.getQueryLocator([SELECT Id, Name, Description FROM Account]);
}
global void execute(Database.BatchableContext BC, List<Account> scope) {
List<Account> accountsToUpdate = new List<Account>();
List<Account> accountsToInsert = new List<Account>();
// Loop through the CSV data (accountList)
for (Account csvAccount : accountList) {
// Search for an existing Account record with a matching name in the scope
Boolean accountExists = false;
for (Account acc : scope) {
if (acc.Name == csvAccount.Name) {
// Account with the same name exists, update it
acc.Description = csvAccount.Description;
// Update other fields as necessary
accountsToUpdate.add(acc);
accountExists = true;
break; // Exit the loop once the matching CSV data is found
}
}
// If no existing Account record is found, insert a new one
if (!accountExists) {
accountsToInsert.add(csvAccount);
}
}
// Perform DML operations here to update and insert Account records
if (!accountsToUpdate.isEmpty()) {
update accountsToUpdate; // Update existing accounts
}
if (!accountsToInsert.isEmpty()) {
insert accountsToInsert; // Insert new accounts
}
// If you need to return results, you can do so here, but for this example, we're not returning anything.
}
global void finish(Database.BatchableContext BC) {
// Any post processing if necessary.
// This is where you might send notifications or log batch job completion.
}
}