1

I need to send my image to salesforce through my iphone app.I have tried those things converted the image--> bytes-->base64 encoding then store the sfdc (Rich data field), it's done perfectly, but i need save as an image .Here are My codes given below (it's not working) Guides me how to achieve that

   NSData *imageData = UIImagePNGRepresentation(imageView.image);
    NSString *boundary = @"---------------------------14737809831466499882746641449";

   SFRestRequest *request = [[SFRestRequest alloc] init];
    [request setDelegate:self];
    [request setEndpoint:kSFDefaultRestEndpoint];
    [request setMethod:SFRestMethodPOST];
    NSMutableData *body = [NSMutableData data];
    [body appendData:[[NSString stringWithFormat:@"rn--%@rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content- Disposition: form-data; name=\"entity_document\"; filename=\"%@\"\r\n",@"Test.png"]] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Type:  multipart/form-data\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Description :\"%s\"\r\n","Marketing brochure for Q1 2011"]] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Keywords :\"%s\"\r\n","marketing,sales,update"]] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"FolderId :\"%s\"\r\n","005D0000001GiU7"]] dataUsingEncoding:NSUTF8StringEncoding]];
     [body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Name :\"%s\"\r\n","Marketing Brochure Q1"]] dataUsingEncoding:NSUTF8StringEncoding]];


    [body appendData:[NSData dataWithData:imageData]];
    [body appendData:[[NSString stringWithFormat:@"rn--%@--rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
     NSString *attbody = [NSString stringWithFormat:@"{\"body\" : {\"Name\" :[{ \"type\" : \"image/jpeg\", \"image\" : \"%@\"}] } }",body];

    [request setPath:[NSString stringWithFormat:@"/v23.0/sobjects/Document/"]];
    [request setQueryParams:(NSDictionary *)[SFJsonUtils objectFromJSONString:attbody]];
    [[SFRestAPI sharedInstance] send:request delegate:self];

Is there any class(Apex) needed to process my request?

4
  • What do you mean about saving as an image? In a salesforce document or attachment or something?
    – Matt Lacey
    Jul 25, 2013 at 23:40
  • whatever, i need to send an image from my iphone to sfdc?
    – Rams
    Jul 26, 2013 at 4:28
  • Well what's not working? What error are you seeing?
    – Matt Lacey
    Jul 26, 2013 at 4:41
  • My doubt is Do i need to create an apex class for accessing my client request like blogs.developerforce.com/developer-relations/2011/09/…
    – Rams
    Jul 26, 2013 at 7:09

4 Answers 4

1

NSData *myData = [[NSData alloc] init];//init with your file content

NSString *b64 = [myData base64EncodedString];

NSDictionary *fields = @{
                         @"Name": @"Document Name.ext",
                         @"Body": b64,
                         @"ParentId": recordId
                         };
attachmentRequest = [[SFRestAPI sharedInstance] requestForCreateWithObjectType:@"Attachment" fields:fields];

[[SFRestAPI sharedInstance] sendRESTRequest:attachmentRequest failBlock:^(NSError *e) {
    NSLog(@"Error");
} completeBlock:^(id dict){
    NSLog(@"Uploaded");
}];
0
0

If you just want to upload binary data to an attachment and/or document then you should not need to write an Apex class at all, you should be able to do everything via the REST API.

Apex classes as shown in the blog post linked are just for creating custom webservices. You can go this route if you choose but it's definitely not required for what you're trying to achieve.

2
  • without apex class i can't able to save my image to sfdc but now i can be able to save my image using apex class
    – Rams
    Jul 28, 2013 at 16:56
  • You can save it as an image if you use a Document or Attachment. Make sure you specify the correct file type if you use a Document or it won't work as expected.
    – Matt Lacey
    Jul 28, 2013 at 23:59
0

I have created apex rest classes to recieve the binary data and then save to SFDC (image saved in Documents).Here are the tutorial i referred .

http://blogs.developerforce.com/developer-relations/2011/09/using-binary-data-with-rest.html

Also here are the tutorial How to access the apex webservice through my ios.

http://techblog.appirio.com/2013/07/rest-with-apex-service-in-native-ios.html

0

To make it so the View function works for images in Attachments in the Salesforce Admin GUI you want the ContentType field as well:

UIImage *testImg = [UIImage imageNamed:@"someImageName"];
NSData *testImgData = UIImagePNGRepresentation(testImg);
NSString *b64 = [testImgData base64Encode]; // base64Encode declared in NSData+SFAdditions.h

NSDictionary *fields = @{
                         @"Name": @"someImage.png",
                         @"Body": b64,
                         @"ParentId": @"00Q50000016TuGIEA0", // of course put your Attachment Id here instead of mine
                         @"ContentType": @"image/png"
                         };
SFRestRequest *attachmentRequest = [[SFRestAPI sharedInstance] requestForCreateWithObjectType:@"Attachment" fields:fields];
[[SFRestAPI sharedInstance] send:attachmentRequest delegate:self];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.