I'm writing a trigger on account to set a custom field

account.Data_Source__c = 'ERP';

for the accounts imported with the data loader from command line .

The problem is that my trigger works also for account created manually.

is there a way in apex to understand if you are getting data from the data loader?

I want that only the records imported have DataSource=Erp.

Thanks in advantage for any advice.


  • Could you not just explicitly set the Data_Source__c field to 'ERP' in the data loader? – Daniel Ballinger Jun 9 '13 at 19:17

There is no "magic" way that I know of to differentiate between records imported via the data loader and records created manually. There are, however, a number of techniques that you may be able to use:

  1. Use an account field - when you use the data loader, set a field on the account to a specific value that you can detect during the trigger. The disadvantage of this approach is that it requires you to manually set that field value on your input data set.

  2. When using the data loader, always use a specific user account. Then your trigger could differentiate based on the running user. The disadvantage of this approach is that you need to be careful to always run the data loader on that account. However, if you are using the command line and perhaps using a batch file that everyone shares, this could be workable.

  3. If you are certain that every data set loaded from the command line will have more than one record, and no other import mechanism exists, you could based your decision on the size of the batch being processed. If you take this approach, be careful to handle edge conditions (for example: a batch of 201 being broken into one set of 200 and a set of 1 that would be misinterpreted as a single insert).

  4. Override the 'New' Account button to create a custom button that pre-populates a custom field on the account. This is the "flip" approach where instead of identifying data loader imports, you identify manual inserts. The catch here is that it won't help you differentiate between data loader imports and imports using other non-UI approaches.

Anyway, one of these might be suitable for your application. I look forward to seeing if anyone has any other suggestions.

  • thank you very much,you are an expert! I'm interested in the third solution.In my trigger, i can check the size for Trigger.New. If size is greater than one, then set the data field as I want. do you know if is there a way to avoid the edge conditions? Thanks again. – Enry Jun 8 '13 at 20:24

You can also try querying the LoginHistory table with something like

Blockquote SELECT ApiType,Application,Browser,LoginTime,LoginType,Status,UserId FROM LoginHistory where UserId =: loggedInUser order by LoginTime desc limit 1

You can then check if the logintype of the returned result set is "Other Apex API" and Browser is "Java (Salesforce.com)".

If both condition are satisfied you can assume that the record is inserted by data loader.

However this approach has a draw back. If a user log's in to data loader then he log's in to the salesforce org from webbrowser and then try to insert record from data loader the result would be incorrect.

Overall i would suggest to create a separate user with api enabled permission to use for data loading purpose.

  • 1
    i have an Erp user, so yours solution should be the best! Thanks,you are very kind! – Enry Jun 8 '13 at 20:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.