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
DumasRoxDumasRox 

Please help with test class for simple insert/update trigger

This is my first trigger. I am not sure how to do the test class.

Any help would be greatly appreciated!

 

 

trigger setAccountOwnerName on Account (before insert, before update)

{       Map<Id,String> userMap = new Map<Id, String>();

 

        for (User u : [Select Id, Name From User]) {

                userMap.put(u.Id, u.Name);

        }

        for (Account a : Trigger.New) {

               a.Owner_Name__c = userMap.get(a.OwnerId);

        }

}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Cory CowgillCory Cowgill

You need to create a new Apex Class that performs an Account insert DML statement.

 

You need to configure your Account instance properties to match what your setting in the code:

 

For example:

 

@isTest

private class AccountTriggerTest

{

     static testMethod void testAccountInsert()

     {

           Account testAccount = new Account();

          testAccount.Name = 'Unit Test Name';

          insert testAccount;

          Account resultAccount = [Select Id, Name from Account where Id = :testAccount.Id];

          system.assertEquals(testAccount.Name,resultAccount.Name);

     }

}

All Answers

Cory CowgillCory Cowgill

You need to create a new Apex Class that performs an Account insert DML statement.

 

You need to configure your Account instance properties to match what your setting in the code:

 

For example:

 

@isTest

private class AccountTriggerTest

{

     static testMethod void testAccountInsert()

     {

           Account testAccount = new Account();

          testAccount.Name = 'Unit Test Name';

          insert testAccount;

          Account resultAccount = [Select Id, Name from Account where Id = :testAccount.Id];

          system.assertEquals(testAccount.Name,resultAccount.Name);

     }

}

This was selected as the best answer
Cory CowgillCory Cowgill

That should be enough to get you started. Their is a full section about testing apex in the Apex Developer Guide http://www.salesforce.com/us/developer/docs/apexcode/index.htm .

Cory CowgillCory Cowgill

That should be enough to get you started. Their is a full section about testing apex in the Apex Developer Guide http://www.salesforce.com/us/developer/docs/apexcode/index.htm

DumasRoxDumasRox

This is very helpful. Thank you!