You need to sign in to do that
Don't have an account?
K_dev
Create accounts while data importing thousands of contacts
Our architecture is that we are storing contact info from a NPPES data base which they will provide monthly 5000 contacts
we need to load into salesforce contacts but immediately we have to create an account with contact firstname,lastname .
And also need to verify if this firstname, lastname already exists must attach the contact to that account if not creat new account.
So how to achieve this any suggestions or code snippets either bulk trigger,batch,@future class which best fits to my
requirement Iam pretty new to development.Please let me know .
Hi,
you can achieve bulk trigger and go through below this code
trigger createacc on Contact (after insert) {
list<Contact > con=[select id,firstname ,lastname from contact where id in:Trigger.newMap.keySet()];
list<account >acc =new list<account>();
list<contact>cc1 =new list<contact>();
for(contact c : con)
{
account a =new account ();
a.name=c.lastname;
acc.add(a);
}
insert acc;
for(account a1:acc){
system.debug('---------------------this is id --------------------'+a1.id);
for(contact c : con){
c.accountid=a1.id;
cc1.add(c);
}
}
update cc1;
}
and if you have any more questions please feel to contact me on support@groundwireconsulting.com
All Answers
If you think you will have less than 5000 contact records loaded you can go with a trigger.
If you think there will be large volume of Contact records you can create a dummy object that has Contact fields, insert all your contacts into the dummy object, create a batch job that executes the below code to create contacts and accounts.
You can have some check box on the holding object to validate if the record has been successfully inserted or not.
Once all the records have been loaded successfully delete the records in the dummy object.
The below logic is for a before insert trigger, if you have to write a batch job use the dummy object instead of Contacts:
//Loop through Contact records and create a Map of the key and Contact record
Map<String,Contact> contMap = new Map<String,Contact>();
Map<String,Account> accMap = new Map<String,Account>();
for(Contact c: newcontacts){
String key = c.FirstName+c.LastName;
contMap.put(key,c);
}
//Retrive all accounts with the matching key and create a map of key and its account
for(Account a: [SELECT name,Id from Account WHERE Name in:contMap.keyset() ]){
accMap.put(a.Name,a);
}
//Loop throught the contact records and identify which contacts do not have accounts
for(String k:contMap.keyset()){
if(!accMap.containskey(k)){
create a new account and add it to the Map
accMap.put(k,newaccount);
}
//Upsert all non existing accounts
Database.upsert(accMap.values());
//Fetch the account id from the account map based on keya dn upsert contacts
for(Contact c: newcontacts){
String key = c.FirstName+c.LastName;
c.AccountId = accMap.get(key).Id;
}
upsert contacts
Hi,
you can achieve bulk trigger and go through below this code
trigger createacc on Contact (after insert) {
list<Contact > con=[select id,firstname ,lastname from contact where id in:Trigger.newMap.keySet()];
list<account >acc =new list<account>();
list<contact>cc1 =new list<contact>();
for(contact c : con)
{
account a =new account ();
a.name=c.lastname;
acc.add(a);
}
insert acc;
for(account a1:acc){
system.debug('---------------------this is id --------------------'+a1.id);
for(contact c : con){
c.accountid=a1.id;
cc1.add(c);
}
}
update cc1;
}
and if you have any more questions please feel to contact me on support@groundwireconsulting.com
How if we have duplicate firstname and lastname of contacts ?
We have an external ID NPI__c which is unique we need to check whether this ID is present or not then create an account.
Your code is not working for bulk insert ?
Iam facing Too many DML rows :1001