0

I want to send PDF file which is generated by VF page from Salesforce to XERO.

But I'm trying to send but it's not happening

XERO syntax, it receives like below format

POST /api.xro/2.0/Receipts/e59a2c7f-4078-a0f3-73537afcbba9/Attachments/Image00394.ddf

Here I'm not understanding how to pass PDF content in the URL above image00394 name.

Here is what I'm doing

    string pdfcon='asdasdasdasd';
Blob pdfBody = EncodingUtil.base64Decode(pdfcon);


String endpoinurl='callout:Xero_Integration/api.xro/2.0/Invoices/32b8b9-4b5-45da-84ba-80e1bb47881/Attachments/';

HttpRequest req= new HttpRequest();
// set endpoint destination
req.SetEndpoint(endpoinurl);
req.setMethod('POST');
req.setHeader('xero-tenant-id','7bc01e1-d87-4a8b-9b96-2ffbc88f1cb8');
req.setHeader('Content-Type','application/pdf');
req.setBodyAsBlob(pdfBody);


Http http = new Http();
// send the request
HttpResponse response = http.send(req);

System.debug(response.getBody());

If I'm doing like below with filename mentioning in the URL, the PDF file is inserting but when I open it in XERO it showing below error:

enter image description here

  String endpoinurl='callout:Xero_Integration/api.xro/2.0/Invoices/322cb8b9-40b5-45da-84ba-80e1bb437881/Attachments/'+pdfBody+'.pdf';

Finally I'm not understanding how to pass PDF content in the ENDPOINT URL of Http callouts?

2
  • There's some issue in blobs with salesforce. I'd suggest to use content version(files) and instead of posting from salesforce to Xero, you can download at Xero directly(or pull through code/ CURL commands).
    – Ysr Shk
    Commented Oct 14, 2020 at 15:25
  • Thanks @YsrShk it's working
    – user89211
    Commented Oct 15, 2020 at 4:33

1 Answer 1

0

Here is the coding what i used to solve that, I used contentversion object in salesforce.

    Attachment attach = [SELECT Id, Name, Body, ContentType, ParentId From Attachment LIMIT 1];

ContentVersion cv = new ContentVersion();
cv.Title = 'Test Document';
cv.PathOnClient = 'TestDocument.pdf';
cv.VersionData = attach.body;
//cv.IsMajorVersion = true;
  
String endpoinurl='callout:Xero_Integration/api.xro/2.0/Invoices/340b5-45-80e1bb481/Attachments/'+cv.PathOnClient

HttpRequest req= new HttpRequest();
req.SetEndpoint(endpoinurl);
req.setMethod('POST');
req.setHeader('xero-tenant-id','7bc2-d087-2ffbc88f1cb8');
req.setHeader('Content-Type','application/pdf');
req.setBodyAsBlob(attach.body);

Http http = new Http();
HttpResponse response = http.send(req);
System.debug(response.getBody());

        

  

If it's useful to you vote it!...

You must log in to answer this question.

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