You need to sign in to do that
Don't have an account?
Bulkify Trigger , problems with SOQL syntax
Hi,
In essence the below trigger creates records in two junction objects, TrialContactAssociation__c & TrialAccountAssociation__c , when a Trial__c record is created that matches an email address in an existing Contact. This creates a way for us to track Accounts and Contacts that are already in our system for incoming trials.
After a zillion hours of Googling I can't seem to figure out how to reference Email__c outside the for loop?
Any help on the bulkification syntax will be greatly appreciated! All bulkification examples I've come across reference the ID as opposed to a 'column' of incoming records.
trigger TrialInsert on Trial__c (after insert) { List<TrialContactAssociation__c> ContactListItem = new List<TrialContactAssociation__c>(); List<TrialAccountAssociation__c> AccountListItem = new List<TrialAccountAssociation__c>(); for (Trial__c c : Trigger.new) { List<Contact> contactID = new List<Contact>([SELECT Id FROM Contact WHERE Email LIKE :c.Email__c]); List<Contact> ContactAccountRel = new List<Contact>([SELECT AccountId FROM Contact WHERE Email LIKE :c.Email__c]); for (Contact contactItem : contactID){ TrialContactAssociation__c clt = new TrialContactAssociation__c(Trial__c = c.ID, Contact__c = contactItem.Id); ContactListItem.add(clt); } for (Contact AccountItem : ContactAccountRel){ TrialAccountAssociation__c clt2 = new TrialAccountAssociation__c(Trial_Account_Asso__c = c.ID, Account_Trial_Link__c = AccountItem.AccountId); AccountListItem.add(clt2); } } insert ContactListItem; insert AccountListItem; }
Thanks
John
What if there is a new trail with an Email which belongs to two or more Contacts? The above code will create a TrailContactAssociation & TrailAccountAssociation records for every match contact email.
Let me know if it doesnt work.
I'm not sure what you're matching logic for email is so you can try this one as well. It's a little more efficienct as it doesn't use any soql in loops at all. Though this was written on the fly so may need some tweaking:
Thanks very much for the help Naidu!!
Looks like the SOQL is still running within a for loop, am I correct? Will this fall over if the number of incoming trials exceeds 20?
"...
..."
We fortunately don' have any duplicate email addresses in Salesforce.
thanks again for your help.
John
if you dont have any duplicate Emails, then go with the above code..