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

Help with Test Class
Hi everyone,
I found a trigger online and modified it slightly. It simply edit/saves the account whenever I modify a related contact. Workflows and triggers get fired once the account has been modified afterwards.
However, I am having trouble with creating the test class. To be honest I always have issues with the "after insert, after update" test classes.
Can you guide me in the right direction to get the test class sorted? I checked the docs but if someone got a sample test class for after insert, after update, that would be great.
Here is the code:
trigger UpdateAccount on Contact (after update,after insert) {
Contact [] con;
con= Trigger.new;
set<Id> accIds = new Set <ID>();
for (Contact ir : con) {
accIds.add(ir.AccountId);
Map <ID, Contact > irforaccs = new Map <ID, Contact > (
[select Id, accountId from Contact
where accountId in :accIds]);
Map<ID, Account> accsToUpdate = new Map<ID, Account> (
[select Id from Account
where Id in :accIds]);
for (Account acc : accsToUpdate.values()) {
acc.id = acc.id ;
update acc;
}}}
If you believe you have an easier solution, then please let me know!
Your help is very much appreciated!
Thanks in advance,
Christian
Hi Christian,
Please accept this first example of only 1 primary contact per account. It's a good example for starters. I will post your requirement of multiple contacts per account shorty. Please do look at the test class now as that will not change.
The Trigger Handler
The Trigger
The Test Class
All Answers
Hi Christian,
Would you mind telling me what your main outcome for the trigger is?
Eg. I am wanting to update the account which is assigned to a contract with ....
I apologise but I am unable to make out what you are trying to acheive. Please post here what you're trying to achieve and i'll submit a test class for you and complete the below classes.
Best practice for triggers is to use a handler class to perform your requirements.
1. Create your trigger handler class by going to Your Name | Setup | Apex Classes | New
2. Create your trigger by going to Your Name | Setup | Customize | Contacts | Triggers
Just to confirm, "edit/saves the account whenever I modify a related contact"
Would you mind telling me why you are wanting to UPDATE the account record when a contact is modified? So i know the best solution to provide you.
Hi,
Sure let me provide you with more information.
In one of my previous posts I mentioned the scenario already. We retrieve all email addresses from the related "primary" contacts into a txt box on the account. That's needed as we want to email them all at once when a ticket is created. This works all fine.
If someone makes a contact a "primary" contact, then the accounts should get updated so that the trigger on the account fires again pulling in the email address from the primary contacts.
Basically I just wanna close the loop so that nobody has to go back to the account and edit/saves it to simply retrieve the emails as it would be an additional (and to me pointless) step the account manager would have to do.
Hope that helps.
Thanks,
Christian
So just to confirm, would a single account have 1 primary contact only? or could a single account have more than 1 primary contact that you wish to email at the same time?
An account can have many primary contacts
Hi Christian,
Please accept this first example of only 1 primary contact per account. It's a good example for starters. I will post your requirement of multiple contacts per account shorty. Please do look at the test class now as that will not change.
The Trigger Handler
The Trigger
The Test Class
Thanks for that! Thanks as well for the comments on the test class! The trigger and class I could insert. If I try to insert the test class though I get an error stating:
Error: Compile Error: Invalid bind expression type of SOBJECT:Contact for column of type Id at line 51 column 108
Any idea?
Oh silly me.. try this new LINE (replacing Is_A_Primary_Contact__c with your own custom field of course)
Hi Christian,
Please try the following code for a multiple primary contact example. The trigger and test class remain the same. I'm sure somone else might have a better way of doing this, please feel free to post if you do :-)
Let me know how you go.
That works like a charm! Thank you so much for sorting this out and providing me with the additional information!!
Have a great day and thank you again!
Christian
Your welcome Christian :-)