+ Start a Discussion
Jefe55Jefe55 

Apex update trigger not working using Apex Data Loader

I have a trigger set up on an object to trigger after an update.  If I do the update manually it works just fine.  However, if I do a mass update using the Data Loader the trigger doesn't get invoked.
 
Is there a way to force the trigger while using the Apex Data Loader?
Best Answer chosen by Admin (Salesforce Developers) 
JimRaeJimRae

Try this:

 

Trigger TaskRegion on Task (before insert, before update) {
Set<ID> ownerIDs = new Set<ID>();
for(Task t:Trigger.new){ownerIDs.add(t.ownerid);}
Map<ID,User> taskOwnerMAP = new Map<ID,User>([select id,Region__c from User where id=:ownerIDs]);
for(Task t2:Trigger.new){
	t2.Assigned_Region__c=taskOwnerMap.get(t2.ownerid).Region__c;
}

 

All Answers

JimRaeJimRae

The trigger should still fire fine when you are loading from the loader.

Was the trigger set up to handle batch record processing?  Could be that it is actually only firing for the first record in your batch, and it just seems that it isn't working.

You need to handle an array of the trigger objects and process each one.

Post your trigger code if you want anyone to take a look at it.

OneNewbieOneNewbie
I am having the same issue. The confusing part is that I have tested batch updates in our sandbox environment and it works fine but it doesn't work in production. The trigger code is the same in sandbox as it is in production. I am still looking for a solution for this problem.
Shadow8002Shadow8002
Hi Jefe

The trigger should still fire even if you use the data loader. The only way batch updates are not handled would be if

> you have used a check like If(Trigger.New/Old.size()==0)
> Or you would have hardcoded 'Trigger.New/Old[0]'

In the first case, the trigger will not get executed when you do a batch update. In the second case, only the first record in the batch gets processed through the trigger.


KitaSanKitaSan

How would you go about modifying for batch? 

 

I have a simple trigger to update a field: 

 

}

rigger TaskRegion on Task (before insert, before update) {
String locale = [Select Region__c from User where id = :Trigger.new[0].OwnerId][0].Region__c;
Trigger.new[0].Assigned_Region__c = locale; 

 

Thanks!

JimRaeJimRae

Something like this should work.

 

Trigger TaskRegion on Task (before insert, before update) {
Set<ID> ownerIDs = new Set<ID>();
for(Task t:Trigger.new){ownerIDs.add(t.ownerid);}
Map<ID,User> taskOwnerMAP = [select id,Region__c from User where id=:ownerIDs];
for(Task t2:Trigger.new){
	t2.Assigned_Region__c=taskOwnerMap.get(t2.ownerid).Region__c;
}

 

KitaSanKitaSan

Thanks!! 

 

I do still get an error though: 

 

Save error: Illegal assignment from LIST<User> to MAP<Id,User>

 

Thanks for your help!

KitaSanKitaSan

Do I have to create a list first? 

 

Thanks!!! 

-Justin 

JimRaeJimRae

Try this:

 

Trigger TaskRegion on Task (before insert, before update) {
Set<ID> ownerIDs = new Set<ID>();
for(Task t:Trigger.new){ownerIDs.add(t.ownerid);}
Map<ID,User> taskOwnerMAP = new Map<ID,User>([select id,Region__c from User where id=:ownerIDs]);
for(Task t2:Trigger.new){
	t2.Assigned_Region__c=taskOwnerMap.get(t2.ownerid).Region__c;
}

 

This was selected as the best answer
KitaSanKitaSan

Thank you!!!

 

I have started with the Force.com Workbook to learn more APEX, are there any other resources you can suggest? 

 

Thanks again!

JimRaeJimRae

That is a good start, use the Cookbook examples from this site, and Google when you have questions. There are many excellent blogs out there on this topic.

Roger WickiRoger Wicki
I don't know if this thread is still read, but I seem to have trouble getting my code to execute correctly in bulk mode. If I update a record through UI, it works like a charm. If I use Data Loader with a batch size of 200, nothing will be updated. I tried now with a data set of 2 records over Data Loaderand that worked. Why would 2 work but in a batch of 200 all of them won't and without causing any kind of exception or error? I catch exceptions but looking at the log i see no exception were even thrown.

My triggers and classes are all built bulk-safe and even handle cases where there might be concurrent updates. If you require insight to my code I am happy to post it as an answer and further detail of what I'm trying to do in the code.

Did anyone else experience problems with that?
Roger WickiRoger Wicki
Update: It worked in a batch of 100, for whatever reason...
Jeremiah BaylesJeremiah Bayles
Just to clarify, when you do an update via the gui the trigger executes with one item in the trigger.new object. On the other hand, when you use dataloader and the trigger is tripped there is an array that populates the trigger.new.
Jeremiah BaylesJeremiah Bayles
basically: for (Integer i = 0; i <Trigger.new.size(); i++) does not work with dataloader
 
Jeremiah BaylesJeremiah Bayles
The solution specified, using the obect iteration instead of the index, did not update any records.
 
Brandon HurleyBrandon Hurley

@Roger Wicki 

No idea if you'll ever see this, but FWIW, I had similar issue with a trigger on opportunity. If your trigger has a DML statement, you could hit the governor limit with a data loader batch of 200. I believe the governor limit is 150, which may be why it works with a batch size of 100.

If anyone knows how i can get dupecatcher to fire via data loader, that'd be amazing!!

Roger WickiRoger Wicki
Hi @Brandon Hurley

Thanks for the notice. My trigger indeed has a DML statement, but so far I was in the strong believe that the batch size for DML was 200 as well... According to the limits page, 150 is the limit of allowed DML statements per transaction. That would almost mean I'd have a DML in a loop. If that'd happen, I would also have to receive an error message for limits exceeded.

If by dupecatcher you mean Salesforce's built in duplicate detector, mine is working. I sometimes get errors in Data Loader if it detects duplicates. My duplicate rules are set up to completely block however. Maybe warnings don't work.