You need to sign in to do that
Don't have an account?
Michael M
Hello,
My use case is that I am importing csv files through data import wizard to land on a custom object ("Network Statistic"), which has a lookup field to its parent object, namely "Account" (custom field called "Referral Account"). This is an insert (not upsert) import. I need the lookup field to populate for 100% of the records, and therefore if the parent account does not exist, I need my Trigger to add and insert that account, before my Network Statistic record is added. Below is some sample code that doesn't work but may be on the right track...
trigger NetworkStatsTrigger on Network_Statistic__c (before insert) {
Map<String,Network_Statistic__c> hospNumbers = new Map<String,Network_Statistic__c>();
Set<String> presentHospNumber = new Set<String>();
List<Account> accList = new List<Account>();
for(Network_Statistic__c d : Trigger.new){
if(d.Referral_account__c != null && d.Referral_account__r.Hospital_number__c != null){
hospNumbers.put(d.Referral_account__r.Hospital_number__c ,d);
}
}
List<Account> presentAccounts = [SELECT Id,hospital_number__c FROM Account WHERE hospital_Number__c in : hospNumbers.keySet()];
for(Account a : presentAccounts){
presentHospNumber.add(a.hospital_number__c);
}
Set<String> hospNumberToAdd = hospNumbers.keySet();
hospNumberToAdd.removeAll(presentHospNumber);
for(String hospNum : hospNumberToAdd){
Account a = new Account();
a.Name = hospNumbers.get(hospNum).hospital_name__c;
a.AccountNumber = hospNum ;
a.hospital_number__c = hospNum ;
accList.add(a);
}
insert accList;
}
Automatically add and insert parent (lookup) record
Hello,
My use case is that I am importing csv files through data import wizard to land on a custom object ("Network Statistic"), which has a lookup field to its parent object, namely "Account" (custom field called "Referral Account"). This is an insert (not upsert) import. I need the lookup field to populate for 100% of the records, and therefore if the parent account does not exist, I need my Trigger to add and insert that account, before my Network Statistic record is added. Below is some sample code that doesn't work but may be on the right track...
trigger NetworkStatsTrigger on Network_Statistic__c (before insert) {
Map<String,Network_Statistic__c> hospNumbers = new Map<String,Network_Statistic__c>();
Set<String> presentHospNumber = new Set<String>();
List<Account> accList = new List<Account>();
for(Network_Statistic__c d : Trigger.new){
if(d.Referral_account__c != null && d.Referral_account__r.Hospital_number__c != null){
hospNumbers.put(d.Referral_account__r.Hospital_number__c ,d);
}
}
List<Account> presentAccounts = [SELECT Id,hospital_number__c FROM Account WHERE hospital_Number__c in : hospNumbers.keySet()];
for(Account a : presentAccounts){
presentHospNumber.add(a.hospital_number__c);
}
Set<String> hospNumberToAdd = hospNumbers.keySet();
hospNumberToAdd.removeAll(presentHospNumber);
for(String hospNum : hospNumberToAdd){
Account a = new Account();
a.Name = hospNumbers.get(hospNum).hospital_name__c;
a.AccountNumber = hospNum ;
a.hospital_number__c = hospNum ;
accList.add(a);
}
insert accList;
}
All Answers
Please find the attached screen shot of the tigger with the comments
One question, if the Account (Lookup) doesn't exist, where we can find the details to insert a new account like Name etc.
Abdul, Thank you for the comments. How would you restructure it to work properly?
The Account (Lookup) fields are mentioned there at the bottom of the code-- Name, AccountNumber, hospital_number__c. Is that what you are asking?
In case if the account doesn't exist, I am expecting the referral account lookup to be blank and I am required to create that account with those fields information (where I can that from). After Insert I need to update the lookup on the Network_Static__c object (this code also missing in your code above)
Account a = new Account();
a.Name = hospNumbers.get(hospNum).hospital_name__c;
a.AccountNumber = hospNum ;
a.hospital_number__c = hospNum ;
Let me know if this understanding is correct?
So regarding your second comment, yes, the hospital number is unique per account, and is an external id.
Did you try the code?
By the way, this is the code I used (the same you sent me, just substituting the real field names)---
trigger NetworkStatsTrigger on Network_Statistic__c (before insert)
{
Map<String,Network_Statistic__c> hospNumbers = new Map<String,Network_Statistic__c>();
Map<String, Account> accountMap = new Map<String, Account>();
List<Account> accountToInsertList = new List<Account>();
for(Network_Statistic__c ns : Trigger.new)
{
if(ns.Hospital_number__c != null)
hospNumbers.put(ns.Hospital_number__c, ns);
}
if(hospNumbers.isEmpty()) return;
Set<String> hospNumbersSet = hospNumbers.keySet();
for(Account account : [SELECT Id, hospital_number__c FROM Account WHERE hospital_Number__c = :hospNumbersSet])
{
accountMap.put(account.hospital_number__c, account);
}
for(String hostnumber : hospNumbers.keySet())
{
if(!accountMap.containsKey(hostnumber))
{
Account accountToInsert = new Account();
accountToInsert.Name = hospNumbers.get(hostnumber).Hospital_Name__c;
accountToInsert.AccountNumber = hospNumbers.get(hostnumber).Account_Number__c;
accountToInsert.hospital_number__c = hospNumbers.get(hostnumber).Account_Number__c;
accountMap.put(accountToInsert.hospital_number__c, accountToInsert);
accountToInsertList.add(accountToInsert); }
}
if(!accountToInsertList.isEmpty()) Database.insert(accountToInsertList);
for(Network_Statistic__c ns : Trigger.new)
{
ns.Referral_account__c = accountMap.get(ns.Hospital_number__c).Id;
}
}
Here is a sample of the csv:
Here is what seems to be the source of the problem, the Data Import Wizard where I need to choose a field to map the Referral Account field based on:
And here is the code:
trigger NetworkStatsTrigger on Network_Statistic__c (before insert)
{
Map<String,Network_Statistic__c> hospNumbers = new Map<String,Network_Statistic__c>();
Map<String, Account> accountMap = new Map<String, Account>();
List<Account> accountToInsertList = new List<Account>();
for(Network_Statistic__c ns : Trigger.new)
{
if(ns.Hospital_number__c != null)
hospNumbers.put(ns.Hospital_number__c, ns);
}
if(hospNumbers.isEmpty()) return;
Set<String> hospNumbersSet = hospNumbers.keySet();
for(Account account : [SELECT Id, hospital_number__c FROM Account WHERE hospital_Number__c = :hospNumbersSet])
{
accountMap.put(account.hospital_number__c, account);
}
for(String hostnumber : hospNumbers.keySet())
{
if(!accountMap.containsKey(hostnumber))
{
Account accountToInsert = new Account();
accountToInsert.Name = hospNumbers.get(hostnumber).Hospital_Name__c;
accountToInsert.AccountNumber = hospNumbers.get(hostnumber).Account_Number__c;
accountToInsert.hospital_number__c = hospNumbers.get(hostnumber).Account_Number__c;
accountMap.put(accountToInsert.hospital_number__c, accountToInsert);
accountToInsertList.add(accountToInsert); }
}
if(!accountToInsertList.isEmpty()) Database.insert(accountToInsertList);
for(Network_Statistic__c ns : Trigger.new)
{
ns.Referral_account__c = accountMap.get(ns.Hospital_number__c).Id;
}
}
It does! Excellent! Thank you.
If my csv contains some records that already have a parent Account, and some that don't, is the solution to NOT assign that field? What would happen if the parent Account already exists? Would it add a duplicate?
Can you please be kind enough to mark the answer best?