In part 1, we looked at where and why the zero's are missing in salesforce's database.
Now, we will turn our attention towards how to put them back by creating a modified version of the data loader.
Please note that this will use regular expressions to change any field ending in .0 to .00 (.00, .000, etc will be left alone). This is not the idea method but it works.
In a perfect world, we would call the DescribeSObject method in the metadata API and look at the DescribeSObjectResult's fieldtype and scale fields to only fix this problem on given field types. I have been hoping that salesforce would update the dataloader to do so but as of version 23 - no such luck. Maybe I will get bored one day and do it myself.
In the mean time, notice how are modifying an existing function. Sales force is doing a similar thing (i.e. not using the meta data API but reformatting a field) in this same method for dates.
Since this is a rather technical post lets go over some prerequisite things you will need:
- A working Eclipse Java environment. If you have the force.com IDE installed, then you should be good to go.
- Install ANT to your machine (this requires the JDK not to be confused with the JRE).
- Install Perl to your machine.
- Download the latest copy of the ApexDataLoader source code and extract it to C:\apexdataloader\
You can generate this file by issuing running ant.bat in C:\apexdataloader\build\
Once completed, it should create C:\apexdataloader\lib\partnerwsdl.jar.
Now you can create a new java project in eclipse, import the filesystem into the project, fix your JDK and partnerwsdl.jar references in the java build window by locating these on the file system..
Once you have the project loaded:
- Open the PartnerQueryVisitor.Java file
- Change import java.util.regex.pattern; to read import java.util.regex.*;
- Locate the convertFieldValuefunction and append this code to the bottom (before the return line)
if (fieldVal != null) { // create a pattern to use regular expressions Pattern ptrnEndsInDotZero = Pattern.compile("\\.[0-9]"); Pattern ptrnEndsInDotTwoNums = Pattern.compile("\\.[0-9][0-9]"); // cast our number to a string String strDecimalAmount = fieldVal.toString(); // create a matcher from our pattern and string Matcher mtchEndsInDotZero = ptrnEndsInDotZero.matcher(strDecimalAmount); Matcher mtchEndsInDotTwoNums = ptrnEndsInDotTwoNums.matcher(strDecimalAmount); boolean blnDotZeroSearchResult = mtchEndsInDotZero.find(); boolean blnDotTwoNumsSearchResult = mtchEndsInDotTwoNums.find(); if (blnDotZeroSearchResult == true && blnDotTwoNumsSearchResult == false) { // Has tenths but not hundredths // found an entry with missing Decimal // add the zero back. fieldVal = fieldVal + "0"; } } // check for null field
Use
C:\apexdataloader\build\ant.bat to build your jar file in C:\apexdataloader\JAR\- If your version of java wasn't compatible with the salesforce exe, you can now call the jar file from the command line (or a bat file) like this:
java.exe -cp ..\MyDataLoader.jar -Dsalesforce.config.dir=..\ConfigDIRGoesHERE com.salesforce.dataloader.process.ProcessRunner
No comments:
Post a Comment