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
MVDMVD 

Test Class Assistance for My First Trigger

Hello! The amazing people in the developer zone at Dreamforce this year inspired me to start learning apex triggers and visualforce (I am a certified admin currently). I have my first trigger completed without errors and now I need to see if it works!! This is very exciting.  It seems I need to create a new APEX CLASS to test this trigger (the trigger is below).

I have been reading through the online workbooks but really am not sure how to start the logic on this (except to create new class and use the public class type).  Thank you in advance for anyone willing to guide me in the right direction!!!

 

 

trigger HPAJunctionAutoRefresh on HPA__c (after update) {

for( HPA__c parent: Trigger.new)
{

//LIST ALL CHILD RECORDS UNDER HPA_JUNCTION OBJECT AND COMPARE TO PARENT EVERY TIME THE HPA_PARENT IS EDITED
//AND UPDATE THE STATUS FIELD ON CHILD HPA_JUNCTION TO MATCH HPA_PARENT SO THAT WORKFLOWS ARE ACTIVATED TO UPDATE THE ACCOUNT
//WHICH IS THE SECOND MASTER-DETAIL RELATIONSHIP TO THE HPA_JUNCTION

 

List<HPA_Property_Junction__c> children = [ SELECT Id, HPA_Name__c, HPA_Current_Status_Snapshot__c from
HPA_Property_Junction__c where HPA_Name__r.id = :parent.Id];

List<HPA_Property_Junction__c> childrenToUpdate = new List<HPA_Property_Junction__c>();

for(HPA_Property_Junction__c thischild: children )
{
if( thischild.HPA_Current_Status_Snapshot__c != parent.HPA_Current_Status_Text__c)
{
thischild.HPA_Current_Status_Snapshot__c = parent.HPA_Current_Status_Text__c;
childrenToUpdate.add(thischild)
;
}

if( !childrenToUpdate.isEmpty())
{
update childrenToUpdate;
}

}
}
}

HCrousePDXHCrousePDX

Well before you write your test code, you shoud rework your trigger.  Right now if you are updating more than 100 or so, probably less you will hit SOQL govenor limits.  First for each record in trigger new you are doing a SOQL query and an update.  You can only do 100 soql queries per session and 150 dml statements.  Also best practice is to not put the actual code inside the trigger but to put in another class and call the method from a trigger handler.

 

You should be querying for children records for every record in trigger.new at once, not per record.  

 

For test class is should look something like below.

 

@isTest

public class testHPATrigger{

 

public testmethod void testupdate(){

  //code for creating records and processing values through trigger here.

}

 

}

 

 

Abhi_TripathiAbhi_Tripathi

Hi,

 

I dont think your trigger is correct, because your have queried records in the "Loop", which will hit Governer Limit  and that'll show you error. because queries are limited in an apex class, check this post

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_gov_limits.htm

 

and you were updating in that loop too, so that was also gonna make some mess

Like if a user updates 1000 records then your code will show error definetly.

 

for Test classes you can refer my blog too.

http://abhithetechknight.blogspot.in/2013/10/salesforce-test-class-basics.html

 

 

so for that thing you can do something like this

 

trigger HPAJunctionAutoRefresh on HPA__c (after update) {
	
	//List of Ids
	List<Id> listId = new List<Id>();

	//List
	List<HPA_Property_Junction__c> childrenToUpdate = new List<HPA_Property_Junction__c>();

	Check for Events
	if(Trigger.isInsert) {
		if(Trigger.isAfter) {

			//Loop
			for( HPA__c parent: Trigger.new ) {

				//Add to list of Id
				listId.add(parent.Id);
			}
	
			List<HPA_Property_Junction__c> children = [ SELECT Id, HPA_Name__c, HPA_Current_Status_Snapshot__c from
								HPA_Property_Junction__c where HPA_Name__r.id =: listId.Id];
    	
			//Loop
			for(HPA__c hpa : Trigger.new ) {
				for(HPA_Property_Junction__c thischild: children ) {

					if( thischild.HPA_Current_Status_Snapshot__c != hpa.HPA_Current_Status_Text__c) {

						thischild.HPA_Current_Status_Snapshot__c = hpa.HPA_Current_Status_Text__c;
						childrenToUpdate.add(thischild);
					}
				}
			}
		}
	
	//Update
	if( !childrenToUpdate.isEmpty()) {
		update childrenToUpdate;
	}
}


//----------------------------------------------------------------
Hers is you test class

@isTest(seeAllData = false) 
private class Test_HPAJunctionAutoRefresh {

	//Test Method
	static testMethod void myTestMethod() {
		
		//Insert HPA__c
		HPA__c hpa = new HPA__c( Name = 'test', HPA_Current_Status_Text__c = '12345');
		insert hpa;

		//Insert HPA_Property_Junction__c
		HPA_Property_Junction__c hpProperty = new HPA_Property_Junction__c(Name = 'test' , HPA_Name__r = hpa.Id, HPA_Current_Status_Snapshot__c = 'game');

		//Asserts
		System.assertEquals(true, hpProperty.HPA_Current_Status_Snapshot__c == '12345' );

	}
}
	

 

                      DON'T FORGET THE KUDOS (star)

Satyendra RawatSatyendra Rawat

hi,

 

update the trigger and respective test method.

 



trigger HPAJunctionAutoRefresh on HPA__c (after update) {
List<HPA_Property_Junction__c> children = [ SELECT Id, HPA_Name__c, HPA_Current_Status_Snapshot__c from HPA_Property_Junction__c where HPA_Name__r.id = :trigger.newMap.keyset()];
List<HPA_Property_Junction__c> childrenToUpdate = new List<HPA_Property_Junction__c>();
for( HPA__c parent: Trigger.new)
{
//LIST ALL CHILD RECORDS UNDER HPA_JUNCTION OBJECT AND COMPARE TO PARENT EVERY TIME THE HPA_PARENT IS EDITED
//AND UPDATE THE STATUS FIELD ON CHILD HPA_JUNCTION TO MATCH HPA_PARENT SO THAT WORKFLOWS ARE ACTIVATED TO UPDATE THE ACCOUNT
//WHICH IS THE SECOND MASTER-DETAIL RELATIONSHIP TO THE HPA_JUNCTION


for(HPA_Property_Junction__c thischild: children )
{
if( HPA_Name__r.id==parent.Id && thischild.HPA_Current_Status_Snapshot__c != parent.HPA_Current_Status_Text__c)
{
thischild.HPA_Current_Status_Snapshot__c = parent.HPA_Current_Status_Text__c;
childrenToUpdate.add(thischild);
}


}
}
if( !childrenToUpdate.isEmpty())
{
update childrenToUpdate;
}

}

 

//----------------------------------------------------------------
Hers is you test class

@isTest(seeAllData = false)
private class TestHPAJunctionAutoRefresh {

//Test Method
static testMethod void myTestMethod() {

//Insert HPA__c
HPA__c hpa = new HPA__c( Name = 'test', HPA_Current_Status_Text__c = '12345');
insert hpa;

//Insert HPA_Property_Junction__c
HPA_Property_Junction__c hpProperty = new HPA_Property_Junction__c(Name = 'test' , HPA_Name__r = hpa.Id, HPA_Current_Status_Snapshot__c = 'game');
insert hpProperty;
update hpa;
//Asserts
System.assertEquals(true, hpProperty.HPA_Current_Status_Snapshot__c == '12345' );

}
}