Sub topic 13
Running Data Loader from the command line
This chapter covers Data Loader's batch mode. In batch mode you describe an operation in configuration files and run it with one command, so a scheduler can run it without anyone clicking through the wizard. You should already know the insert, update, upsert, delete, and export operations. You should also know how field mapping, success files, and error files work in the Data Loader window. The examples assume Windows, where batch mode has traditionally been supported.
When batch mode is worth it
The interactive wizard suits one-off loads. Batch mode suits jobs that repeat on a schedule or run unattended, for example:
- A nightly export of changed Accounts to a file that another system picks up.
- A daily insert of a CSV file that an upstream system drops into a folder.
- A scripted sandbox refresh step that reloads test data.
Batch mode costs you more setup. You write XML, manage an encryption key, and read log files instead of watching a progress bar. For a job you will run once or twice, use the interface.
The pieces of a batch run
A batch run uses a configuration folder and a launcher script. The script reads the folder and runs one named process.
| Item | Purpose |
|---|---|
| `process.bat` | Launcher in Data Loader's `bin` folder. It takes a configuration folder and a process name. |
| `process-conf.xml` | Defines each process as a named bean with its settings. |
| `config.properties` | Default settings, such as batch size and endpoint. Settings in `process-conf.xml` override it. |
| Mapping file (`.sdl`) | Maps CSV column names to Salesforce field API names. |
| Key file | Holds the key used to encrypt and decrypt the stored password. |
| `encrypt.bat` | Creates the key file and encrypts a password. |
The launcher takes the configuration folder first and the process name second:
bin\process.bat "C:\dataloader\conf" accountInsertData Loader finds the bean whose id is accountInsert in C:\dataloader\conf\process-conf.xml. It applies that bean's settings on top of config.properties, then runs the operation.
Step 1: Encrypt the password
Do not store a plain-text password in process-conf.xml. Create a key file, then encrypt the password with it.
bin\encrypt.bat -k C:\dataloader\conf\dataloader.key
bin\encrypt.bat -e "MyPasswordAndToken" C:\dataloader\conf\dataloader.keyThe first command writes a key file. The second prints an encrypted string that you paste into the configuration. If your org requires a security token for API logins, append the token to the password before encrypting, with no space between them. Re-encrypt whenever the password changes, because the stored value stops working.
Newer Data Loader releases have moved login toward OAuth, and password-based API logins may be restricted in your org. If the login step fails, check the command-line documentation for your Data Loader version to see which authentication settings it expects.
Step 2: Write the mapping file
A mapping file is a plain text file with one CSV column=Salesforce field pair per line. Using the insert file from earlier in the tutorial, save this as accountInsertMap.sdl:
# CSV column = Salesforce field API name
Name=Name
Phone=Phone
Industry=Industry
Website=WebsiteThe left side must match the CSV header exactly. The right side is the field API name, so custom fields end in __c. You can create this file in the Data Loader window by mapping fields and choosing Save Mapping. That is usually quicker than typing it, and it avoids typos in field names.
Step 3: Define the process
Each process is a bean in process-conf.xml. This file defines an insert and an export.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="accountInsert"
class="com.salesforce.dataloader.process.ProcessRunner"
singleton="false">
<description>Insert accounts from accounts.csv</description>
<property name="name" value="accountInsert"/>
<property name="configOverrideMap">
<map>
<entry key="sfdc.endpoint" value="https://test.salesforce.com"/>
<entry key="sfdc.username" value="[email protected]"/>
<entry key="sfdc.password" value="PASTE_ENCRYPTED_PASSWORD_HERE"/>
<entry key="process.encryptionKeyFile" value="C:\dataloader\conf\dataloader.key"/>
<entry key="sfdc.timeoutSecs" value="600"/>
<entry key="sfdc.loadBatchSize" value="200"/>
<entry key="sfdc.entity" value="Account"/>
<entry key="process.operation" value="insert"/>
<entry key="process.mappingFile" value="C:\dataloader\conf\accountInsertMap.sdl"/>
<entry key="dataAccess.type" value="csvRead"/>
<entry key="dataAccess.name" value="C:\dataloader\data\accounts.csv"/>
<entry key="process.outputSuccess" value="C:\dataloader\out\accountInsert_success.csv"/>
<entry key="process.outputError" value="C:\dataloader\out\accountInsert_error.csv"/>
</map>
</property>
</bean>
<bean id="accountExport"
class="com.salesforce.dataloader.process.ProcessRunner"
singleton="false">
<description>Export Energy accounts to CSV</description>
<property name="name" value="accountExport"/>
<property name="configOverrideMap">
<map>
<entry key="sfdc.endpoint" value="https://test.salesforce.com"/>
<entry key="sfdc.username" value="[email protected]"/>
<entry key="sfdc.password" value="PASTE_ENCRYPTED_PASSWORD_HERE"/>
<entry key="process.encryptionKeyFile" value="C:\dataloader\conf\dataloader.key"/>
<entry key="sfdc.timeoutSecs" value="600"/>
<entry key="sfdc.entity" value="Account"/>
<entry key="process.operation" value="extract"/>
<entry key="sfdc.extractionSOQL" value="SELECT Id, Name, Phone, Industry FROM Account WHERE Industry = 'Energy'"/>
<entry key="dataAccess.type" value="csvWrite"/>
<entry key="dataAccess.name" value="C:\dataloader\out\energy_accounts.csv"/>
</map>
</property>
</bean>
</beans>The export bean reuses the SOQL query from the export example earlier in the chapter. Note these details:
process.operationtakesinsert,update,upsert,delete,hard_delete, orextract. An export that includes deleted records usesextract_all.dataAccess.typeiscsvReadwhen the CSV is the input andcsvWritewhen it is the output.- For upsert, add
sfdc.externalIdFieldwith the API name of the external ID field. For example, use a value such asLegacy_Id__c. - To use the Bulk API, set
sfdc.useBulkApitotrueand raisesfdc.loadBatchSize. Batch limits differ between the SOAP and Bulk APIs, so check the limits for your API version. - The endpoint for a sandbox is
https://test.salesforce.com. For production and Developer Edition orgs it ishttps://login.salesforce.com. - Use a one-line SOQL string. Data Loader reads the value as written.
Step 4: Run it and check the result
Run the process from a command prompt first, not from a scheduler:
bin\process.bat "C:\dataloader\conf" accountInsertThen check three things:
- The console output or log file. It should report the number of successes and errors and no login failure.
- The success file, which lists each created record with its new Salesforce ID.
- The error file, which lists each failed row with the reason.
The success and error files have the same format as the ones from the interactive tool. You fix failed rows the same way: correct the data in the error file, remove the error column, and load it again.
Step 5: Schedule it
Data Loader has no built-in scheduler. Use the operating system's scheduler. This Windows command runs the export every day at 02:00:
schtasks /Create /SC DAILY /ST 02:00 /TN "AccountExport" /TR "C:\dataloader\bin\process.bat C:\dataloader\conf accountExport"Run the task under the service account that owns the key file, and confirm the path to java is available to that account. A task that works in your own prompt can fail under the scheduler because its environment differs. Missing Java and a different working folder are the usual causes.
Common errors
| Symptom | Likely cause |
|---|---|
| Login fails with `INVALID_LOGIN` | The security token was left out of the encrypted password, the endpoint is wrong, or the password changed after encryption. |
| Password cannot be decrypted | The key file path is wrong, or the password was encrypted with a different key file. |
| Process name not found | The `id` in `process-conf.xml` does not match the name given on the command line. Names are case sensitive. |
| Every row fails with an invalid field error | A mapping line names a field by label, not by API name, or a CSV header has extra spaces. |
| No output files appear | The output folders do not exist. Data Loader does not create them for you. |
| Works by hand, fails when scheduled | The scheduled account lacks file permissions, Java, or access to the key file. |
Safe practice for unattended jobs
Everything from the safe-import chapter applies, and unattended runs add risks, because nobody is watching when something goes wrong.
- Build and test each process against a sandbox endpoint, then change the endpoint and username for production.
- Keep a separate bean per operation. Do not reuse one bean for both an insert and a delete by editing it.
- Treat
deleteandhard_deleteprocesses with special care. Run them manually, never on a schedule, unless a downstream check guards the input file. - Keep dated copies of the success and error files so you can trace what a job changed.
Exercise
A nightly Data Loader upsert job runs from Task Scheduler. The scheduler shows "success" every night, but a user reports that some records never updated. Where do you look first, and why?
Show answer
Look at the error file for the process (the file named in process.outputError). A batch run exits normally even when individual rows fail, so the scheduler reports success. The error file lists each failed row with the reason, such as a validation rule failure or a missing external ID value. Fix the data, remove the error column, and reload only the failed rows. Then add a check to the job that flags a non-empty error file.
Exercise
You need a process that updates Account phone numbers from phones.csv, where the CSV has the columns Id and Phone. Which three keys in the configOverrideMap differ from the insert example, and what do they become?
Show answer
process.operation becomes update. dataAccess.name points to phones.csv. process.mappingFile points to a mapping file containing Id=Id and Phone=Phone. The Id column is required for an update because it identifies which existing records change. The success and error output paths should also get new names so they do not overwrite the insert job's files.