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

Help With Test Code Coverage For Account Owner Trigger.
Hello all!
I need to learn how to write a test code coverage for my account trigger, and i've tried many time, but I just can't get my head around the logic. I've read up on so many articles, but I couldn't find any which would apply to my particular scenario.
Here's the trigger:
trigger OnAccountOwnerChanged on Account (before insert, before update) { for( Account a : Trigger.new ) { Account AcctBeforeUpdate = Trigger.oldMap.get( a.Id ); if( AcctBeforeUpdate.OwnerId != a.OwnerId ) { a.Previous_Owner__c = AcctBeforeUpdate.OwnerId; } } }
Can anyone help me with this?
Ed.
Hi,
Yes, the test is written for checking that only. You might be confused by looking at the code where we are inserting the User, but that is required as while writing the test cases, you have to assume that there is no data in Salesforce Database and insert the data. This is essential because when we deploy the application to production, we cannot presume that same data will be present there.
Also if you want to check if your trigger works in correct way; you can use "System.assertEquals" and "System.assertNotEquals" methods. You can find the documention about these two methods in "Apex Language Reference"
All Answers
Hi,
For your logic you will have to first insert the Account with say Owner X in your test code.
eg:
Account objAccount = new Account();
//// Just for simplicity I have written 'xxxxxxxxx' but as a best practice you should never add the direct IDs and you should //query the User. As it is not possible to insert User in Test Method.
objAccount.OwnerId = 'xxxxxxxxx';
Insert objAccount;
Now what you can do is change the owner Id of your Account and update it.
objAccount.OwnerId = 'yyyyyyyy'; //
update objAccount;
Also, before insert scenario for your trigger will never hold.
Hope this will help you. Let me know if you have any issues.
Mandar.
mandar19.kulkarni@gmail.com
Hi Mandar,
Thank you very much for responding. Was beginning to think that no-one else knew how to put together a test code.
I have created this, but because I'm not very experienced at doing this, do you know where I'm going wrong?
I keep getting this error message:
Error: Compile Error: unexpected token: @ at line 1 column 0
I thought that there had to be a @isTest at the top of all test codes?
Please forgive my inexperience with this.
Hi,
Make your class as private.
I've done that, but the error is still there :(
Hi you have one extra closing brace in your code.
Your code should be like as follows:
Hope this will help you!!
You will have to compile it and check out.
Hello again!
Thanks a million for that code. I just want to check that this is going to fulfill what I need to test it for.
The original trigger is there to record who the previous owner of an account was, and is applied each time the Account Owner is changed.
Does the test code cover this logic? I'm not creating a new user, but changing the user of an account to a new owner (a user who already exists in the system). The trigger would then bring me the value of who the previous Account owner was.
Hi,
Yes, the test is written for checking that only. You might be confused by looking at the code where we are inserting the User, but that is required as while writing the test cases, you have to assume that there is no data in Salesforce Database and insert the data. This is essential because when we deploy the application to production, we cannot presume that same data will be present there.
Also if you want to check if your trigger works in correct way; you can use "System.assertEquals" and "System.assertNotEquals" methods. You can find the documention about these two methods in "Apex Language Reference"