+ Start a Discussion
jason_bestjason_best 

Trigger Design

I am very new to SF, but I'm building out a Student Recruiting App for 
my org.  I would love some feedback on the design of this trigger I 
just created.  I'm not sure how much background you need on the data 
structure of the app to provide feedback, but I will give you a basic 
overview. 

 

  • Education Object is detail to to Contact - used to show all schools a 
    prospect has attended.
  • Education has Account Lookup field for link back to School(Account 
    Record) 
  •  Enrollment is Child to Contact - used to Inquiry through graduation 
    for students 
  •  Application is Child to Enrollment 
  •  Document is Child to Application 

I hope that's enough to make sense of it all.  Here is the trigger.  It works just fine, but I 
would love to hear from the experts if this looks like it will scale 
well and any other feedback you are willing to offer. 

 

trigger CreateDocuments on Application__c (after insert) { 

List<Document__c> createdocument = new List <Document__c> {}; 
	for (Application__c app : trigger.new) { 
		List<Contact> c=[SELECT Id,Name FROM Contact WHERE 
		Id=:app.Contact__c]; 
		List<Education__c> ed = [SELECT School__c FROM Education__c WHERE 
		Student__c=:c]; 
		if(app.Recieved_Method__c <> 'Online'){ 
			for(Education__c e : ed){ 
			createdocument.add(new Document__c ( Name = 'Transcript', 
			Application__c =app.id,School__c=e.School__c,Contact__c=app.Contact__c)); 
			} 
		createdocument.add(new Document__c ( Name = 'Recommendation 1', 
		Application__c = app.id,Contact__c=app.Contact__c)); 
		createdocument.add(new Document__c ( Name = 'Recommendation 2', 
		Application__c = app.id,Contact__c=app.Contact__c)); 
		} 
	} 
try { 
	insert createdocument; 
} 
catch (Exception Ex) 
{ 
system.debug(Ex); 
}

 

Best Answer chosen by Admin (Salesforce Developers) 
rmehrmeh

Hi Jason,

 

My first question to you is that is the trigger bulk enabled. Have you tested with a quiet a more number of records.

I think you might hit the apex governor limits because of the below said reason.

  • Firstly a golden rule to salesforce is that you should never query inside a for loop (be it a trigger or apex). There is a possibility that might work for a single or fewer records, but with more data you are sure to hit the Governor limits.
  • Making use of Sets and Maps will help you avoid the queries in the for loop. For instance in your trigger you could avoid the 1st query as follows:
List<Document__c> createdocument = new List <Document__c> {}; 
Set<Id> setContactApp = new Set<Id>();
for (Application__c app : trigger.new) {
if(app.Contact__c != null)
{
setContactApp.add(app.Contact__c);
}
}

List<Contact> c=[SELECT Id,Name FROM Contact WHERE Id IN:setContactApp limit 1000];

Apply the changes accordingly and check how your trigger works.

And do let me know if any concerns.

 

All Answers

rmehrmeh

Hi Jason,

 

My first question to you is that is the trigger bulk enabled. Have you tested with a quiet a more number of records.

I think you might hit the apex governor limits because of the below said reason.

  • Firstly a golden rule to salesforce is that you should never query inside a for loop (be it a trigger or apex). There is a possibility that might work for a single or fewer records, but with more data you are sure to hit the Governor limits.
  • Making use of Sets and Maps will help you avoid the queries in the for loop. For instance in your trigger you could avoid the 1st query as follows:
List<Document__c> createdocument = new List <Document__c> {}; 
Set<Id> setContactApp = new Set<Id>();
for (Application__c app : trigger.new) {
if(app.Contact__c != null)
{
setContactApp.add(app.Contact__c);
}
}

List<Contact> c=[SELECT Id,Name FROM Contact WHERE Id IN:setContactApp limit 1000];

Apply the changes accordingly and check how your trigger works.

And do let me know if any concerns.

 

This was selected as the best answer
jason_bestjason_best

Thanks for the feedback.  Governor limits is exactly what I was wondering about.  I have made this change and it works wonderfully.  (side note: We could only dream at this point that we would need to worry about managing thousands of applications.  But that's a marketing issue)

 

I really appreciate you taking the time to school this newbie.

 

Jason