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
Anil KathariAnil Kathari 

need test class for this trigger

trigger ownerupdate on Account (before insert) {
set<id> setAccOwners= new set<id>();

for(Account acc: Trigger.new){
setAccOwners.add(acc.ownerID);
}

Map<id,user> user_map = new map<id,user>([select name from user where id in : setAccOwners]);
for (Account acc: Trigger.new){
user u = user_map.get(acc.ownerID);
 acc.RecordOwner__c = u.name;
}
}
Prosenjit Sarkar 7Prosenjit Sarkar 7
Hi anil, 

When your are testing a trigger,
  1. you need to insert atleast 200+ records to test.
  2. Inside test Class you need to use Test.startTest() and Test.stopTest()
  3. Atleast ONE system.assert();
Please find the test class code, 
 
@isTest
public class OwnerUpdate_TestClass {
    public static testmethod void ownerUpdate()
    {
        List<Account> accList = new List<Account>();
		for(Integer i=0;i<250;i++) {
			accList.add(new Account(Name='Test Account ' + String.valueOf(i)));
		}
		Test.starttest();
		insert accList;
		Test.stopTest();
		System.Assert(accList.size() == 250);
    }
}

Although, I think your requirement can be solved using formula field to capture the Name of the Owner of an Account in a different field.

Thanks, 
Prosenjit

 
Arvind KumarArvind Kumar
Hi Anil,

You have to change in trigger.
1.) Write your line acc.RecordOwner__c = u.Id;  instead of acc.RecordOwner__c = u.name; Because you can put user name in user look up field. You have to put Id in lookup fied

2.) After that, test class will run.

Test Class:
 
@isTest
public class OwnerUpdateTest {
    public static testmethod void ownerUpdate()
    {
        Account accObj = New Account();
        accObj.Name='Test Account';
        insert accObj;
    }

}
If you have any query, please let me know.

Thanks,
Arvind Kumar