You need to sign in to do that
Don't have an account?
Simple Contact Creation from Account Trigger
I'm having issues writing a trigger that will insert a Contact record from information based on the Account.
Basically, if it's an account is inserted, I want it to create a Contact. That part is easy. The next part that I am having trouble writing is if the account is updated part. If it's an update, I want it to look to see if there are any Contacts with the Contact_Type__c = General. If not, then it needs to create a Contact like in the insert portion. Thought it was simple enough but I can't get it work correctly. I also need to make sure that this bulk safe since we load accounts nightly.
I marked the line it's failing on in red.
trigger CreateAccountContact on Account (after insert, after update){ if(Trigger.isInsert){ List<Contact> ct = new List <Contact>(); for(Account acc : trigger.new){ Contact c = new Contact(LastName = acc.name, AccountId=acc.id, Fax=acc.Fax, MailingStreet=acc.BillingStreet, MailingCity=acc.BillingCity, MailingState=acc.BillingState, MailingPostalCode=acc.BillingPostalCode, MailingCountry=acc.BillingCountry, Phone=acc.Phone); ct.add(c); } if(!ct.isEmpty()) insert ct; } else{ List<contact> cntsload = new List <Contact>(); for(Account acc : trigger.new){ List<Contact> cnt = new List<Contact>([select id, Contact_Type__c from Contact where AccountId = acc.id and Contact_Type__c = 'General']); if(cnt.isEmpty()){ Contact c = new Contact(LastName = acc.name, AccountId=acc.id, Fax=acc.Fax, MailingStreet=acc.BillingStreet, MailingCity=acc.BillingCity, MailingState=acc.BillingState, MailingPostalCode=acc.BillingPostalCode, MailingCountry=acc.BillingCountry, Phone=acc.Phone); cntsload.add(c); } } if(!cntsload.isEmpty()) insert cntsload; } }
What is the error you are getting?
To bulkify you just need to do the following.
Set<Id> acctIds = new Set<Id>{};
Map<Id, Contact> contactmap = new Map<Id, Contact>{};
for(Account acc : trigger.new){
acctIds.add( acc.id);
contactmap.put( acc.id, acc);
}
for ( Contact cnt : [select id, Contact_Type__c, AccountId from Contact where AccountId in :acctIds and Contact_Type__c = 'General']){
Account acc = contactmap.get(cnt);
Contact c = new Contact(LastName = acc.name,
AccountId=acc.id,
Fax=acc.Fax,
MailingStreet=acc.BillingStreet,
MailingCity=acc.BillingCity,
MailingState=acc.BillingState,
MailingPostalCode=acc.BillingPostalCode,
MailingCountry=acc.BillingCountry,
Phone=acc.Phone);
cntsload.add(c);
}
this is the error that I am getting:
Add this line to your code
List<contact> cntsload=mew List<contact>();
---
---
change cnt.add(c); to cntsload.add(c);
insert cnt; to insert cnt;sload
Thanks, that fixed that error! Now I am getting another one though. Here is the error:
Compile Error: Incompatible value type SOBJECT:Account for MAP<Id,Contact> at line 37 column 15 (marked line in red below)
I also had another question, if the account is being updated, how would work if I wanted to insert the contact if it doesn't exist or if the contact does exist, update the information from the account? Could I just change the insert at the bottom to an upsert to get to work?
contactmap.put(acc.id, acc);
change the map from <Id, Contact> to <Id, Account>. My bad
Thanks! I also made some other changes too (it didn't like the key type on this line: Account acc = contactmap.get(cnt.AccountId); ), so I changed it. I got it to save and it works. I am having issues with it creating duplicates.
So if there is a contact with the type of General, don't create a contact. If there isn't a contact with the type of General, create the contact.
I made some changes to this since it wasn't working correctly. Would anyone be able to take a look at the changes I made? I was able to change the code to insert and to update when it already existed. I'm having issues now with inserting the contact record on already existing accounts.
Basically in the bottom part, in the first half of the update, I am looking to see if a Contact with Contact_Type__c = General exists. If so, then don't create and add the id the contacts to update portion. if a Contact with Contact_Type__c = General doesn't exist, then insert. I thought i could use the isEmpty(), but then it says the variable c doesn't exist on the red line below.
I tried using this code.... it worked perfectly fine but contact created for that particular account is not visible in account related list.
it is getting saved in contact but not visible on related list of account.
please help.