Skip to content

Commit

Permalink
Bulk Import: support custom date fields
Browse files Browse the repository at this point in the history
  • Loading branch information
brmeyer committed Aug 1, 2023
1 parent 624a612 commit 3539216
Showing 1 changed file with 43 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import com.sforce.ws.ConnectionException;
import com.stripe.util.CaseInsensitiveMap;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -1886,22 +1888,21 @@ protected void setBulkImportCustomFields(SObject sObject, SObject existingSObjec
if (key.startsWith("Append ")) {
// appending to a multiselect picklist
key = key.replace("Append ", "");
String value = getMultiselectPicklistValue(key, entry.getValue(), existingSObject);
sObject.setField(key, value);
setMultiselectPicklistValue(key, entry.getValue(), sObject, existingSObject);
} else {
sObject.setField(key, getCustomBulkValue(entry.getValue()));
setCustomBulkValue(key, entry.getValue(), sObject);
}
});
}

protected String getMultiselectPicklistValue(String key, String value, SObject existingSObject) {
protected void setMultiselectPicklistValue(String key, String value, SObject sObject, SObject existingSObject) {
if (existingSObject != null) {
String existingValue = (String) existingSObject.getField(key);
if (!Strings.isNullOrEmpty(existingValue) && !existingValue.contains(value)) {
return Strings.isNullOrEmpty(existingValue) ? value : existingValue + ";" + value;
value = Strings.isNullOrEmpty(existingValue) ? value : existingValue + ";" + value;
}
}
return value;
sObject.setField(key, value);
}

protected void processBulkImportCampaignRecords(List<CrmImportEvent> importEvents) throws Exception {
Expand Down Expand Up @@ -1947,22 +1948,47 @@ protected void processBulkImportCampaignRecords(List<CrmImportEvent> importEvent

// TODO: This is going to be a pain in the butt, but we'll try to dynamically support different data types for custom
// fields in bulk imports/updates, without requiring the data type to be provided. This is going to be brittle...
protected Object getCustomBulkValue(String value) {
protected void setCustomBulkValue(String key, String value, SObject sObject) {
Calendar c = null;
try {
// If a client is uploading a sheet for bulk upsert,
// the dates are most likely to be in their own TZ
c = Utils.getCalendarFromDateString(value, env.getConfig().timezoneId);
} catch (Exception e) {}
if (key.contains("dd/mm/yyyy")) {
c = Calendar.getInstance();
c.setTime(new SimpleDateFormat("dd/MM/yyyy").parse(value));
key = key.replace("dd/mm/yyyy", "");
} else if (key.contains("dd-mm-yyyy")) {
c = Calendar.getInstance();
c.setTime(new SimpleDateFormat("dd-MM-yyyy").parse(value));
key = key.replace("dd-mm-yyyy", "");
} else if (key.contains("mm/dd/yyyy")) {
c = Calendar.getInstance();
c.setTime(new SimpleDateFormat("MM/dd/yyyy").parse(value));
key = key.replace("mm/dd/yyyy", "");
} else if (key.contains("mm/dd/yy")) {
c = Calendar.getInstance();
c.setTime(new SimpleDateFormat("MM/dd/yy").parse(value));
key = key.replace("mm/dd/yy", "");
} else if (key.contains("mm-dd-yyyy")) {
c = Calendar.getInstance();
c.setTime(new SimpleDateFormat("MM-dd-yyyy").parse(value));
key = key.replace("mm-dd-yyyy", "");
} else if (key.contains("yyyy-mm-dd")) {
c = Calendar.getInstance();
c.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(value));
key = key.replace("yyyy-mm-dd", "");
}
} catch (ParseException e) {
env.logJobError("failed to parse date", e);
}

// TODO: yes/no -> bool is causing trouble for picklist/text imports that include those values
if (c != null) {
return c;
} else if ("true".equalsIgnoreCase(value) || "yes".equalsIgnoreCase(value)) {
return true;
} else if ("false".equalsIgnoreCase(value) || "no".equalsIgnoreCase(value)) {
return false;
sObject.setField(key, c);
} else if ("true".equalsIgnoreCase(value)/* || "yes".equalsIgnoreCase(value)*/) {
sObject.setField(key, true);
} else if ("false".equalsIgnoreCase(value)/* || "no".equalsIgnoreCase(value)*/) {
sObject.setField(key, false);
} else {
return value;
sObject.setField(key, value);
}
}

Expand Down

0 comments on commit 3539216

Please sign in to comment.