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
Hammad ShahHammad Shah 

Need Help Writing Test Class on Update Trigger

Hello All,

 

I am having a hard time writing a test class to deploy the following Trigger.

 

 

 

trigger PA_Update_Percentage_Trigger on Productivity_Allocations__c (before update) {

String errmsg = '';
Boolean isError = false;

Productivity_Allocations__c NewPA = Trigger.new[0];
Productivity_Allocations__c OldPA = Trigger.old[0];

Decimal Percent3 = 0.00;
	if(OldPA.Allocation_Percentage__c== null){
	Percent3 = NewPA.Allocation_Percentage__c;
		}
		else
			{Percent3 = NewPA.Allocation_Percentage__c - OldPA.Allocation_Percentage__c;
			}	
			
Opportunity RelatedOp = [Select Id, Opportunity_Percentage_Allocated__c, OwnerId
					from Opportunity
					where Id =: NewPA.Opportunity_Name__c Limit 1]; 
					Percent3 = Percent3 + RelatedOp.Opportunity_Percentage_Allocated__c;
	if (Percent3 > 100.00){
	errmsg += 'Total Productivity Allocation percent is currently '+Percent3+'% The Total Allocation percentage cannot be greater than 100%';
	isError = true;
	NewPA.addError(errmsg);
							}

}

 

 

 

Please bare with me as I am mainly an admin who is trying to do some development work. I have done a lot of research on the boards along with the force.com cookbook to get the correct syntax, etc. I have many different version of the test classes I have written for this trigger. However I cant seem to get it correct. Any help would be greatly appreciated.

 

Regards,

Hammad Shah

Best Answer chosen by Admin (Salesforce Developers) 
Imran MohammedImran Mohammed

Hi,

 

Add this method to your test class code.

static testMethod void triggerTest()

{

 

Opportunity[] oList = [select id, name, field1, field2 from Opportunity limit 1];

Productivity_Allocations__c pa = new Productivity_Allocations__c(Opportunity_Name__c = oList[0].id, assign other fields);

insert pa;

 

Productivity_Allocations__c[] paList = [select id, Allocation_Percentage__c, Opportunity_Name__c from Productivity_Allocations__c where id = :pa.id];

 

//for example update allocation percentage

paList[0].Allocation_Percentage__c = assign some value here;

update paList;

 

}

 

Let me know if any issues faced.

All Answers

Imran MohammedImran Mohammed

Hi,

 

Add this method to your test class code.

static testMethod void triggerTest()

{

 

Opportunity[] oList = [select id, name, field1, field2 from Opportunity limit 1];

Productivity_Allocations__c pa = new Productivity_Allocations__c(Opportunity_Name__c = oList[0].id, assign other fields);

insert pa;

 

Productivity_Allocations__c[] paList = [select id, Allocation_Percentage__c, Opportunity_Name__c from Productivity_Allocations__c where id = :pa.id];

 

//for example update allocation percentage

paList[0].Allocation_Percentage__c = assign some value here;

update paList;

 

}

 

Let me know if any issues faced.

This was selected as the best answer
sh-at-youseesh-at-yousee

Hi,

 

The Force.com platform maintains a strict test coverage policy to ensure all code deployed into the production environment is (somewhat) tested. This is done using code analysis.

 

Now, in the ideal world you would not only have an overall test coverage of 100% but you would also validate the outcome of your test methods using the System.assert methods. However, it is possible to achieve 100% test coverage without actually validating the outcome (not a good idea but possible).

 

I haven't written a test class for your specific trigger but tried to leave you with an example and some considerations. I hope you find your way to successful testing from there.

 

Assume I have a trigger on my opportunity object which updates a checkbox field named requiresApproval based on the amount field. If the amount is greater than 125.000 the checkbox is checked and if not the checkbox is unchecked. My test method could the look something like this:

 

 

static testMethod void testRequiresApproval() {
  // Create test data - should be done before startTest (not a requirement - just good practice according to the docs)

  Opportunity o = new Opportunity();
  o.Amount = 10000;
  o.requiresApproval = false;

  // Mark beginning of test
  Test.startTest();

  // Insert test opportunity
  insert o;

  // Update test opportunity
  o.Amount = 300000;
  update o;

  // Validate outcome of update (not required but _very_ good practice)
  System.assert(o.requiresApproval);

  // Mark end of test
  Test.stopTest();
}

 

 

Now, you could do without the 'System.assert' line and still get 100% code coverage (which would allow you to deploy your code to production). However, it is more than just good testing practice to include 'sanity checks' in your tests. It also serves an insurance to yourself. How else will you know if you (or someone else) introduced some new code that suddenly breaks your existing code? Without it you'd still have a 100% code coverage (or slightly less but still above the limit) and you'd assume everything works fine even though the outcome may now be different.

 

I hope this helps and should anyone have something to add please feel free to do so. I'm always keen on learning more about testing as I consider it a vital part of being a developer.

 

/Søren Nødskov Hansen

Imran MohammedImran Mohammed

Yes, i totally agree with Hansen.

You have to assertions to validate all DML operations that are done.

And it is enough to have atleast 75% code coverage but one should always target for 100%.

 

Hammad ShahHammad Shah

Hello Imran,

 

Thanks for the help so far. I am running into an error. I have modified your outline with the fields I am interested in and the code looks as follows. I am getting an issue on line 12 "

Opportunity_Name__c = oList[].Id ,  The error reads "- expecting a right parentheses, found '['"

 

@isTest
private class PA_Update_Percentage_Test_Class {

  static testMethod void triggerTest() {
        // TO DO: implement unit test
        
Opportunity <> oList = [select Id, Name, Opportunity_Percentage_Allocated__c, OwnerId 
					   from Opportunity limit 1]; 
					   

Productivity_Allocations__c pa = new Productivity_Allocations__c ( 
								Opportunity_Name__c = oList[].Id , 
								Sales_Associate__c =  oList[].OwnerId); 

insert pa;

 
Productivity_Allocations__c[] paList = [select id, Allocation_Percentage__c, Opportunity_Name__c 
										from Productivity_Allocations__c 
										where id = :pa.Id];

 

//for example update allocation percentage



paList[].Allocation_Percentage__c = 100;

update paList;

 

}
}

 Please advise.

 

Regards,

Hammad Shah

 

Imran MohammedImran Mohammed

You have to use oList[0] and paList[0] when retrieving values from a List.

Hammad ShahHammad Shah

Hello Imran,

 

I have adjusted the trigger as you mentioned but I am getting 0% coverage on the trigger I am writing this test class for. The code so far is as follows.

 

 

@isTest
private class PA_Update_Percentage_Test_Class {

  static testMethod void myUnitTest() {
        // TO DO: implement unit test
        
Opportunity [] oList = [select id, name, Opportunity_Percentage_Allocated__c, OwnerId 
					   from Opportunity limit 1];

Productivity_Allocations__c pa = new Productivity_Allocations__c ( 
								Opportunity_Name__c = oList[0].Id, 
								Sales_Associate__c = oList[0].OwnerId,
								Allocation_Percentage__c = 60);

insert pa;



 
Productivity_Allocations__c[] paList = [select id, Allocation_Percentage__c, Opportunity_Name__c 
										from Productivity_Allocations__c 
										where id = :pa.id];

 

//for example update allocation percentage

paList[0].Allocation_Percentage__c = 100;




update paList;

}
}

 

Something to keep in mind is that this is an update trigger I am trying to test. The function the trigger performs is looks at updated PA records which are detail records to an Opp. Based on a roll up summary field on the Opp Opportunity_Percentage_Allocated__c the trigger sends an error if the total allocated percentage is over 100%

 

 

I really appreciate the help so far.

 

Regards,

Hammad Shah

Imran MohammedImran Mohammed

Can you add some assert statements in the test method and see whether the insert or update is happening or not.

for ex: 

insert pa;

Productivity_Allocations__c assertPa = [select id, Allocation_Percentage__c from Productivity_Allocations__c where id = :pa.id];

system.assertEquals(assertPa.Allocation_Percentage__c, pa.Allocation_Percentage__c);

 

Let me know what you found.

 

dmchengdmcheng

In test methods, you should create and insert dummy data for testing and not rely on existing records in the database like you are doing with the Opportunity.  You need to create  and insert a dummy Opportunity, and a dummy account/contact if necessary, then link your pa record to the new opp.

 

BTW - here is a good web page on Apex test methods.

http://wiki.developerforce.com/index.php/An_Introduction_to_Apex_Code_Test_Methods

Hammad ShahHammad Shah

Hello Imran,

 

Thank you for the help. The original code you suggested was good enough to get me 71% coverage. Combined with my other triggers and classes I managed to barely hit 75% total coverage. I was able to do my deployment succesfully. I really appreciate the help. The finished code looks like the following.

 

 

@isTest
private class PA_Update_Percentage_Test_Class {

  static testMethod void myUnitTest() {
        // TO DO: implement unit test
        
Opportunity [] oList = [select id, name, Opportunity_Percentage_Allocated__c, OwnerId 
					   from Opportunity limit 1];

Productivity_Allocations__c pa = new Productivity_Allocations__c ( 
								Opportunity_Name__c = oList[0].Id, 
								Sales_Associate__c = oList[0].OwnerId,
								Allocation_Percentage__c = 60);

insert pa;



 
Productivity_Allocations__c[] paList = [select id, Allocation_Percentage__c, Opportunity_Name__c 
										from Productivity_Allocations__c 
										where id = :pa.id];

 

//for example update allocation percentage

paList[0].Allocation_Percentage__c = 100;




update paList;

}
}

 

 

Regards,

Hammad Shah