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
NewGirlNewGirl 

Need Help with test case....

Hello All - 

 

I am new to SF and I just wrote my first apex trigger.  I believe that in order to deploy the trigger into production I need to write test cases?  How and where do I do this?  I have looked at samples but they seem complex?

 

Any help would be great!

 

Below is my trigger....

Thanks!!!

 

 

 

trigger OrderOwnerUpdate on Opportunity (before insert, before update) {
//change the owner from customer service to RSM
ID TerritoryId;

Opportunity[] opp = Trigger.new;
for (opportunity s:opp)
  {
    if (s.name != null)
        {
            ID OpportunityOwnerId = [Select User_id__c from territories__c 
            where name = :s.opp_ship_to__c].user_id__c;
            s.ownerid = OpportunityOwnerId;
        }
  }

}

Best Answer chosen by Admin (Salesforce Developers) 
Etherios DanEtherios Dan

Here is quick mock up..

 

 

@isTest  
private class orderOwnerUpdateTest{ 
	
	 static testMethod void myUnitTest() {
	 	String name = 'Tester';
	 	String profileName = 'Standard User'; 
	 	String roleName = null;
	 	Integer howMany = 2;
     	List<User> userList = new List<User>();
	 	
	 	// Create a users with
	 	System.debug('------------------------ create Users START');
		
		// declare local vars
     	ID profileId = [select id from Profile where name=:profileName limit 1].id;
     	ID roleId = null;
     	if (roleName != null)
     		roleId = [select id from UserRole where name=:roleName limit 1].id;
     	
     	
     	// build User list
     	for(Integer i=0;i<howMany;i++){
			User u = new User(alias='zyx'+i,username=name+'test'+i+'@advanceddevelopercert.test',
			                  communitynickname=name+'test'+i,emailencodingkey='UTF-8',languagelocalekey='en_US',
			                  localesidkey='en_US', isActive=true,
							  timezonesidkey='America/Los_Angeles',
			                  firstname='testtest'+i,lastname='testtest'+i,userroleid=roleId,
			                  email='test'+i+'@advanceddevelopercert.com',profileid=profileId);
			
			userList.add(u);
     	}
     	// insert users, if there are issues try to grab existing users that fit the profile/role
		try{
			insert userList;
		}catch(DMLException e){
		    userList = [select id,firstname,lastname,profileid,profile.name from User where profileid=:profileId and userroleid=:roleId and isActive=true limit :howMany];
		}		
		System.debug('------------------------ create Users END');
	 	
	 	// create Account
	 	Account acc = new Account(name='test');
	 	try{
	 		insert acc;
		}catch(DMLException e){
		    System.debug('------------------------ something went wrong');
		}
		
		
		// Add a territories__c with the 
		System.debug('------------------------ create Territory START');
		//TODO add the Territory with User_id__c equal to userList.get(1)
		System.debug('------------------------ create Territory END');
		
	 	// create Opportunity
	 	System.debug('------------------------ create Opportunity START');
	 	Opportunity oppty = new Opportunity(
	 			AccountId = acc.Id, 
	 			StageName = 'Prospecting',
	 			Name = 'test',
	 			Owner = userList.get(0),
	 			CloseDate = Date.Today().addDays(30));

	 	System.RunAs(userList.get(0))
        {
            Test.startTest();
            // Calling ValidateAddressService with is test perametter
            try{
			insert oppty;
			System.debug('------------------------ Opportunity id is ' + oppty.Id);
			System.debug('------------------------ Opportunity Owner id is ' + oppty.Owner.Id);
			}catch(DMLException e){
			    System.debug('------------------------ something went wrong');
			}
			// Test the ownerid value
		 	System.debug('------------------------ Opportunity Owner id is ' + oppty.Owner.Id);
		 	System.debug('------------------------ User 1 id is ' + userList.get(0).Id);
			System.debug('------------------------ User 2 id is ' + userList.get(1).Id);
		 	System.assertEquals (userList.get(1).Id, oppty.Owner.Id);
            Test.stopTest();                
        }
	 	System.debug('------------------------ create Opportunity END');
	 }
}

 

Make sure to write the todo for the territories__c  object with the right user Id. 

 

Dan Grandquist
Etherios - Technical Architect

Dan GrandquistEtherios - Technical Architect

 

All Answers

Etherios DanEtherios Dan

Here is quick mock up..

 

 

@isTest  
private class orderOwnerUpdateTest{ 
	
	 static testMethod void myUnitTest() {
	 	String name = 'Tester';
	 	String profileName = 'Standard User'; 
	 	String roleName = null;
	 	Integer howMany = 2;
     	List<User> userList = new List<User>();
	 	
	 	// Create a users with
	 	System.debug('------------------------ create Users START');
		
		// declare local vars
     	ID profileId = [select id from Profile where name=:profileName limit 1].id;
     	ID roleId = null;
     	if (roleName != null)
     		roleId = [select id from UserRole where name=:roleName limit 1].id;
     	
     	
     	// build User list
     	for(Integer i=0;i<howMany;i++){
			User u = new User(alias='zyx'+i,username=name+'test'+i+'@advanceddevelopercert.test',
			                  communitynickname=name+'test'+i,emailencodingkey='UTF-8',languagelocalekey='en_US',
			                  localesidkey='en_US', isActive=true,
							  timezonesidkey='America/Los_Angeles',
			                  firstname='testtest'+i,lastname='testtest'+i,userroleid=roleId,
			                  email='test'+i+'@advanceddevelopercert.com',profileid=profileId);
			
			userList.add(u);
     	}
     	// insert users, if there are issues try to grab existing users that fit the profile/role
		try{
			insert userList;
		}catch(DMLException e){
		    userList = [select id,firstname,lastname,profileid,profile.name from User where profileid=:profileId and userroleid=:roleId and isActive=true limit :howMany];
		}		
		System.debug('------------------------ create Users END');
	 	
	 	// create Account
	 	Account acc = new Account(name='test');
	 	try{
	 		insert acc;
		}catch(DMLException e){
		    System.debug('------------------------ something went wrong');
		}
		
		
		// Add a territories__c with the 
		System.debug('------------------------ create Territory START');
		//TODO add the Territory with User_id__c equal to userList.get(1)
		System.debug('------------------------ create Territory END');
		
	 	// create Opportunity
	 	System.debug('------------------------ create Opportunity START');
	 	Opportunity oppty = new Opportunity(
	 			AccountId = acc.Id, 
	 			StageName = 'Prospecting',
	 			Name = 'test',
	 			Owner = userList.get(0),
	 			CloseDate = Date.Today().addDays(30));

	 	System.RunAs(userList.get(0))
        {
            Test.startTest();
            // Calling ValidateAddressService with is test perametter
            try{
			insert oppty;
			System.debug('------------------------ Opportunity id is ' + oppty.Id);
			System.debug('------------------------ Opportunity Owner id is ' + oppty.Owner.Id);
			}catch(DMLException e){
			    System.debug('------------------------ something went wrong');
			}
			// Test the ownerid value
		 	System.debug('------------------------ Opportunity Owner id is ' + oppty.Owner.Id);
		 	System.debug('------------------------ User 1 id is ' + userList.get(0).Id);
			System.debug('------------------------ User 2 id is ' + userList.get(1).Id);
		 	System.assertEquals (userList.get(1).Id, oppty.Owner.Id);
            Test.stopTest();                
        }
	 	System.debug('------------------------ create Opportunity END');
	 }
}

 

Make sure to write the todo for the territories__c  object with the right user Id. 

 

Dan Grandquist
Etherios - Technical Architect

Dan GrandquistEtherios - Technical Architect

 

This was selected as the best answer
NewGirlNewGirl

THANK YOU!!!!!!