Home > Blog > Data Loader With Command Line

Data Loader With Command Line

Overview – Blog holds the explanation of accessing data loader using Command Line and inserting records.
Problem – It is usually the requirement that we need some automated process that can automatically insert all the records into your Salesforce Org.
Solution – The possible solution is that we can achieve the solution to above problem by using the command line Data–Loader. Note: – Command Line Data Loader is only supported for Windows. What is Data Loader? Data Loader is a client application for the bulk import or export of data. We can use it to insert, update, upsert, delete, export or export all of the SalesForce records. When importing data, it reads then extracts, and then loads data from comma-separated values (CSV) files or from a database connection. When exporting data, it outputs the data as CSV files.   You can use Data Loader in two different ways:
  • User interface — when you use the user interface, select the required action, CSV files used for import and export, and the field mappings that map the field names in your file with the field names in Salesforce to prepare an SDL File.
  • Command line (Windows only) — to make an automated process for data loader, when you use the command line, you have to specify the configuration i.e., data loader actions, data sources, mappings.
PROCESS: – Below are the following steps defined that show how to implement data loader with command line functionality: –
  • Step 1: Create the encryption key
  • Step 2: Create the encrypted password for the login username
  • Step 3: Create the SDL(Mapping) File
  • Step 4: Create a process-conf.xml file that contains the import configuration settings
  • Step 5: Run the process and import the data
Prerequisites for using CLI Data Loader: –
  1. Make sure you have downloaded data loader. If not, login to your Salesforce Org. Then Setup –> Data Management –> Data Loader. Choose the option “Download Data Loader for Windows “.
  2. Make sure you have the Admin rights on the machine.
  3. Also, you must have latest Java Run Time Environment. If not, you will have to download the Java Run Time Environment from Oracle site downloads page. Install the downloaded JRE file.
Check java version by entering following commands in Command Prompt: – “java -version”   Explanation: – Step 1: – Create the encryption key
  1. Open your C drive, then migrate to “bin” folder within “Salesforce.com > DataLoader” folder.
  2. Open Command Prompt(cmd), and navigate to the bin folder of data loader directory as shown in the image below.
  3. Use the command “bat -g anyKeyTextOfYourChoice”. You can use any text you want to use as an Encryption key. Once you press enter, you will find a key, copy it and save it in a text file. For example, I saved my key in “E:\CLI Test\encryptionKey.txt”.
Step 2: Create the encrypted password for your login username
  1. For creating your encrypted password, firstly we need to get our org’s “Security Token”. Login in to your Salesforce org, then “My Settings -> Personal -> Reset My Security Token -> Click on Reset Security Token Button” After this you will receive security token on your registered mail. Save this security token for future reference as this will used for generating the encrypted password.
  2. Now on command prompt, enter following command: – “encrypt.bat -e <yourOrgPassword> <location of the txt file with encrypted key>”“yourOrgPassword” will have to be the combination of your Org’s Password and the Security Token You Received in the mail.For example: – If My org’s password is “Test1234” and the security token I received on my mail is “iDIy7LozsaJDJ55A83i7qVpU”, and the path for my encrypted text file is “E:\CLI Test”.Then on command prompt: – bat -e Test1234iDIy7LozsaJDJ55A83i7qVpUE:\CLI Test\encryptionKey.txt” Copy this encrypted value of your password. Save it also in a .txt file.
Step 3: Create the Field Mapping File and the CSV file for inserting records Create the mapping same as shown below, by putting the CSV file header name on left side and Salesforce API name on right side and then save the file with “.sld” extension. For example: Name it “AccInsert.sdl”. Create a CSV file as shown below, with records and values of your choice. This is the file that we are going to Upload. For example: Name it “AccountInsert.csv”. Keep both files in the same directory. For Example, I stored in “E:\CLI Test”. Step 4: Create a process-conf.xml file that contains the import configuration settings
  1. Prepare a “process-conf.XML” and store it in same directory. For Example, I stored in “E:\CLI Test”.
Copy the following code given below: – <!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>accountInsert job gets the account record from the CSV file and inserts it into Salesforce.</description> <property name=”name” value=”accountInsert”/> <property name=”configOverrideMap”> <map> <entry key=”sfdc.debugMessages” value=”true”/> <entry key=”sfdc.debugMessagesFile” value=”E:\accountInsertSoapTrace.log”/> <entry key=”sfdc.endpoint” value=”https://login.salesforce.com”/> <entry key=”sfdc.username” value=”htiwari@kloudrac.com”/> <entry key=”sfdc.password”  value=”beaf4cbe88c41bde0cb2694375242gfgrs67128c66ab55cb1e945e20292bf7d0717222db5f61″/> <entry key=”process.encryptionKeyFile” value=”E:\CLI Test\encryptionKey.txt”/> <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=”E:\CLI Test\accInsert.sdl”/> <entry key=”dataAccess.name” value=”E:\CLI Test\AccountInsert.csv”/> <entry key=”process.outputSuccess” value=”E:\CLI Test\accountDataToInsertSuccess.csv”/> <entry key=”process.outputError” value=”E:\CLI Test\accountDataToInsertLogError.csv”/> <entry key=”dataAccess.type” value=”csvRead”/> <entry key=”process.initialLastRunDate” value=”2017-08-10T00:00:00.000-0800″/> </map> </property> </bean> </beans> Now, once you copy the code, make following changes: – – Your usernamePassword (encrypted)Instance NameSuccess error file locationChange directory path and edit it to the same path as in your system. Edit the process-conf.XML just as shown in the image above. Whereas,
  • sfdc.endpoint—Enter the URL of the Salesforce instance for your organization
  • sfdc.username—Enter the username Data Loader uses to log in
  • sfdc.password—Enter the encrypted password value that you created in step 2
  • process.encryptionKeyFile – Enter path for the encrypted key file(.txt)
  • sfdc.entity – Enter the object on which you want to insert data
  • process.operation – Enter the operation (insert, update, export) you want to perform, though here we have configured the “process-conf.XML” for insertion operation only.
  • process.mappingFile—Enter the path of the file and file name of the SDL file for mapping that we just created.
  • dataAccess.Name—Enter the path and file name of the data file that contains the accounts that you want to import(.CSV)
  • sfdc.debugMessages— Currently set to true for troubleshooting.
  • sfdc.debugMessagesFile—Enter the path and file name of the command line log file
  • process.outputSuccess—Enter the path and file name of the success log file
  • process.outputError—Enter the path and file name of the error log file
Step 5: Run the process and import the data On the same location on command prompt run the following command: process.bat “<file path to process-conf.xml>” <process name> For Example: – “<file path to process-conf.xml>”, for me the path is “E:\CLI Test”. And <process name> is, “accountInsert”. So the whole command would be: – process.bat “E:\CLI Test” accountInsert Enter the command in command prompt. If the insertion is successful you will see a screen like: – After successful execution at same folder where your other files were stored, a “accountDataToInsertLogError.csv” and “accountDataToInsertSuccess.csv” will also get created having records of all successful insertions and errors if any. Using windows task scheduler to automate the CLI Data Loader process:- To automate CLI Data Loader, using WINDOWS Task Scheduler, we need to create a batch (.bat) file where you have created “process-conf.xml” file, which we are going to invoke using Task Scheduler.
  1. Create a Batch File using the following code: –
@echo “off” cls cd “C:\Program Files (x86)\salesforce.com\Data Loader\bin” CALL process.bat “E:\CLI Test” “accountInsert” Pause
  • After cd, the path given is the path where the SalesForce – Data Loader has been installed.
  • CALL “your.bat file” “Path of Batch file” “Process Name in Process-conf.xml”>
    1. Save the file with extension “.bat”.
    2. After saving, open Windows – Task Scheduler.
4. Then click on Action tab -> Create Task 5. Provide any name to your task, choose if you want the task to run only when you are logged into the system, or when the system is not logged in, in that you case, the scheduler asks you to provide the System Password (if any). 6. Then Click on TRIGGERS tab – > New Choose a suitable time when you want your task to get executed. Then click on “OK”. 7. Then Click on Action Tab – > New From Browse button select your Process.Bat batch file. Click on OK and again click on OK. 8. If you have previously selected the option of running the process when user is logged in or not, the Task Scheduler asks you for the system password (if any). 9. Enter your password and click on OK. 10. On your desired time schedule, the scheduler will automatically start the CLI Data Loader process. Conclusion: –

After the process is executed the command prompt with show the success and failure errors. You can also check the log files: “insertAccounts_success.csv” and “insertAccounts_error.csv”. Once the process executes correctly, the file “insertAccounts_success.csv” has ID and Status of all the records. Hence this is how we can automate the process of executing Data Loader using Command Line.

Enquire Now

lets get over a cup of coffee and discuss!