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

Bulk Record Insert
I have the following extremely basic code written in my sandbox:
trigger SubUpdate on Subscriber__c (before insert) {
list<Fiber_Qualified_Address__c> gpon = [select id,name, street_address__c, loc__c, zip_code__c from Fiber_Qualified_Address__c where street_address__c=:Trigger.new[0].street_address__c
and loc__c=:Trigger.new[0].loc__c
and zip_code__c=:Trigger.new[0].zip_code__c
];
if(gpon.size()>0)
{
Trigger.new[0].fiber_address__c = gpon[0].id;
}}
The issue I am having is that when I insert new records, I use the Apex Data Loader and am sometimes inserting hundreds or thousands of records at a time. What am I missing here so that the trigger fires on all records and not just the first record in the batch? I am still fairly green on code writing.
Any help is appreciated.
Matt
Below id the code. Check for typos, if any. You should add some more checks and logic to handle null or blank values. Also the below code can be optimized to generate the unique key within a formula field itself. But this would also work:
All Answers
Matthew,
Triggers get a list of records and not a single record hence trigger code should process all the records that a trigger gets. The issue with your code is that you are processing only the first element of the Trigger.new whereas you should process all the elements in Trigger.new list.
You should collect all the values required in a set and use these SET in your queries like:
You can collect LOC and ZIP code in the same loop but in three different sets and use them in your query. Once you query records you can write another for loop and a logic to process all these records accordingly.
G:
Thanks for the help. I just want to make sure I am doing this correctly. The Subscriber__c contains three fields: street_address__c, loc__c and zip_code__c. In order for the trigger to fire, I need it to find a record on Fiber_Qualified_Address__c where all three fields match. Do I still need to collect each set separately?
Also, I am having issues with the logic and such after the sets. Can you point me in the right direction given what I have provided?
Thanks again!
Matt
Below id the code. Check for typos, if any. You should add some more checks and logic to handle null or blank values. Also the below code can be optimized to generate the unique key within a formula field itself. But this would also work:
Gulshan:
Thank you for the code. I think I understand all of it, what it is doing, etc.
My last (hopefully) question pertains to the final step:
Fiber_Address__c on Subscriber__c is a Lookup field so if there is a match, I need to pull the ID from Fiber_Qualified_Address__c. What is the best way to append the last line of the code so that when the field is updated, it is updated with a record ID and not a text string?
Thanks in advance!
Matt
Correct, being a lookup it should return an ID, correcting the last lines:
FQAMap will return you the matching record and ".Id" will give you the Id fo that record which is set in fiber_address__c field.
I really appreciate all of you help. That worked perfectly.
Thanks again!
I am back for more help (sadly).
The code above works great on all of the testing I have done.
I need to take the same code (or something similar) and reverse it now. What I need is after I insert or update a record on Fiber_Qualified_Address__c, I need the code to do the same thing (look for a match on Street_Address__c + Loc__c + Zip_Code__c on Subscriber__c) and if there is a match, update Subscriber__c. I have been trying for four hours and cannot get it to work. Can you assist?