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
birdofpreybirdofprey 

test code for assignment in a trigger.

So I have this trigger, but i have no idea how to write a test code for assignment within a trigger. Any pointers?

All the private strings and instatiation of the object goes untested.

 

the code is below:

 

trigger ZipBoroughContact on Contact (after insert, before update) {

	private String PriBoro;
	private String SecBoro;
	private String ZipCode;	
	private String ZipCodeSecondary;
	private List <Contact> getRecords;
	Contact Rec = trigger.new[0];
	
		getRecords = [SELECT Primary_Borough__c, Secondary_Borough__c, MailingPostalCode, OtherPostalCode From Contact Where Id IN :Trigger.old];
	
		PriBoro = getRecords[0].Primary_Borough__c;	
		SecBoro = getRecords[0].Secondary_Borough__c;
		ZipCode = getRecords[0].MailingPostalCode;
		ZipCodeSecondary = getRecords[0].OtherPostalCode;
		
		if (ZipCode != NULL && PriBoro == NULL){
			ZipCodeList fillPriBoro = new ZipCodeList();
			fillPriBoro.ZipCode = ZipCode;
			Rec.Primary_Borough__c = fillPriBoro.getBorough();
		}	
		
 		
}

 

Best Answer chosen by Admin (Salesforce Developers) 
James LoghryJames Loghry

Damien_ is correct in that your test case basically needs to create / update a contact, and it should fire this trigger.

 

I noticed your trigger probably doesn't do what you need it to, however.  I'd take a closer look at Trigger best practices: http://wiki.developerforce.com/page/Apex_Code_Best_Practices

 

In particular,

1) You don't need the SOQL query, as Trigger.old will already have that information available for you.

2) Your code only deals with the first contact record in the transaction.  There may be up to 200 transactions total, so 199 of the 200 contacts will be missed.  In order to adjust this, you'll need to loop through Trigger.old and perform that same zip code / borough calculation.

3) Since you have after insert, and not before insert, no changes will be made to any new contacts unless you call update on the list of new Contacts.  I'm not sure what your use case calls for, but take a closer look at the differences between before insert and after insert.

4) There are a few others, but 1-3 are the heavy hitters.

 

Hope that helps.

 

All Answers

Damien_Damien_

insert a Contact in your test class, then update one.  It should hit both the after insert and before update.

James LoghryJames Loghry

Damien_ is correct in that your test case basically needs to create / update a contact, and it should fire this trigger.

 

I noticed your trigger probably doesn't do what you need it to, however.  I'd take a closer look at Trigger best practices: http://wiki.developerforce.com/page/Apex_Code_Best_Practices

 

In particular,

1) You don't need the SOQL query, as Trigger.old will already have that information available for you.

2) Your code only deals with the first contact record in the transaction.  There may be up to 200 transactions total, so 199 of the 200 contacts will be missed.  In order to adjust this, you'll need to loop through Trigger.old and perform that same zip code / borough calculation.

3) Since you have after insert, and not before insert, no changes will be made to any new contacts unless you call update on the list of new Contacts.  I'm not sure what your use case calls for, but take a closer look at the differences between before insert and after insert.

4) There are a few others, but 1-3 are the heavy hitters.

 

Hope that helps.

 

This was selected as the best answer