From 58a2470e138f1be580d1854cd5ad654770f24c75 Mon Sep 17 00:00:00 2001 From: ashitsalesforce Date: Tue, 14 Jan 2025 18:49:07 -0800 Subject: [PATCH] trim leading and trailing whitespace chars in idlookup fields during uploads trim leading and trailing whitespace chars in idlookup fields during uploads if sfdc.load.removeNonBreakingSpaceInIdLookupField is set to true (default is true). This is to work around server side not handling leading and trailing full-width space chars in the same way as half-width space chars. --- .../action/visitor/DAOLoadVisitor.java | 32 ++++++++++++++++++- .../dataloader/config/AppConfig.java | 3 +- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/salesforce/dataloader/action/visitor/DAOLoadVisitor.java b/src/main/java/com/salesforce/dataloader/action/visitor/DAOLoadVisitor.java index d596a56e..c659a32b 100644 --- a/src/main/java/com/salesforce/dataloader/action/visitor/DAOLoadVisitor.java +++ b/src/main/java/com/salesforce/dataloader/action/visitor/DAOLoadVisitor.java @@ -211,7 +211,8 @@ public boolean visit(TableRow row) throws OperationException, DataAccessObjectEx DynaBean dynaBean = SforceDynaBean.convertToDynaBean(dynaClass, sforceDataRow); Map fieldMap = BeanUtils.describe(dynaBean); for (String fName : fieldMap.keySet()) { - if (fieldMap.get(fName) != null) { + Object fieldVal = fieldMap.get(fName); + if (fieldVal != null) { // see if any entity foreign key references are embedded here Object value = this.getFieldValue(fName, dynaBean.get(fName)); dynaBean.set(fName, value); @@ -372,6 +373,13 @@ private synchronized void getHtmlFormattedAndPhoneSforceFieldList() { } public Object getFieldValue(String fieldName, Object fieldValue) { + // TODO: this needs to be controlled by a config property. + if (getController().getAppConfig().getBoolean(AppConfig.PROP_LOAD_REMOVE_NONBREAKING_SPACE_IN_IDLOOKUP_FIELD) + && isIdLookupField(fieldName)) { + String fieldValueStr = (String)fieldValue; + // idLookupFields do not have leading or trailing whitespace chars + return fieldValueStr.strip(); // remove leading and trailing whitespace + } fieldValue = getHtmlFormattedFieldValue(fieldName, fieldValue); fieldValue = getPhoneFieldValue(fieldName, fieldValue); return fieldValue; @@ -499,4 +507,26 @@ protected void setLastRunProperties(Object[] results) throws LoadException { handleException(errMsg, e); } } + + private HashMap fieldTypesMap = new HashMap(); + private boolean isIdLookupField(String fieldName) { + if (fieldTypesMap.containsKey(fieldName)) { + return fieldTypesMap.get(fieldName); + } + PartnerClient partnerClient = this.getController().getPartnerClient(); + DescribeSObjectResult describeSObjectResult; + try { + describeSObjectResult = partnerClient.describeSObject(this.getConfig().getString(AppConfig.PROP_ENTITY)); + Field[] fields = describeSObjectResult.getFields(); + for (Field field : fields) { + if (field.getName().equalsIgnoreCase(fieldName)) { + fieldTypesMap.put(fieldName, field.isIdLookup()); + return field.isIdLookup(); + } + } + } catch (ConnectionException e) { + logger.warn(fieldName + " field type not found in describeSObjectResult"); + } + return false; + } } diff --git a/src/main/java/com/salesforce/dataloader/config/AppConfig.java b/src/main/java/com/salesforce/dataloader/config/AppConfig.java index 6d4f2b8f..10d5d243 100644 --- a/src/main/java/com/salesforce/dataloader/config/AppConfig.java +++ b/src/main/java/com/salesforce/dataloader/config/AppConfig.java @@ -339,6 +339,7 @@ public class AppConfig { public static final String PROP_EXTRACT_ALL_CAPS_HEADERS="sfdc.extraction.allCapsHeaders"; public static final String PROP_EXTRACT_CSV_OUTPUT_BOM="sfdc.extraction.outputByteOrderMark"; public static final String PROP_LOAD_PRESERVE_WHITESPACE_IN_RICH_TEXT = "sfdc.load.preserveWhitespaceInRichText"; + public static final String PROP_LOAD_REMOVE_NONBREAKING_SPACE_IN_IDLOOKUP_FIELD="sfdc.load.removeNonBreakingSpaceInIdLookupField"; // // process configuration (action parameters) @@ -787,7 +788,7 @@ private void setDefaults(Map cliOptionsMap) { setDefaultValue(PROP_SAVE_ALL_PROPS, false); setDefaultValue(PROP_EXTRACT_ALL_CAPS_HEADERS, false); setDefaultValue(PROP_EXTRACT_CSV_OUTPUT_BOM, true); - + setDefaultValue(PROP_LOAD_REMOVE_NONBREAKING_SPACE_IN_IDLOOKUP_FIELD, true); } /**