+ Start a Discussion
JayseJayse 

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

Johann03Johann03

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.

kibitzerkibitzer

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

 

JayseJayse

Hi Dan,

 

The sell_to_customer and Company_no are separate (different format) reference numbers, so that should not be the case.

 

Jayson

kibitzerkibitzer

Well, now I'm confused...

 

You load the salesDataMap with Sell_to_Customer_No__c as the key

 

salesDataMap.put(salesData.Sell_to_Customer_No__c, salesData)

You then query by comparing Company_No__c against that keyset (all Sell_to_Customer_No__c values)

 

WHERE Company_No__c IN :salesDataMap.KeySet()

You then use the sData object using Company_No__c as the key

 

Sales_Data__c sData = salesDataMap.get(account.Company_No__c);

 

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

 

 

 

 

 

JayseJayse

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

 

kibitzerkibitzer

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:

 

for (Account account: [SELECT Id, Company_No__c FROM Account
                     WHERE Company_No__c IN :salesDataMap.KeySet()]) {
system.debug(account);
system.debug(salesDataMap.keyset()); Sales_Data__c sData = salesDataMap.get(account.Company_No__c); // update SalesData SellToCustomer ID sData.Sell_to_Customer_ID__c = account.id; }

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

 

JayseJayse

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.

JayseJayse

Bulk upload:

 

25.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
09:55:46.103 (103911000)|EXECUTION_STARTED
09:55:46.103 (103946000)|CODE_UNIT_STARTED|[EXTERNAL]|TRIGGERS
09:55:46.103 (103970000)|CODE_UNIT_STARTED|[EXTERNAL]|01qM00000008cBi|salesDataSellToCustomerTrigger on Sales_Data trigger event BeforeInsert for [new, new, new, new, new]
09:55:46.105 (105294000)|SYSTEM_METHOD_ENTRY|[5]|LIST<Sales_Data__c>.iterator()
09:55:46.105 (105553000)|SYSTEM_METHOD_EXIT|[5]|LIST<Sales_Data__c>.iterator()
09:55:46.105 (105589000)|SYSTEM_METHOD_ENTRY|[5]|system.ListIterator.hasNext()
09:55:46.105 (105625000)|SYSTEM_METHOD_EXIT|[5]|system.ListIterator.hasNext()
09:55:46.105 (105744000)|SYSTEM_METHOD_ENTRY|[11]|MAP.put(ANY, ANY)
09:55:46.105 (105782000)|SYSTEM_METHOD_EXIT|[11]|MAP.put(ANY, ANY)
09:55:46.105 (105796000)|SYSTEM_METHOD_ENTRY|[5]|system.ListIterator.hasNext()
09:55:46.105 (105811000)|SYSTEM_METHOD_EXIT|[5]|system.ListIterator.hasNext()
09:55:46.105 (105857000)|SYSTEM_METHOD_ENTRY|[11]|MAP.put(ANY, ANY)
09:55:46.105 (105878000)|SYSTEM_METHOD_EXIT|[11]|MAP.put(ANY, ANY)
09:55:46.105 (105892000)|SYSTEM_METHOD_ENTRY|[5]|system.ListIterator.hasNext()
09:55:46.105 (105906000)|SYSTEM_METHOD_EXIT|[5]|system.ListIterator.hasNext()
09:55:46.105 (105946000)|SYSTEM_METHOD_ENTRY|[11]|MAP.put(ANY, ANY)
09:55:46.105 (105965000)|SYSTEM_METHOD_EXIT|[11]|MAP.put(ANY, ANY)
09:55:46.105 (105979000)|SYSTEM_METHOD_ENTRY|[5]|system.ListIterator.hasNext()
09:55:46.105 (105992000)|SYSTEM_METHOD_EXIT|[5]|system.ListIterator.hasNext()
09:55:46.106 (106031000)|SYSTEM_METHOD_ENTRY|[11]|MAP.put(ANY, ANY)
09:55:46.106 (106051000)|SYSTEM_METHOD_EXIT|[11]|MAP.put(ANY, ANY)
09:55:46.106 (106064000)|SYSTEM_METHOD_ENTRY|[5]|system.ListIterator.hasNext()
09:55:46.106 (106078000)|SYSTEM_METHOD_EXIT|[5]|system.ListIterator.hasNext()
09:55:46.106 (106116000)|SYSTEM_METHOD_ENTRY|[11]|MAP.put(ANY, ANY)
09:55:46.106 (106132000)|SYSTEM_METHOD_EXIT|[11]|MAP.put(ANY, ANY)
09:55:46.106 (106144000)|SYSTEM_METHOD_ENTRY|[5]|system.ListIterator.hasNext()
09:55:46.106 (106157000)|SYSTEM_METHOD_EXIT|[5]|system.ListIterator.hasNext()
09:55:46.106 (106194000)|SYSTEM_METHOD_ENTRY|[19]|MAP.keySet()
09:55:46.106 (106250000)|SYSTEM_METHOD_EXIT|[19]|MAP.keySet()
09:55:46.106 (106508000)|SOQL_EXECUTE_BEGIN|[19]|Aggregations:0|select Id, Company_No__c from Account 
09:55:46.328 (328324000)|SOQL_EXECUTE_END|[19]|Rows:2
09:55:46.328 (328438000)|SYSTEM_METHOD_ENTRY|[19]|Database.QueryLocator.iterator()
09:55:46.328 (328611000)|SYSTEM_METHOD_ENTRY|[7]|QueryLocatorIterator.QueryLocatorIterator()
09:55:46.328 (328642000)|SYSTEM_METHOD_EXIT|[7]|QueryLocatorIterator
09:55:46.328 (328797000)|SYSTEM_METHOD_EXIT|[19]|Database.QueryLocator.iterator()
09:55:46.328 (328819000)|SYSTEM_METHOD_ENTRY|[19]|Database.QueryLocatorIterator.hasNext()
09:55:46.328 (328901000)|SYSTEM_METHOD_ENTRY|[33]|LIST<Account>.size()
09:55:46.328 (328937000)|SYSTEM_METHOD_EXIT|[33]|LIST<Account>.size()
09:55:46.328 (328953000)|SYSTEM_METHOD_EXIT|[19]|Database.QueryLocatorIterator.hasNext()
09:55:46.328 (328966000)|SYSTEM_METHOD_ENTRY|[19]|Database.QueryLocatorIterator.next()
09:55:46.328 (328984000)|SYSTEM_METHOD_ENTRY|[48]|Database.QueryLocatorIterator.hasNext()
09:55:46.329 (329005000)|SYSTEM_METHOD_ENTRY|[33]|LIST<Account>.size()
09:55:46.329 (329017000)|SYSTEM_METHOD_EXIT|[33]|LIST<Account>.size()
09:55:46.329 (329029000)|SYSTEM_METHOD_EXIT|[48]|Database.QueryLocatorIterator.hasNext()
09:55:46.329 (329073000)|SYSTEM_METHOD_EXIT|[19]|Database.QueryLocatorIterator.next()
09:55:46.329 (329105000)|SYSTEM_METHOD_ENTRY|[22]|System.debug(ANY)
09:55:46.329 (329170000)|USER_DEBUG|[22]|DEBUG|Account:{Company_No__c=C07_004117, Id=001M000000JJuDSIA1}
09:55:46.329 (329183000)|SYSTEM_METHOD_EXIT|[22]|System.debug(ANY)
09:55:46.329 (329212000)|SYSTEM_METHOD_ENTRY|[23]|MAP.keySet()
09:55:46.329 (329247000)|SYSTEM_METHOD_EXIT|[23]|MAP.keySet()
09:55:46.329 (329260000)|SYSTEM_METHOD_ENTRY|[23]|System.debug(ANY)
09:55:46.329 (329280000)|USER_DEBUG|[23]|DEBUG|{C07_004117, C07_004317}
09:55:46.329 (329289000)|SYSTEM_METHOD_EXIT|[23]|System.debug(ANY)
09:55:46.329 (329348000)|SYSTEM_METHOD_ENTRY|[25]|MAP.get(ANY)
09:55:46.329 (329394000)|SYSTEM_METHOD_EXIT|[25]|MAP.get(ANY)
09:55:46.329 (329514000)|SYSTEM_METHOD_ENTRY|[19]|Database.QueryLocatorIterator.hasNext()
09:55:46.329 (329540000)|SYSTEM_METHOD_ENTRY|[33]|LIST<Account>.size()
09:55:46.329 (329551000)|SYSTEM_METHOD_EXIT|[33]|LIST<Account>.size()
09:55:46.329 (329563000)|SYSTEM_METHOD_EXIT|[19]|Database.QueryLocatorIterator.hasNext()
09:55:46.329 (329572000)|SYSTEM_METHOD_ENTRY|[19]|Database.QueryLocatorIterator.next()
09:55:46.329 (329585000)|SYSTEM_METHOD_ENTRY|[48]|Database.QueryLocatorIterator.hasNext()
09:55:46.329 (329599000)|SYSTEM_METHOD_ENTRY|[33]|LIST<Account>.size()
09:55:46.329 (329610000)|SYSTEM_METHOD_EXIT|[33]|LIST<Account>.size()
09:55:46.329 (329621000)|SYSTEM_METHOD_EXIT|[48]|Database.QueryLocatorIterator.hasNext()
09:55:46.329 (329653000)|SYSTEM_METHOD_EXIT|[19]|Database.QueryLocatorIterator.next()
09:55:46.329 (329669000)|SYSTEM_METHOD_ENTRY|[22]|System.debug(ANY)
09:55:46.329 (329703000)|USER_DEBUG|[22]|DEBUG|Account:{Company_No__c=C07_004317, Id=001M000000JJuGgIAL}
09:55:46.329 (329714000)|SYSTEM_METHOD_EXIT|[22]|System.debug(ANY)
09:55:46.329 (329729000)|SYSTEM_METHOD_ENTRY|[23]|MAP.keySet()
09:55:46.329 (329746000)|SYSTEM_METHOD_EXIT|[23]|MAP.keySet()
09:55:46.329 (329757000)|SYSTEM_METHOD_ENTRY|[23]|System.debug(ANY)
09:55:46.329 (329772000)|USER_DEBUG|[23]|DEBUG|{C07_004117, C07_004317}
09:55:46.329 (329781000)|SYSTEM_METHOD_EXIT|[23]|System.debug(ANY)
09:55:46.329 (329802000)|SYSTEM_METHOD_ENTRY|[25]|MAP.get(ANY)
09:55:46.329 (329822000)|SYSTEM_METHOD_EXIT|[25]|MAP.get(ANY)
09:55:46.329 (329895000)|SYSTEM_METHOD_ENTRY|[19]|Database.QueryLocatorIterator.hasNext()
09:55:46.329 (329913000)|SYSTEM_METHOD_EXIT|[19]|Database.QueryLocatorIterator.hasNext()
09:55:47.219 (329938000)|CUMULATIVE_LIMIT_USAGE
09:55:47.219|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 1 out of 100
  Number of query rows: 2 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Number of script statements: 14 out of 200000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10

09:55:47.219|CUMULATIVE_LIMIT_USAGE_END

09:55:46.330 (330104000)|CODE_UNIT_FINISHED|salesDataSellToCustomerTrigger on Sales_Data trigger event BeforeInsert for [new, new, new, new, new]
09:55:46.338 (338419000)|CODE_UNIT_STARTED|[EXTERNAL]|01qM00000008cBh|salesDataBillToCustomerTrigger on Sales_Data trigger event BeforeInsert for [new, new, new, new, new]
09:55:46.338 (338649000)|SYSTEM_METHOD_ENTRY|[5]|LIST<Sales_Data__c>.iterator()
09:55:46.338 (338685000)|SYSTEM_METHOD_EXIT|[5]|LIST<Sales_Data__c>.iterator()
09:55:46.338 (338702000)|SYSTEM_METHOD_ENTRY|[5]|system.ListIterator.hasNext()
09:55:46.338 (338721000)|SYSTEM_METHOD_EXIT|[5]|system.ListIterator.hasNext()
09:55:46.338 (338815000)|SYSTEM_METHOD_ENTRY|[11]|MAP.put(ANY, ANY)
09:55:46.338 (338851000)|SYSTEM_METHOD_EXIT|[11]|MAP.put(ANY, ANY)
09:55:46.338 (338865000)|SYSTEM_METHOD_ENTRY|[5]|system.ListIterator.hasNext()
09:55:46.338 (338880000)|SYSTEM_METHOD_EXIT|[5]|system.ListIterator.hasNext()
09:55:46.338 (338925000)|SYSTEM_METHOD_ENTRY|[11]|MAP.put(ANY, ANY)
09:55:46.338 (338946000)|SYSTEM_METHOD_EXIT|[11]|MAP.put(ANY, ANY)
09:55:46.338 (338960000)|SYSTEM_METHOD_ENTRY|[5]|system.ListIterator.hasNext()
09:55:46.338 (338974000)|SYSTEM_METHOD_EXIT|[5]|system.ListIterator.hasNext()
09:55:46.339 (339013000)|SYSTEM_METHOD_ENTRY|[11]|MAP.put(ANY, ANY)
09:55:46.339 (339034000)|SYSTEM_METHOD_EXIT|[11]|MAP.put(ANY, ANY)
09:55:46.339 (339048000)|SYSTEM_METHOD_ENTRY|[5]|system.ListIterator.hasNext()
09:55:46.339 (339062000)|SYSTEM_METHOD_EXIT|[5]|system.ListIterator.hasNext()
09:55:46.339 (339102000)|SYSTEM_METHOD_ENTRY|[11]|MAP.put(ANY, ANY)
09:55:46.339 (339121000)|SYSTEM_METHOD_EXIT|[11]|MAP.put(ANY, ANY)
09:55:46.339 (339134000)|SYSTEM_METHOD_ENTRY|[5]|system.ListIterator.hasNext()
09:55:46.339 (339148000)|SYSTEM_METHOD_EXIT|[5]|system.ListIterator.hasNext()
09:55:46.339 (339187000)|SYSTEM_METHOD_ENTRY|[11]|MAP.put(ANY, ANY)
09:55:46.339 (339203000)|SYSTEM_METHOD_EXIT|[11]|MAP.put(ANY, ANY)
09:55:46.339 (339216000)|SYSTEM_METHOD_ENTRY|[5]|system.ListIterator.hasNext()
09:55:46.339 (339229000)|SYSTEM_METHOD_EXIT|[5]|system.ListIterator.hasNext()
09:55:46.339 (339265000)|SYSTEM_METHOD_ENTRY|[19]|MAP.keySet()
09:55:46.339 (339299000)|SYSTEM_METHOD_EXIT|[19]|MAP.keySet()
09:55:46.339 (339529000)|SOQL_EXECUTE_BEGIN|[19]|Aggregations:0|select Id, Company_No__c from Account 
09:55:46.375 (375753000)|SOQL_EXECUTE_END|[19]|Rows:2
09:55:46.375 (375826000)|SYSTEM_METHOD_ENTRY|[19]|Database.QueryLocator.iterator()
09:55:46.375 (375972000)|SYSTEM_METHOD_EXIT|[19]|Database.QueryLocator.iterator()
09:55:46.375 (375994000)|SYSTEM_METHOD_ENTRY|[19]|Database.QueryLocatorIterator.hasNext()
09:55:46.376 (376018000)|SYSTEM_METHOD_ENTRY|[33]|LIST<Account>.size()
09:55:46.376 (376032000)|SYSTEM_METHOD_EXIT|[33]|LIST<Account>.size()
09:55:46.376 (376044000)|SYSTEM_METHOD_EXIT|[19]|Database.QueryLocatorIterator.hasNext()
09:55:46.376 (376056000)|SYSTEM_METHOD_ENTRY|[19]|Database.QueryLocatorIterator.next()
09:55:46.376 (376070000)|SYSTEM_METHOD_ENTRY|[48]|Database.QueryLocatorIterator.hasNext()
09:55:46.376 (376084000)|SYSTEM_METHOD_ENTRY|[33]|LIST<Account>.size()
09:55:46.376 (376095000)|SYSTEM_METHOD_EXIT|[33]|LIST<Account>.size()
09:55:46.376 (376106000)|SYSTEM_METHOD_EXIT|[48]|Database.QueryLocatorIterator.hasNext()
09:55:46.376 (376139000)|SYSTEM_METHOD_EXIT|[19]|Database.QueryLocatorIterator.next()
09:55:46.376 (376183000)|SYSTEM_METHOD_ENTRY|[21]|MAP.get(ANY)
09:55:46.376 (376219000)|SYSTEM_METHOD_EXIT|[21]|MAP.get(ANY)
09:55:46.376 (376321000)|SYSTEM_METHOD_ENTRY|[19]|Database.QueryLocatorIterator.hasNext()
09:55:46.376 (376344000)|SYSTEM_METHOD_ENTRY|[33]|LIST<Account>.size()
09:55:46.376 (376359000)|SYSTEM_METHOD_EXIT|[33]|LIST<Account>.size()
09:55:46.376 (376373000)|SYSTEM_METHOD_EXIT|[19]|Database.QueryLocatorIterator.hasNext()
09:55:46.376 (376383000)|SYSTEM_METHOD_ENTRY|[19]|Database.QueryLocatorIterator.next()
09:55:46.376 (376396000)|SYSTEM_METHOD_ENTRY|[48]|Database.QueryLocatorIterator.hasNext()
09:55:46.376 (376411000)|SYSTEM_METHOD_ENTRY|[33]|LIST<Account>.size()
09:55:46.376 (376421000)|SYSTEM_METHOD_EXIT|[33]|LIST<Account>.size()
09:55:46.376 (376432000)|SYSTEM_METHOD_EXIT|[48]|Database.QueryLocatorIterator.hasNext()
09:55:46.376 (376459000)|SYSTEM_METHOD_EXIT|[19]|Database.QueryLocatorIterator.next()
09:55:46.376 (376486000)|SYSTEM_METHOD_ENTRY|[21]|MAP.get(ANY)
09:55:46.376 (376505000)|SYSTEM_METHOD_EXIT|[21]|MAP.get(ANY)
09:55:46.376 (376580000)|SYSTEM_METHOD_ENTRY|[19]|Database.QueryLocatorIterator.hasNext()
09:55:46.376 (376598000)|SYSTEM_METHOD_EXIT|[19]|Database.QueryLocatorIterator.hasNext()
09:55:47.266 (376621000)|CUMULATIVE_LIMIT_USAGE
09:55:47.266|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 2 out of 100
  Number of query rows: 4 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Number of script statements: 24 out of 200000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10

09:55:47.266|CUMULATIVE_LIMIT_USAGE_END

09:55:46.376 (376782000)|CODE_UNIT_FINISHED|salesDataBillToCustomerTrigger on Sales_Data trigger event BeforeInsert for [new, new, new, new, new]
09:55:46.962 (962820000)|CODE_UNIT_FINISHED|TRIGGERS
09:55:46.962 (962839000)|EXECUTION_FINISHED

 

JayseJayse

 

Debug log from single update (executed through the browser application)

 

25.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
10:02:12.055 (55556000)|EXECUTION_STARTED
10:02:12.055 (55598000)|CODE_UNIT_STARTED|[EXTERNAL]|TRIGGERS
10:02:12.055 (55621000)|CODE_UNIT_STARTED|[EXTERNAL]|01qM00000008cBi|salesDataSellToCustomerTrigger on Sales_Data trigger event BeforeUpdate for [a0YM0000002yM6E]
10:02:12.056 (56829000)|SYSTEM_METHOD_ENTRY|[5]|LIST<Sales_Data__c>.iterator()
10:02:12.057 (57086000)|SYSTEM_METHOD_EXIT|[5]|LIST<Sales_Data__c>.iterator()
10:02:12.057 (57118000)|SYSTEM_METHOD_ENTRY|[5]|system.ListIterator.hasNext()
10:02:12.057 (57142000)|SYSTEM_METHOD_EXIT|[5]|system.ListIterator.hasNext()
10:02:12.057 (57248000)|SYSTEM_METHOD_ENTRY|[11]|MAP.put(ANY, ANY)
10:02:12.057 (57286000)|SYSTEM_METHOD_EXIT|[11]|MAP.put(ANY, ANY)
10:02:12.057 (57300000)|SYSTEM_METHOD_ENTRY|[5]|system.ListIterator.hasNext()
10:02:12.057 (57314000)|SYSTEM_METHOD_EXIT|[5]|system.ListIterator.hasNext()
10:02:12.057 (57350000)|SYSTEM_METHOD_ENTRY|[19]|MAP.keySet()
10:02:12.057 (57407000)|SYSTEM_METHOD_EXIT|[19]|MAP.keySet()
10:02:12.062 (62403000)|SOQL_EXECUTE_BEGIN|[19]|Aggregations:0|select Id, Company_No__c from Account 
10:02:12.086 (86401000)|SOQL_EXECUTE_END|[19]|Rows:1
10:02:12.086 (86466000)|SYSTEM_METHOD_ENTRY|[19]|Database.QueryLocator.iterator()
10:02:12.086 (86590000)|SYSTEM_METHOD_ENTRY|[7]|QueryLocatorIterator.QueryLocatorIterator()
10:02:12.086 (86613000)|SYSTEM_METHOD_EXIT|[7]|QueryLocatorIterator
10:02:12.086 (86732000)|SYSTEM_METHOD_EXIT|[19]|Database.QueryLocator.iterator()
10:02:12.086 (86753000)|SYSTEM_METHOD_ENTRY|[19]|Database.QueryLocatorIterator.hasNext()
10:02:12.086 (86821000)|SYSTEM_METHOD_ENTRY|[33]|LIST<Account>.size()
10:02:12.086 (86855000)|SYSTEM_METHOD_EXIT|[33]|LIST<Account>.size()
10:02:12.086 (86869000)|SYSTEM_METHOD_EXIT|[19]|Database.QueryLocatorIterator.hasNext()
10:02:12.086 (86880000)|SYSTEM_METHOD_ENTRY|[19]|Database.QueryLocatorIterator.next()
10:02:12.086 (86897000)|SYSTEM_METHOD_ENTRY|[48]|Database.QueryLocatorIterator.hasNext()
10:02:12.086 (86920000)|SYSTEM_METHOD_ENTRY|[33]|LIST<Account>.size()
10:02:12.086 (86932000)|SYSTEM_METHOD_EXIT|[33]|LIST<Account>.size()
10:02:12.086 (86942000)|SYSTEM_METHOD_EXIT|[48]|Database.QueryLocatorIterator.hasNext()
10:02:12.086 (86979000)|SYSTEM_METHOD_EXIT|[19]|Database.QueryLocatorIterator.next()
10:02:12.087 (87004000)|SYSTEM_METHOD_ENTRY|[22]|System.debug(ANY)
10:02:12.087 (87059000)|USER_DEBUG|[22]|DEBUG|Account:{Company_No__c=C07_004117, Id=001M000000JJuDSIA1}
10:02:12.087 (87072000)|SYSTEM_METHOD_EXIT|[22]|System.debug(ANY)
10:02:12.087 (87097000)|SYSTEM_METHOD_ENTRY|[23]|MAP.keySet()
10:02:12.087 (87129000)|SYSTEM_METHOD_EXIT|[23]|MAP.keySet()
10:02:12.087 (87142000)|SYSTEM_METHOD_ENTRY|[23]|System.debug(ANY)
10:02:12.087 (87158000)|USER_DEBUG|[23]|DEBUG|{C07_004117}
10:02:12.087 (87166000)|SYSTEM_METHOD_EXIT|[23]|System.debug(ANY)
10:02:12.087 (87216000)|SYSTEM_METHOD_ENTRY|[25]|MAP.get(ANY)
10:02:12.087 (87255000)|SYSTEM_METHOD_EXIT|[25]|MAP.get(ANY)
10:02:12.087 (87356000)|SYSTEM_METHOD_ENTRY|[19]|Database.QueryLocatorIterator.hasNext()
10:02:12.087 (87376000)|SYSTEM_METHOD_EXIT|[19]|Database.QueryLocatorIterator.hasNext()
10:02:12.380 (87398000)|CUMULATIVE_LIMIT_USAGE
10:02:12.380|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 1 out of 100
  Number of query rows: 1 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Number of script statements: 6 out of 200000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10

10:02:12.380|CUMULATIVE_LIMIT_USAGE_END

10:02:12.087 (87523000)|CODE_UNIT_FINISHED|salesDataSellToCustomerTrigger on Sales_Data trigger event BeforeUpdate for [a0YM0000002yM6E]
10:02:12.094 (94560000)|CODE_UNIT_STARTED|[EXTERNAL]|01qM00000008cBh|salesDataBillToCustomerTrigger on Sales_Data trigger event BeforeUpdate for [a0YM0000002yM6E]
10:02:12.094 (94760000)|SYSTEM_METHOD_ENTRY|[5]|LIST<Sales_Data__c>.iterator()
10:02:12.094 (94797000)|SYSTEM_METHOD_EXIT|[5]|LIST<Sales_Data__c>.iterator()
10:02:12.094 (94813000)|SYSTEM_METHOD_ENTRY|[5]|system.ListIterator.hasNext()
10:02:12.094 (94829000)|SYSTEM_METHOD_EXIT|[5]|system.ListIterator.hasNext()
10:02:12.094 (94911000)|SYSTEM_METHOD_ENTRY|[11]|MAP.put(ANY, ANY)
10:02:12.094 (94945000)|SYSTEM_METHOD_EXIT|[11]|MAP.put(ANY, ANY)
10:02:12.094 (94958000)|SYSTEM_METHOD_ENTRY|[5]|system.ListIterator.hasNext()
10:02:12.094 (94972000)|SYSTEM_METHOD_EXIT|[5]|system.ListIterator.hasNext()
10:02:12.095 (95007000)|SYSTEM_METHOD_ENTRY|[19]|MAP.keySet()
10:02:12.095 (95039000)|SYSTEM_METHOD_EXIT|[19]|MAP.keySet()
10:02:12.095 (95266000)|SOQL_EXECUTE_BEGIN|[19]|Aggregations:0|select Id, Company_No__c from Account 
10:02:12.108 (108653000)|SOQL_EXECUTE_END|[19]|Rows:1
10:02:12.108 (108705000)|SYSTEM_METHOD_ENTRY|[19]|Database.QueryLocator.iterator()
10:02:12.108 (108817000)|SYSTEM_METHOD_EXIT|[19]|Database.QueryLocator.iterator()
10:02:12.108 (108835000)|SYSTEM_METHOD_ENTRY|[19]|Database.QueryLocatorIterator.hasNext()
10:02:12.108 (108858000)|SYSTEM_METHOD_ENTRY|[33]|LIST<Account>.size()
10:02:12.108 (108870000)|SYSTEM_METHOD_EXIT|[33]|LIST<Account>.size()
10:02:12.108 (108882000)|SYSTEM_METHOD_EXIT|[19]|Database.QueryLocatorIterator.hasNext()
10:02:12.108 (108892000)|SYSTEM_METHOD_ENTRY|[19]|Database.QueryLocatorIterator.next()
10:02:12.108 (108905000)|SYSTEM_METHOD_ENTRY|[48]|Database.QueryLocatorIterator.hasNext()
10:02:12.108 (108920000)|SYSTEM_METHOD_ENTRY|[33]|LIST<Account>.size()
10:02:12.108 (108930000)|SYSTEM_METHOD_EXIT|[33]|LIST<Account>.size()
10:02:12.108 (108941000)|SYSTEM_METHOD_EXIT|[48]|Database.QueryLocatorIterator.hasNext()
10:02:12.108 (108970000)|SYSTEM_METHOD_EXIT|[19]|Database.QueryLocatorIterator.next()
10:02:12.109 (109009000)|SYSTEM_METHOD_ENTRY|[21]|MAP.get(ANY)
10:02:12.109 (109047000)|SYSTEM_METHOD_EXIT|[21]|MAP.get(ANY)
10:02:12.109 (109129000)|SYSTEM_METHOD_ENTRY|[19]|Database.QueryLocatorIterator.hasNext()
10:02:12.109 (109148000)|SYSTEM_METHOD_EXIT|[19]|Database.QueryLocatorIterator.hasNext()
10:02:12.402 (109168000)|CUMULATIVE_LIMIT_USAGE
10:02:12.402|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 2 out of 100
  Number of query rows: 2 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Number of script statements: 10 out of 200000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10

10:02:12.402|CUMULATIVE_LIMIT_USAGE_END

10:02:12.109 (109286000)|CODE_UNIT_FINISHED|salesDataBillToCustomerTrigger on Sales_Data trigger event BeforeUpdate for [a0YM0000002yM6E]
10:02:12.135 (135551000)|CODE_UNIT_FINISHED|TRIGGERS
10:02:12.135 (135567000)|EXECUTION_FINISHED

 

JayseJayse

think i've solved it, albeit by starting over, using the following forum posting as a guide: forum posting

 

 //loop through SalesData make a set of Sell_to_Customer_No__c 
   Set<String> salesDataListSellToCustomerNo = new Set<String>();  
   for(Sales_Data__c x : Trigger.new) {
       
       if (x.Sell_to_Customer_No__c == null ) {
           x.Sell_to_Customer_Id__c = null;
       }
       else {
           salesDataListSellToCustomerNo.add(x.Sell_to_Customer_No__c );
       }
   }
        
   //Retrieve all Accounts in one query
   List<Account> accountList =  [SELECT Id, Company_No__c FROM Account
                     WHERE Company_No__c IN :salesDataListSellToCustomerNo];
  
   //Loop through Accounts List, create map for <Company_No__c , Id (from account)>  
   Map<String,String> companyNoAccIdMap = new Map<String,String>();
   
   Integer i = 0;
        for(Account accountTest : accountList ){
           companyNoAccIdMap.put(accountList.get(i).Company_No__c, accountList.get(i).Id);
           i ++;
        }
    
   //now again loop through Sales_Data__c and update Sell_to_Customer_ID__c 
   for(Sales_Data__c sd:  Trigger.new) {       
        if (sd.Sell_to_Customer_No__c != null ) {
            String sellToCustId = companyNoAccIdMap.get(sd.Sell_to_Customer_No__c);
            sd.Sell_to_Customer_ID__c = sellToCustId;
        }
   }  

 

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.

 

   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);
       }
   }

 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).