You need to sign in to do that
Don't have an account?

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!
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!
- 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
Please check once below links :
http://www.saaspie.com/reading-csv-file-in-apex/
http://sfdcsrini.blogspot.com/2014/04/how-to-read-csv-file-from-apex_17.html
http://www.salesforceadda.com/2017/06/import-csv-file-using-apex-controller.html
Hope it helps you.
Thanks
Varaprasad
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.
- 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
DML requires SObject or SObject list type: Map<String,Account>
Salesforce do not allow to update records from Map. Please update your code to
Thanks,
Gaurav
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