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
Nicolás KacowiczNicolás Kacowicz 

insert account or update

Hello, let's say I have a custom object like Account. I receive a cvs file with the name and other data fields and an external ID.

I want to update if the Account already exists, or create a new Account if it doesn't.

I thought of putting all of the External IDs of the file in a list and then use Set or Map and then do a query to get the Accounts that have these External IDs so I don't do a new Account.

The problem is I don't know where to start :(

Can anyone give me a hand with a simple example?

I don't want to do a query inside the loop because I understand it's a bad practice.

Thanks!
Best Answer chosen by Nicolás Kacowicz
YogeshMoreYogeshMore
Hi,

- If you are using data loader then it is very simple to achieve by upset operation with external Id field.

- If you are going through the codding then it will be like this

1. Let’s say you have CSV data in the List name as csvList and External Id field at column index zero. add this data into the map with External filed as key value and object as a value like account.

Map<String, Account> csvMap = new Map<String, Account>();
for(Integer i=0; i<csvList.size(); i++){
Account acc = new Account();
acc.Name = csvList[1];// map other fields here.
csvMap.put(csvList[0], acc);
}

2. Then query on account object and put the retrieved records into the map.

List<Account> lstAccount = [Select Id, Name, ExternalId from Account Where ExternalId IN : csvMap.keySet()];
Map<String, Account> accountMap = new Map<String, Account>
for(Account acc : lstAccount ){
accountMap.put(acc.ExternalId, acc );
}

3. Then check the records is exist or not. If its exists then map Id to the particular account else create new.

for(String str : csvMap.keySet()){
If(accountMap.containsKey(str)){
csvMap.get(str).Id = accountMap.get(str).Id; // This links to existing account
}
}

4. Then actual DML operation.

If(csvMap.size()>0){
upsert csvMap;
}

Please mark this answer as SOLVED and BEST ANSWER if it helps you.

Regards,
Yogesh More
Salesforce consultant || Salesforce Developer
more.yogesh422@gmail.com || yogeshsfdc11@gmail.com
www.yogeshmore.com || Skype:-yogesh.more44

All Answers

Nicolás KacowiczNicolás Kacowicz

I already have the code to read a csv file. I just want to update if the Account already exists or to insert if it doesn't.
Naveen KNNaveen KN
Not sure if this is the right answer. you can make use of data loader right, when you try to insert the records, if the record is already present then it will update. If not, it will create a new record. 
YogeshMoreYogeshMore
Hi,

- If you are using data loader then it is very simple to achieve by upset operation with external Id field.

- If you are going through the codding then it will be like this

1. Let’s say you have CSV data in the List name as csvList and External Id field at column index zero. add this data into the map with External filed as key value and object as a value like account.

Map<String, Account> csvMap = new Map<String, Account>();
for(Integer i=0; i<csvList.size(); i++){
Account acc = new Account();
acc.Name = csvList[1];// map other fields here.
csvMap.put(csvList[0], acc);
}

2. Then query on account object and put the retrieved records into the map.

List<Account> lstAccount = [Select Id, Name, ExternalId from Account Where ExternalId IN : csvMap.keySet()];
Map<String, Account> accountMap = new Map<String, Account>
for(Account acc : lstAccount ){
accountMap.put(acc.ExternalId, acc );
}

3. Then check the records is exist or not. If its exists then map Id to the particular account else create new.

for(String str : csvMap.keySet()){
If(accountMap.containsKey(str)){
csvMap.get(str).Id = accountMap.get(str).Id; // This links to existing account
}
}

4. Then actual DML operation.

If(csvMap.size()>0){
upsert csvMap;
}

Please mark this answer as SOLVED and BEST ANSWER if it helps you.

Regards,
Yogesh More
Salesforce consultant || Salesforce Developer
more.yogesh422@gmail.com || yogeshsfdc11@gmail.com
www.yogeshmore.com || Skype:-yogesh.more44
This was selected as the best answer
Nicolás KacowiczNicolás Kacowicz
Hi Yogesh, I get an error on the last part of code:

DML requires SObject or SObject list type: Map<String,Account>
GauravGargGauravGarg
Hi Nicolas,

Salesforce do not allow to update records from Map. Please update your code to
update csvMap.values();

Thanks,

Gaurav

YogeshMoreYogeshMore
Hi,
Just change following dml line.
If(csvMap.size()>0){
upsert csvMap.values();
}

Please mark this answer as SOLVED and BEST ANSWER if it helps you.

Regards,
Yogesh More
Salesforce consultant || Salesforce Developer
more.yogesh422@gmail.com || yogeshsfdc11@gmail.com
www.yogeshmore.com || Skype:-yogesh.more44