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
Matthew HamptonMatthew Hampton 

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

Best Answer chosen by Admin (Salesforce Developers) 
MiddhaMiddha

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:

 

 

trigger SubUpdate on Subscriber__c (before insert) {

Set<String> addressSet = new Set<String>();
Set<String> locSet = new Set<String>();
Set<String> zipSet = new Set<String>();
		for(Subscriber__c s: Trigger.new)
			{
				addressSet.add(s.street_address__c);
				locSet.add(s.loc__c);
				zipSet.add(s.zip_code__c);
			}




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 IN : addressSet
and loc__c IN: locSet
and zip_code__c IN: zipSet
];

	if(gpon.size()>0)
	{
		Map<String, Fiber_Qualified_Address__c> FQAMap = new Map<String, Fiber_Qualified_Address__c>();
		
		//ADD CHECKS AND LOGIC IF ANY ONE OF THESE KEY VALUES ARE NULL
		for(Fiber_Qualified_Address__c foa: gpon)
		{
			FQAMap.put(foa.street_address__c + '' + foa.loc__c + '' + foa.zip_Code__c, foa);
		}

		for(Subscriber__c s: Trigger.new)
		{
			if(FQAMap.containsKey(s.street_address__c + '' + s.loc__c + '' + s.zip_Code__c))
				s.fiber_address__c = FQAMap.get(s.street_address__c + '' + s.loc__c + '' + s.zip_Code__c);
		}

	}

}

 

 

 

All Answers

MiddhaMiddha

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:

 

		Set<String> addressSet = new Set<String>();
		for(Subscriber__c s: Trigger.new)
			{
				addressSet.add(s.street_address__c);
			}

 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.

 

Matthew HamptonMatthew Hampton

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

MiddhaMiddha

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:

 

 

trigger SubUpdate on Subscriber__c (before insert) {

Set<String> addressSet = new Set<String>();
Set<String> locSet = new Set<String>();
Set<String> zipSet = new Set<String>();
		for(Subscriber__c s: Trigger.new)
			{
				addressSet.add(s.street_address__c);
				locSet.add(s.loc__c);
				zipSet.add(s.zip_code__c);
			}




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 IN : addressSet
and loc__c IN: locSet
and zip_code__c IN: zipSet
];

	if(gpon.size()>0)
	{
		Map<String, Fiber_Qualified_Address__c> FQAMap = new Map<String, Fiber_Qualified_Address__c>();
		
		//ADD CHECKS AND LOGIC IF ANY ONE OF THESE KEY VALUES ARE NULL
		for(Fiber_Qualified_Address__c foa: gpon)
		{
			FQAMap.put(foa.street_address__c + '' + foa.loc__c + '' + foa.zip_Code__c, foa);
		}

		for(Subscriber__c s: Trigger.new)
		{
			if(FQAMap.containsKey(s.street_address__c + '' + s.loc__c + '' + s.zip_Code__c))
				s.fiber_address__c = FQAMap.get(s.street_address__c + '' + s.loc__c + '' + s.zip_Code__c);
		}

	}

}

 

 

 

This was selected as the best answer
Matthew HamptonMatthew Hampton

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

MiddhaMiddha

Correct, being a lookup it should return an ID, correcting the last lines:

 

 

if(FQAMap.containsKey(s.street_address__c + '' + s.loc__c + '' + s.zip_Code__c))
				s.fiber_address__c = FQAMap.get(s.street_address__c + '' + s.loc__c + '' + s.zip_Code__c).Id;

 

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.

 

Matthew HamptonMatthew Hampton

I really appreciate all of you help. That worked perfectly.

 

Thanks again!

Matthew HamptonMatthew Hampton

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?