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
Krisztian Valko 5Krisztian Valko 5 

Capture HTTP response

Hi Guys,

I am triggering a POST request via process builder and it works but can you please tell me how can I capture the HTTP response I get back? The external system will send me a string I would like to store on the record which started the process. 
 

@future(callout=true)

        public static void calloutmethod(String name){
                Http http = new Http();
                HttpRequest request = new HttpRequest();
                request.setEndpoint('XXX');
                request.setMethod('POST');
                request.setHeader('Content-Type', 'application/json;charset=UTF-8');
                request.setHeader('accept', 'application/json');
                request.setBody(name);
                
                response = http.send(request);
        }

Thanks a lot.

Krisz
Best Answer chosen by Krisztian Valko 5
Abdul KhatriAbdul Khatri
See if you can set the property
 
public string returnVal  { get; set; }

@future(callout=true)
public static void calloutmethod(String name){
    Http http = new Http();
    HttpRequest request = new HttpRequest();
    request.setEndpoint('XXX');
    request.setMethod('POST');
    request.setHeader('Content-Type', 'application/json;charset=UTF-8');
    request.setHeader('accept', 'application/json');
    request.setBody(name);
    
    response = http.send(request);
    
    if(response.getStatusCode() == 200) 
        returnVal = String.valueOf(response.getBody());   
}

 

All Answers

Abdul KhatriAbdul Khatri
Here is the code
 
@future(callout=true)
public static String calloutmethod(String name){
    Http http = new Http();
    HttpRequest request = new HttpRequest();
    request.setEndpoint('XXX');
    request.setMethod('POST');
    request.setHeader('Content-Type', 'application/json;charset=UTF-8');
    request.setHeader('accept', 'application/json');
    request.setBody(name);
    
    response = http.send(request);
    
    if(response.getStatusCode() == 200) 
        return String.valueOf(response.getBody());
    
    return null;
    
}

 
Krisztian Valko 5Krisztian Valko 5
Hi Abdul,

Thanks a lot. My method is void and I cannot change it because it says that a constructor cannot be non-void. But since the method is void I cannot return anything.

What do you think?

Thanks a lot.

Regards,
Krisz
Abdul KhatriAbdul Khatri
See if you can set the property
 
public string returnVal  { get; set; }

@future(callout=true)
public static void calloutmethod(String name){
    Http http = new Http();
    HttpRequest request = new HttpRequest();
    request.setEndpoint('XXX');
    request.setMethod('POST');
    request.setHeader('Content-Type', 'application/json;charset=UTF-8');
    request.setHeader('accept', 'application/json');
    request.setBody(name);
    
    response = http.send(request);
    
    if(response.getStatusCode() == 200) 
        returnVal = String.valueOf(response.getBody());   
}

 
This was selected as the best answer
Abdul KhatriAbdul Khatri
By the way I don't understand you above comment regarding the constructor part, would you kind to share the entire class to see why you are getting this.
Krisztian Valko 5Krisztian Valko 5
public class OpportunityTransfer {
    
	public class AccountRecord {   
        @InvocableVariable(label='Account ID')
        public String accId;
        @InvocableVariable(label='Account Name')
        public String accName;
        @InvocableVariable(label='Primary Contact First Name')
        public String primaryContactFirstName;
        @InvocableVariable(label='Primary Contact Last Name')
        public String primaryContactLastName;
        @InvocableVariable(label='Primary Contact Email Address')
        public String primaryContactEmail;
        @InvocableVariable(label='Primary Contact Phone Number')
        public String primaryContactPhone;
        @InvocableVariable(label='Account Billing Country')
        public String accCountry;
	}
    
    @TestVisible static HttpResponse response;
    
    @InvocableMethod(label='Post To')
    public static void PostTo(List<AccountRecord> accs) {
        
      AccountRecord a = accs[0];
        
        JSONGenerator gen = JSON.createGenerator(true);    
            gen.writeStartObject();      
            gen.writeStringField('companyName', a.accName);
            gen.writeStringField('firstName', a.primaryContactFirstName);
            gen.writeStringField('lastName', a.primaryContactLastName);
        	gen.writeStringField('email', a.primaryContactEmail);
        	gen.writeStringField('phone', a.primaryContactPhone);
        	gen.writeStringField('country', a.accCountry);
            gen.writeEndObject();    
            String jsonS = gen.getAsString();
        System.debug('jsonMaterials'+jsonS);
        calloutmethod(jsonS);
       
}
    
    @future(callout=true)

        public static void calloutmethod(String name){
                Http http = new Http();
                HttpRequest request = new HttpRequest();
                request.setEndpoint('XXXX');
                request.setMethod('POST');
                request.setHeader('Content-Type', 'application/json;charset=UTF-8');
                request.setHeader('accept', 'application/json');
                request.setBody(name);
                
                response = http.send(request);
        }
    
}

 
Krisztian Valko 5Krisztian Valko 5
Hi Abdul,

Thanks for taking the time helping me. I attached the code above.

Regards,
K
Abdul KhatriAbdul Khatri
Sorry some other things are dragging me alot. Did you try my last code by using property.?
Abdul KhatriAbdul Khatri
Can you give a try my last solution of assigning to a property? You can also use any public variable?

Can you also explain what exactly you are trying to accomplish by the returning value that you are looking for?
Krisztian Valko 5Krisztian Valko 5
Hi Abdul, Thanks, the solution works perfectly. Thank you for your time.

Regards,
Krisz