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

Download Attachment Body Content #20

Closed
rgustavs opened this issue May 20, 2019 · 5 comments
Closed

Download Attachment Body Content #20

rgustavs opened this issue May 20, 2019 · 5 comments
Assignees
Labels
question or help Clarification or help may suffice to resolve

Comments

@rgustavs
Copy link

Hi
Thank you for a great package. Very useful to me.

I would like to download attachment and I am struggling with the final step.

Here is what I do

sf_auth() b <- sf_query("SELECT Body, Name, Id FROM Attachment WHERE ParentId = 'XX'")

and I get a list of references back
>glimpse(b)

Body <chr> /services/data/v42.0/sobjects/Attachment/00P5700001SCWgXXXX/Body"
Name <chr> "Doc.pdf"
Id <chr> "00P570000X"

How do I download Doc.pdf which is hiding inside /services/data/v42.0/sobjects/Attachment/00P5700001SCWgXXXX/Body?

@StevenMMortimer StevenMMortimer self-assigned this May 20, 2019
@StevenMMortimer StevenMMortimer added the question or help Clarification or help may suffice to resolve label May 20, 2019
@StevenMMortimer StevenMMortimer changed the title Dowload Attachement Body content Download Attachment Body Content May 20, 2019
@StevenMMortimer
Copy link
Owner

@rgustavs The Salesforce documentation is here: https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/dome_sobject_blob_retrieve.htm

Below is a function I've written called sf_download_attachment() that will perform the action of getting the attachment as a binary body and then writing it to disk. I'll add this function to the next CRAN release, for now, just load it in your script and use it as demonstrated below the function.

sf_download_attachment <- function(body, name, path = "."){
  resp <- rGET(sprintf("%s%s", salesforcer_state()$instance_url, body))
  f <- file.path(path, name)
  writeBin(content(resp, "raw"), f)
  return(file.exists(f))
}

sf_download_attachment(body = "/services/data/v45.0/sobjects/Attachment/00P6A00000PnNK3UAN/Body", 
                       name = "test.pdf",
                       path = ".")

@StevenMMortimer
Copy link
Owner

@rgustavs If you'd like to download all of the attachments at once, then use mapply(). Like this:

queried_attachments <- sf_query("SELECT Body, Name
                                 FROM Attachment
                                 WHERE ParentId = '0016A0000035mJ5'")
mapply(sf_download_attachment, queried_attachments$Body, queried_attachments$Name)

@rgustavs
Copy link
Author

wow! thanks for quick response. I will try this first thing in the morning and revert back to you.

@rgustavs
Copy link
Author

Works like a charm. Many thanks. Also appreciate the login_url fix.

Early adopters, needed the following libraries

library(tidyverse)
library(salesforcer) -> github version, not cran
library(httr)

@StevenMMortimer
Copy link
Owner

@rgustavs The function I prototyped above has now been included in the latest CRAN version of the package (0.1.3)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question or help Clarification or help may suffice to resolve
Projects
None yet
Development

No branches or pull requests

2 participants