You need to sign in to do that
Don't have an account?

How to bulkify and batchify a trigger to avoid Governance Limits
Hi All,
I am very new to Apex and trying to bulkify and batchify a trigger to avoid the Governance Limits. Below is the my trigger
trigger IAA on Contact (before insert, before update)
{
for (Contact c : Trigger.new)
{
if (c.Area_of_Interest_del__c != 'Law' && (c.Visa_Type__c == 'J1 Exchange Visitor'||c.Visa_Type__c =='F1 Nonimmigrant Student'||c.International_Student__c ==True))
{
Account iaa = [Select Id FROM Account WHERE Account.Name = 'GGU - International Admissions & Advising'];
c.AccountId = iaa.Id;
}
}
}
I will really appreciate any help.
Thanks
I am very new to Apex and trying to bulkify and batchify a trigger to avoid the Governance Limits. Below is the my trigger
trigger IAA on Contact (before insert, before update)
{
for (Contact c : Trigger.new)
{
if (c.Area_of_Interest_del__c != 'Law' && (c.Visa_Type__c == 'J1 Exchange Visitor'||c.Visa_Type__c =='F1 Nonimmigrant Student'||c.International_Student__c ==True))
{
Account iaa = [Select Id FROM Account WHERE Account.Name = 'GGU - International Admissions & Advising'];
c.AccountId = iaa.Id;
}
}
}
I will really appreciate any help.
Thanks
in your case, since you are going to use the same account on all contacts matching a condition, you dont need to query the account again & again in the loop.. just query it once at the begining and use it..
trigger IAA on Contact (before insert, before update)
{
Account iaa = [Select Id FROM Account WHERE Account.Name = 'GGU - International Admissions & Advising'];
for (Contact c : Trigger.new)
{
if (c.Area_of_Interest_del__c != 'Law' && (c.Visa_Type__c == 'J1 Exchange Visitor'||c.Visa_Type__c =='F1 Nonimmigrant Student'||c.International_Student__c ==True))
{
c.AccountId = iaa.Id;
}
}
}
for more details on how to bulklify your code, refer this article
http://wiki.developerforce.com/page/Best_Practice%3A_Bulkify_Your_Code
All Answers
in your case, since you are going to use the same account on all contacts matching a condition, you dont need to query the account again & again in the loop.. just query it once at the begining and use it..
trigger IAA on Contact (before insert, before update)
{
Account iaa = [Select Id FROM Account WHERE Account.Name = 'GGU - International Admissions & Advising'];
for (Contact c : Trigger.new)
{
if (c.Area_of_Interest_del__c != 'Law' && (c.Visa_Type__c == 'J1 Exchange Visitor'||c.Visa_Type__c =='F1 Nonimmigrant Student'||c.International_Student__c ==True))
{
c.AccountId = iaa.Id;
}
}
}
for more details on how to bulklify your code, refer this article
http://wiki.developerforce.com/page/Best_Practice%3A_Bulkify_Your_Code
let me know if it solved ur issue or if you have any more queries..
Thanks for your help. It works.
Kamaldeep