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
Mohsinkhan PathanMohsinkhan Pathan 

How to fetch the HttpPost response and assign it to the body of a attachment.

Hi Team,

I am creating a record  in custom object every time a transaction happens between salesforce and external system. When this record is created, I need to attach the request and  the response to this record.

I am able to save the request as attachment.But unable to save the response.I am trying to save the respone returned by Httppost method in a string before it is returned. It saves the response in the string. I verified it by having a debug statement. But when I use the same string variable to assign to attachment.body (Creating a attachment record) it is null. Creation of attachment is happening in seperate method (public) based on the success or Failure of the transaction. I have also declared the string as global static so that I can use it in other methods.

How can I assign the response to attachment.body so that the response that is sent to external system is saved as attachment to the record.

Note I am using wrapper class to set the response variables. Below is the code of wrapper class.

 global class createNewwrapper

            global String Fields {get;set;}
            global String Message {get;set;}
            global String Statuscode {get;set;}
            
            global createNewwrapper(){ }
}

Please help me on how to do this. Looking forward to hear from you. Thanks in Advance.

Regards,
Khan
Aslam ChaudharyAslam Chaudhary
Below code may help you.
Blob xmlData ;
xmlData=blob.valueOf('<xml><h1>your response string</h1><xml>');

Attachment attach=new Attachment();
attach.Body=xmlData;
attach.Name='filename.txt';
attach.ParentId ='0017F00000J3QgM';
insert attach;


 
Mohsinkhan PathanMohsinkhan Pathan
Hi Aslam, 

Thanks for the reply. But this is not working. I am trying to access the returned values of one method and use them in another. 
Aslam ChaudharyAslam Chaudhary
Can you please paste your code?