+ Start a Discussion
udayar_jayamudayar_jayam 

Test class for trigger

Hi All,

        

I have create custom User lookup field in Account,Field Name is SalesRep.When i select user name(John) and save the account.  Opportunity Owner name chanege as (john) on Opportunity in the Account

 

I can't deploy it to production because I'm not able to create a test class for it... . The code is below, can someone help me build a test class for it? 

 

trigger Opportunity_SalesRep on Account(after update) {
     List<Id> accIds = new List<Id>(); 
     for(Account acc : trigger.New){
         //check if salesrep changed
         if(acc.EXO_Sales_Rep__c != trigger.oldMap.get(acc.Id).EXO_Sales_Rep__c){
              accIds.add(acc.Id);
         }
     }
    //query all the matching opps
    List<Opportunity > oppList = [SELECT Id,AccountId FROM Opportunity WHERE AccountId IN:accIds];

    for(Opportunity opp : oppList ){
    if(trigger.NewMap.containsKey(opp.AccountId)&&
trigger.NewMap.get(opp.AccountId).EXO_Sales_Rep__c != null){
          opp.OwnerId = trigger.NewMap.get(opp.AccountId).EXO_Sales_Rep__c;
          }
 }
     
 update oppList; 
}

Best Answer chosen by Admin (Salesforce Developers) 
iKnowSFDCiKnowSFDC

Something like the following should work - however, it doesn't provide bulk testing or include other best practices. The book "Advanced Apex Programming for Salesforce.com and Force.com" by Dan Appleman has some great testing solutions.  

 

 

@isTest
private class MyTest {
	
	private static testMethod void myUnitTest(){
		//insert records to test, include the fields you need to test

		Profile p = [select id from profile where name='Standard User']; 
        	User u = new User(alias = 'standt', email='standarduser@testorg.com', 
   			            emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US', 
			            localesidkey='en_US', profileid = p.Id, 
			            timezonesidkey='America/Los_Angeles', username='standarduser@testorg.com');
			insert u;
		
		User x = new User(alias = 'test2', email='standarduser2@testorg.com', 
   			            emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US', 
			            localesidkey='en_US', profileid = p.Id, 
			            timezonesidkey='America/Los_Angeles', username='standarduser2@testorg.com');
			insert x;
		
		Account a = new Account(Name='test', EXO_Sales_Rep__c = u.id);
			insert a;
		
		Opportunity o = new Opportunity(Name='Test', AccountId=a.id, CloseDate=date.today(), StageName='Qualified', Ownerid=u.id);
			insert o;
			

		//update the record that is triggering the change
		a.Exo_Sales_Rep__c = x.id;
		update a;

		//check to see if the change you expect happened
		system.assertEquals(x.id, o.OwnerId);	
	}
}

 

All Answers

iKnowSFDCiKnowSFDC

Something like the following should work - however, it doesn't provide bulk testing or include other best practices. The book "Advanced Apex Programming for Salesforce.com and Force.com" by Dan Appleman has some great testing solutions.  

 

 

@isTest
private class MyTest {
	
	private static testMethod void myUnitTest(){
		//insert records to test, include the fields you need to test

		Profile p = [select id from profile where name='Standard User']; 
        	User u = new User(alias = 'standt', email='standarduser@testorg.com', 
   			            emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US', 
			            localesidkey='en_US', profileid = p.Id, 
			            timezonesidkey='America/Los_Angeles', username='standarduser@testorg.com');
			insert u;
		
		User x = new User(alias = 'test2', email='standarduser2@testorg.com', 
   			            emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US', 
			            localesidkey='en_US', profileid = p.Id, 
			            timezonesidkey='America/Los_Angeles', username='standarduser2@testorg.com');
			insert x;
		
		Account a = new Account(Name='test', EXO_Sales_Rep__c = u.id);
			insert a;
		
		Opportunity o = new Opportunity(Name='Test', AccountId=a.id, CloseDate=date.today(), StageName='Qualified', Ownerid=u.id);
			insert o;
			

		//update the record that is triggering the change
		a.Exo_Sales_Rep__c = x.id;
		update a;

		//check to see if the change you expect happened
		system.assertEquals(x.id, o.OwnerId);	
	}
}

 

This was selected as the best answer
udayar_jayamudayar_jayam

thank u very much joannc