Skip to main content

I am trying to fetch few pdf and xls files from SFDC org1 and store it in SFDC org2. I could get the contentversion versondata body using the service '/services/data/v42.0/sobjects/ContentVersion/06890000003oTkqAAE/VersionData'. But I am not sure what is the format of the response and how to store it in SFDC org2. I tried the below code, it creates pdf and xls files but the files are corrupted.

Http h = new Http();

HttpRequest req = new HttpRequest();

req.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm() + '/services/data/v42.0/sobjects/ContentVersion/06890000003oTkqAAE/VersionData');

req.setMethod('GET');

req.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId());

req.setHeader('Content-Type', 'application/json');

HttpResponse res = h.send(req);

contentVersion cv = new contentVersion();

cv.FirstPublishLocationId = '5002aikl0000aes';

cv.Title = 'Solution pdf';

cv.PathOnClient = 'Solution.pdf';

cv.VersionData = Blob.valueof( res.getbody() );

insert cv;

 
2 件の回答
  1. 2020年5月7日 12:27

    Superb Abhishek, the forums gave me the pointer to find the solution. Below code works and migrates attachments in right format.

    HttpRequest req = new HttpRequest();

    req.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm() + '/services/data/v42.0/sobjects/ContentVersion/06890000003oTkqAAE/VersionData');

    req.setMethod('GET');

    req.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId());

    req.setHeader('Content-Type', 'application/json');

    HttpResponse res = h.send(req);

    contentVersion cv = new contentVersion();

    cv.FirstPublishLocationId = '5002aikl0000aes';

    cv.Title = 'Solution pdf';

    cv.PathOnClient = 'Solution.pdf';

    cv.VersionData = res.getBodyAsBlob(); // Directly convert response body into blob content.

    insert cv;

0/9000