+ Start a Discussion

Get and Add attachments to a custom object

Hi All,


I'm trying to retrieve jpeg attachments from a Custom object and I can query for them ("SELECT Id, Name, Body from Attachment WHERE ParentId='"+Id+"'") but I'm getting lost trying to add them to my hybrid application.


I end up with this response:


    "totalSize": 1,
    "done": true,
    "records": [
            "attributes": {
                "type": "Attachment",
                "url": "/services/data/v23.0/sobjects/Attachment/00PD000000Bvp9dMAB"
            "Id": "00PD000000Bvp9dMAB",
            "Name": "building-foundation-2.jpg",
            "Body": "/services/data/v23.0/sobjects/Attachment/00PD000000Bvp9dMAB/Body"


But I don't know what to do with it, any ideas?


I'm also trying to add further images from the Android Camera but I'm having no joy trying to figure out how to add attachments - any help appreciated.






Okay, some progress this morning:


I discovered that my desktop browser manages to retrieve images (there are 2 in the test object) if I add "https://c.eu1.content.force.com/servlet/servlet.FileDownload?file=" and then my Id.


So I loop through my response thus:


$j.each(response.records, function(){
    $j("<img/>").css({"width":"100%"}).attr({"src":baseUri + this.Id,"alt":baseUri + this.Id}).appendTo(imageHolder);


But I'm not getting anything back. I've tried to ajax the URI and alerting the response and it seems like I'm getting a HTML page requesting me to log in but my app has already signed in using OAuth...


Any ideas?



Gaurav KheterpalGaurav Kheterpal

You might want to look at this post for reference. It's a hybrid iOS app which uses the device camera to upload images to Contacts. The source code for this sample is publicly available in Github.


I hope this helps.




Hi Gaurav,


thanks for your reply but that doesn't seem to help - I'd seen that before and it seems to be aimed at storing an image as base64 encoded text within a field on an object.


A cool idea really but not really what I'm after - I need to be able store and retrieve it as an attachment.


I'm getting an HTML page as a response from the FileDownload servlet rather than the image itself - I'd have guessed that the OAuth token would've let me download the image without an extra layer of security being in place but I seem to be wrong.


I'll keep on trying and update here if/when I crack it ;-)


Thanks again though.