function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
subhajit13subhajit13 

Sending Attachment from Salesforce to external system

Hi,

Can send can send attachment(s) from Salesforce to and external system using WSDL or HTTP Callout? If yes, is there any limit of size of file can be sent?

From what I found out so far, it seems.. attachment (blob) can not be sent using WSDL call..

The following data types are only supported when used as call ins, that is, when an external Web service calls an Apex Web service method. These data types are not supported as callouts, that is, when an Apex Web service method calls an external Web service.
•             blob
•             decimal
•             enum

Can we use HTTPRequest method setBodyAsBlob (to an EndPoint URL) and set MIME type and send the file instead in case WSDL based callout does not support it?

Please suggest, would really appriciate any help/input, thanks in advance.

Regards,
Subhajit
Best Answer chosen by subhajit13
AK VineethAK Vineeth
Hi subhajit13 ,

Yes , you can send attachments by using HTTP/HTTPS Protocol ,There are Lots of Salesforce Encoding Util class (https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_restful_encodingUtil.htm.) methods

Following are the aspects you need to understand and focus while using HTTP Protocol from Client side is .
1.Construct the http class .
2.Convert  the attachment into base 64 and MIME versions using salesforce utils class methods.
3.Create the connection and send the file .
4 .Check the response which is optional .

You need to clearly understand the server architeure before sending the attachments and creating a connection using a HTTP/HTTPS protocol .

And one more best suggestion while using SOAP/WSDL ,It would be better if you can use JAVA as medium for sending attachments to the external web service.

Thanks 


All Answers

Hargobind_SinghHargobind_Singh
Hi Subhajit13, 

I've done this in the past and uploaded images to external web-services, by sending the Blob as POST data to an HTTP web-service, using HttpRequest and SetBody (for additional parameters as well, like image name etc) . You can look at governor limits for information of  max size of post data from Apex though.

- Hargobind

AK VineethAK Vineeth
Hi subhajit13 ,

Yes , you can send attachments by using HTTP/HTTPS Protocol ,There are Lots of Salesforce Encoding Util class (https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_restful_encodingUtil.htm.) methods

Following are the aspects you need to understand and focus while using HTTP Protocol from Client side is .
1.Construct the http class .
2.Convert  the attachment into base 64 and MIME versions using salesforce utils class methods.
3.Create the connection and send the file .
4 .Check the response which is optional .

You need to clearly understand the server architeure before sending the attachments and creating a connection using a HTTP/HTTPS protocol .

And one more best suggestion while using SOAP/WSDL ,It would be better if you can use JAVA as medium for sending attachments to the external web service.

Thanks 


This was selected as the best answer
subhajit13subhajit13
Thanks Hargobind and AK for the useful information and help..
Uday rajUday raj
Hi Guys,

i am unable to get response from Jira. Any suggestions highly appreciated.

global class CaseAttachment {
       @future(callout=true)

    public static void uploadFile(Blob file_body, String file_name,string Jiraid){
      
    
          String boundary = ' ';
          String header = '--'+boundary+'\r\nContent-Disposition: form-data; name="file"; filename="'+file_name+'"\r\nContent-Type: application/octet-stream';
          String footer = '\r\n--'+boundary+'--';              
          String headerEncoded = EncodingUtil.base64Encode(Blob.valueOf(header+'\r\n\r\n'));
           
          // String file_name= '';
          String endpoint = 'https:// jiraurl/rest/api/2/issue/{JiraId}/attachments-addAttachment';
          while(headerEncoded.endsWith('='))
          {
           header+=' ';
           headerEncoded = EncodingUtil.base64Encode(Blob.valueOf(header+'\r\n\r\n'));
          }
          String bodyEncoded = EncodingUtil.base64Encode(file_body);
          String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));

          Blob bodyBlob = null;
         String last4Bytes = bodyEncoded.substring(bodyEncoded.length()-4,bodyEncoded.length());
          if(last4Bytes.endsWith('='))
          {
               Blob decoded4Bytes = EncodingUtil.base64Decode(last4Bytes);
               HttpRequest tmp = new HttpRequest();
               tmp.setBodyAsBlob(decoded4Bytes);
               String last4BytesFooter = tmp.getBody()+footer;   
               bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded.substring(0,bodyEncoded.length()-4)+EncodingUtil.base64Encode(Blob.valueOf(last4BytesFooter)));
          }
          else
          {
                bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+footerEncoded);
          }

          HttpRequest req = new HttpRequest();
          req.setHeader('Content-Type','multipart/form-data; boundary='+boundary);
          req.setMethod('POST');
          req.setEndpoint(endpoint);
          req.setBodyAsBlob(bodyBlob);
             req.setCompressed(true);
          req.setTimeout(120000);
        
          Http http1 = new Http();
          
    HTTPResponse resp = http1.send(req);
    }
}
Murali Krishna 56Murali Krishna 56
@Uday raj

did you able to fix above code? I stuck with same code