You need to sign in to do that
Don't have an account?
Rung41
Help with insert trigger after update
I have create 2 trigger to insert a Contact when a new Account record is created andupdate a Contact's Email if a custom field (Company Email) on the Account record is changed. I am not sure how to add a new contact if the Account record already exists and Company Email feild was previously "null" and then update with an email address.
Insert Trigger
trigger CompanyEmail on Account (after insert) { List<Contact> ct = new List <Contact>(); for (Account newAccount: Trigger.New){ if (newAccount.Company_Email__c != null){ ct.add (new Contact( FirstName = 'Account', LastName = 'Email', Email = newAccount.Company_Email__c, AccountId = newAccount.id, OwnerId = '00530000001jaC1')); } insert ct; }}
Update trigger:
trigger TriggerWithUpdateEmail on Account (after update) { Integer index = 0; for(Account acc : Trigger.New) { //Check entry criteria if(acc.Company_Email__c != Trigger.Old[index].Company_Email__c) { List<Contact> listCon = [Select Email from Contact where (AccountId =: acc.id) and (FirstName = 'Account')]; for(Contact con : listCon) { con.Email = acc.Company_Email__c; } update listCon; } index++; } }
I updated the trigger to cover the condition, check if it helps.
FYI: I have also avoided a double for loop, since it drastically increases the script statements as it there is a limit on number of script statements.
All Answers
So in your After Update trigger, this comes into play inside of your "Check Entry Criteria" block. The code you have there currently should be inside an if statement, and the Else will be for the case you're trying to cover (Company_Email__c used to be null but it isn't now).
Warning: your After Update trigger is querying once for every record. If you insert too many Accounts, you will hit governor limits. Better to collect Account IDs in a set and make one large query with an ID map than a bunch of individual ones.
Jeremy
Follow the best practices by bulkifying your records.
* Never update or query inside a for loop.
I didn't actually get what you are trying to do,
as per my understanding i think you want to update the contact's email only if Company Email of Account is changed.
Also you can club both triggers in one and pushing your logic inside event isInsert and isUpdate.
I modified the code for you:
Let me know if you face any problem.
Thanks for the reply and code modification. I knew there was a way to combine triggers..just not so good at it yet.
What I am trying to do is the following:
I updated the trigger to cover the condition, check if it helps.
FYI: I have also avoided a double for loop, since it drastically increases the script statements as it there is a limit on number of script statements.
Thanks for the help...I have started writting a test method for the trigger but once again I am stuck at the "update portion". If you could point me in the right direction, I would greatly appreciate it. On a side note...I did sign up for Apex class in hopes to learn and instead of asking.
Hi Rung41,
Good to know that you would join the classes.
I have modified the test method to cover the trigger. check if it helps, if not let me know what didn't worked.