0

I am trying to build an utility to bulk publish documents through apex HTTPCallout. I dont want to read individual files from some visualforce page as this not feasible for bulk publishes. Rather want to be able to just provide URL and the files get sent through HTTPCallout. I tried this with Postman for one single document and it did work. Just trying to put the same but for mulitple documents in an apex code. Kindly suggest.

public void publish(){
    if(UserInfo.getSessionId() != null) {
        Http h = new Http();
        HttpRequest req = new HttpRequest();
        req.setEndpoint('https://ignore.domainname.com/api/v17.2/objects/documents');
        req.setMethod('POST');
        req.setHeader('Content-Type', 'multipart/form-data');
        req.setTimeout((Integer) settings.My_Timeout_MVN__c);
        req.setHeader('Authorization', '5F055E6F00EA0JHKH98Y8989');
        String body = 'file= C:\\ignore\\ignore\\Downloads\\my sample document.docx'
            +'& type__v = type1'
            +'& name__v = Document from Utility'
            +'& summary__c = Summary of Doc';
        req.setBody(body);
        req.setHeader('Accept', 'application/json');
        try {
            HttpResponse res = h.send(req);
            System.debug(res.getBody());
        }catch(Exception e) {
            System.debug(e);
            ApexPages.addMessages(e);
        }
    }               
}
2
  • 1
    You should remove your authorization code from the question, and invalidate it.
    – David Reed
    Commented Jan 2, 2018 at 20:01
  • Thank You. I did take care of that. It's just a combination of some random letters and numbers. :) Commented Jan 2, 2018 at 20:05

1 Answer 1

0

Enrico Murru has provided a brilliant solution for this Salesforce problem. Please find the path below.

http://blog.enree.co/2013/01/salesforce-apex-post-mutipartform-data.html

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .