You need to sign in to do that
Don't have an account?
wickham
Making a contact owner same as account owner when creating a contact record
We have a requirement in our organization which the contact owner needs to be the same as the contact.
I've tested the triger below in our sandbox edition and it seems to be working fine. The probem is when I upload the package there's no test methods.
From my understanding you can't write test methods in a trigger and need it needs to be moved it to a class. I've tried copying the trigger to a class and none of the test methods seem to be working.
This is the first time I'm attempting this, I've tried reading Chapter 7 in the Apex Guide and I've tried the examples there and it doesn't seem to be working.
Does anyone have a suggestion on a test method for my trigger. Here's the example.
trigger ChangeOwner on Contact (before insert) { Contact[] cons = Trigger.new; for(Contact c:cons) { String ownerId = [select ownerId from Account where Id =: c.accountId].ownerId; System.debug('Query Success-->'+ownerId); c.ownerId = ownerId; System.debug('Owner set Success-->'+c.ownerId); } }
You don't put the trigger code in the class, from the class you write testMethods that call your trigger.
Example:
write a testMethod that creates an account, then creates a contact under that account.
Then check to see if the owners match.
Also the way you have your trigger written you are not running it in bulk.
Please review this post.