function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Trinityed78Trinityed78 

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.

 

Best Answer chosen by Admin (Salesforce Developers) 
MandyKoolMandyKool

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

MandyKoolMandyKool

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

Trinityed78Trinityed78

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? 

 

 

@isTest
public class TestOnAccountOwnerChanged {

    static testMethod void myUnitTest() {
       Account objAccount = new Account();
       
        //// Owner attached to account.
        objAccount.OwnerId = 'xxxxxxxxx'; 
        Insert objAccount;
 
        //// Owner is changed and record is updated.
 
        objAccount.OwnerId = 'yyyyyyyy'; //
        update objAccount;
        }
    }
}

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. 

 

MandyKoolMandyKool

Hi,

 

Make your class as private.

 

Trinityed78Trinityed78

I've done that, but the error is still there :(

MandyKoolMandyKool

Hi you have one extra closing brace in your code.

 

 

Your code should be like as follows:

 

 

@isTest
private class TestOnAccountOwnerChanged 
{
    static testMethod void myUnitTest() {
    Account objAccount = new Account();
       
    Map<String,ID> profiles = new Map<String,ID>();
    List<Profile> ps = [select id, name from Profile where name = 'Standard User'
                        or name ='System Administrator'];
    for(Profile p : ps)
      profiles.put(p.name, p.id);

    // Create the users to be used in this test.
    // First make a new user. 
    
    User standard = new User(alias ='standt', 
    email='standarduser@testorg.com', 
    emailencodingkey='UTF-8', 
    lastname='Testing', languagelocalekey='en_US', 
    localesidkey='en_US', 
    profileid = profiles.get('Standard User'), 
    timezonesidkey='America/Los_Angeles', 
    username='standarduser@testorg.com');
    insert standard;

    //// Owner attached to account.
    objAccount.OwnerId = standard.Id; 
    Insert objAccount;
 
    User standard = new User(alias ='admin', 
    email='adminuser@testorg.com', 
    emailencodingkey='UTF-8', 
    lastname='Testing', languagelocalekey='en_US', 
    localesidkey='en_US', 
    profileid = profiles.get('System Administrator'), 
    timezonesidkey='America/Los_Angeles', 
    username='adminuser@testorg.com');
    insert Admin;

    //// Owner is changed and record is updated.
    objAccount.OwnerId = Admin.Id; //
    update objAccount;
    }
}

 


Hope this will help you!!

You will have to compile it and check out. 

Trinityed78Trinityed78

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.

MandyKoolMandyKool

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"

This was selected as the best answer