You need to sign in to do that
Don't have an account?
Jayse
Bulkified trigger not running when loading data with dataloader
Hi Everyone.
I've seen a few posts that are similar, but not the same as what i'm encountering. I've written a bulkified trigger (or at least i think I have), but when I load data in with dataloader, it does not appear that the trigger has fired when i open up the record.
If i click edit and save on the record, I can see the trigger has worked as it populates the fields as I expect it to.
trigger code is below:
trigger salesDataSellToCustomerTrigger on Sales_Data__c (before insert, before update) { Map<String, Sales_Data__c > salesDataMap = new Map<String, Sales_Data__c >(); for (Sales_Data__c salesData: System.Trigger.new) { if (salesData.Sell_to_Customer_No__c == null ) { salesData.Sell_to_Customer_Id__c = null; } else { salesDataMap.put(salesData.Sell_to_Customer_No__c, salesData); } } // Using a single database query, find all the accounts in // the database that have the same Company No as any // of the Sales Data with SellToCustomer Number for (Account account: [SELECT Id, Company_No__c FROM Account WHERE Company_No__c IN :salesDataMap.KeySet()]) { Sales_Data__c sData = salesDataMap.get(account.Company_No__c); // update SalesData SellToCustomer ID sData.Sell_to_Customer_ID__c = account.id; } }
Thought there may be an option in the data loader settings, but I couldn't see anything obvious, so any advice / guidence would be appreciated.
Jayson
Hi,
I was looking for something like your code the whole day, thanks.
But I have the same problem. Creating an object myself works flawless. Using the Data Loader for mac I get an error:
Null pointer exception: attempt to de reference a null object.
Is there a possibility that a Sell_to_Customer_No__c and Company_No__c only differ by case?
If so, the query will pull the match (case insensitive match), but will fail on the line:
Sales_Data__c sData = salesDataMap.get(account.Company_No__c);
Because the lookup is case sensitive - so sData will be null.
This has nothing to do with the use of the dataloader - just that on a bulk import you're more likely to run into this situation if it does exist.
Dan
Hi Dan,
The sell_to_customer and Company_no are separate (different format) reference numbers, so that should not be the case.
Jayson
Well, now I'm confused...
You load the salesDataMap with Sell_to_Customer_No__c as the key
You then query by comparing Company_No__c against that keyset (all Sell_to_Customer_No__c values)
You then use the sData object using Company_No__c as the key
That only makes sense if those values (Company_No__c and Sell_to_Customer__c ) have exactly the same format and case.
Otherwise, I don't see how you'd get any results to the query at all, unless the problem is that you're matching records where Company_No__c is null.....
Dan
Apologies Dan, don't think i was completely awake when i replied this morning...
Yes, the sellToCustomer is the same as the customer number.
What i'm doing is: the SalesData object has the sellToCustomerNo field, which is a text field. The trigger takes this text value, searches against the companyNo field on account to return the account Id and inserts that back onto the SalesData record on the SellToCustomerID field.
Hope that clarified what I'm trying to acheive.
As I mentioned in the initial post, this does work on a single record when edited via the browser, it just fails to run when loaded in bulk via dataloader.
Thanks
Honestly, I think the dataloader issue is misleading - it's just making an existing issue visible rather than being the source of the problem.
Since the problem is reproducable under the dataloader, here's my suggestion:
Turn on debug logs.
Do an import using the data loader.
Add System.Debug statements as follows:
Look for an account where Company_No__c is null or is not found in the keyset. Neither of these should be possible - unless there is a difference in case between them.
I'm assuming the null exception is located at the reference to sData.Sell_to_Customer_ID__c. Please confirm that this is correct.
Try it - I look forward to seeing what you discover.
Dan
Apologies for the delay updating this thread, was busy with personal stuff.
With debug enabled, I loaded 5 records in with dataloader. I'm no expert at reading the debug logs, but no clear exception visible as far as i can see and it looks like it's picked up the correct records.
Then went and edited a record that i'd uploaded (no data changes made, just clicked the edit button, then save). This worked as expected. As debug was still enabled, I tried comparing the 2 output files, nothing (as far as i can tell) as to why the bulk upload is failing.
Will attach the debug logs below
Hope someone who is more au fait with reading debug logs can spot the difference; and hopefully the cause of the problem.
Bulk upload:
Debug log from single update (executed through the browser application)
think i've solved it, albeit by starting over, using the following forum posting as a guide: forum posting
After i'd done this, what I *think* the problem with my initial code was the last line of the initial for loop to populate the map.
Is the map entry being overwritten everytime when a key already exists, in which case only the last record being uploaded (through data loader), will be stored as the object value in the map.
I've tested it with 5 records being loaded in from dataloader (4 entries had the same sell to customer no, the related ID were correctly populated on all 4 records).