Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bulk 2.0 Upsert Error with safe_bind_rows within combine_record_types with datetimes. #109

Open
JoshRosenstein opened this issue Sep 12, 2021 · 1 comment
Assignees
Labels
bug Unintended behavior that should be corrected
Milestone

Comments

@JoshRosenstein
Copy link

Issue description

First of all, I'd like to say thank you for addressing #95 with the latest release. Since then I have been running a bulk upsert process for a while now, and just now ran into a unique scenario that may be a potential bug.
Note I have been able to find a work around by passing combine_record_types =FALSE, and parsing the results myself, but I wanted to share this rare scenario. When receiving a single failed result with a NA column that's suppose to be a datetime, the type is a logical NA and the current method of converting datatypes does not work for rbindlist. However substituting the internal safe_bind_rows with dplyr::bind_rows does not fail.

reprex

if(combine_record_types){

records <- list(
  successfulResults = tibble::tibble(Start_Time__c = anytime::anytime("2021-09-12T04:14:30Z", tz = "UTC", asUTC = TRUE), B = 1),
  failedResults = tibble::tibble(Start_Time__c = as.logical(NA), B = 1),
  unprocessedRecords = tibble::tibble(Start_Time__c = as.character(), B = as.character())
)
# records <-status
base_class_df <- records[[head(which.max(sapply(records, nrow)), 1)]]
# Second, convert the datatypes for any data.frames with zero rows
# From StackOverflow post here: https://stackoverflow.com/a/47800157/5258043
# distributed under the CC BY-SA 3.0 license terms.
for (i in 1:length(records)) {
  if (nrow(records[[i]]) == 0) {
    common <- names(records[[i]])[names(records[[i]]) %in% names(base_class_df)]
    records[[i]][common] <- lapply(common, function(x) {
      match.fun(paste0("as.", class(base_class_df[[x]])[1]))(records[[i]][[x]])
    })
  }
}
# Third, now bind them together
res <- salesforcer::safe_bind_rows(records)
#> Error in rbindlist(l = l, fill = fill, idcol = idcol, ...): Class attribute on column 1 of item 2 does not match with column 1 of item 1.
# res <- dplyr::bind_rows(records)

Session Info

Session info ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 setting  value                       
 version  R version 4.0.3 (2020-10-10)
 os       Ubuntu 18.04.5 LTS          
 system   x86_64, linux-gnu           
 ui       RStudio                     
 language (EN)                        
 collate  C.UTF-8                     
 ctype    C.UTF-8                     
 tz       America/New_York            
 date     2021-09-12     

 salesforcer  * 1.0.0                  2021-07-06 [1] RSPM (R 4.0.5)
 data.table     1.14.0                 2021-02-21 [1] RSPM (R 4.0.3)
 dplyr        * 1.0.7                  2021-06-18 [1] RSPM (R 4.0.5)
@StevenMMortimer
Copy link
Owner

@JoshRosenstein Thanks for reporting! 🙏 I'll take a look. It's possible dplyr has improved its functionality latest. I remember wrestling quite a bit with NAs that turn into logicals and how to convert to the expected data type. It's odd to me that Salesforce doesn't provide metadata about the data types of the columns that are being returned by a query or CRUD operation like this.

@StevenMMortimer StevenMMortimer added the bug Unintended behavior that should be corrected label Sep 17, 2021
@StevenMMortimer StevenMMortimer added this to the v1.0.2 milestone Mar 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Unintended behavior that should be corrected
Projects
None yet
Development

No branches or pull requests

2 participants