0

I'm making integration to an external system and send them a request, and I get a JSON as a response via REST API.

The response has a lot of data to upsert to SF.

While upserting the Data inside the response - I want to validate the data.

After doing so, I want to send all the data that didn't pass the validation.

My question is - Do I need to have another request? Or can I use the response to send another data? (Like make the connection open)

Http h = new Http();
HttpRequest httpReq = new HttpRequest();
httpReq.setMethod('POST');
httpReq.setHeader('Content-Type','application/json'); 
httpReq.setEndpoint('Some URL');
httpReq.setBody('body');
HttpResponse res = h.send(httpReq); //! GET the JSON HERE


 RootObject data = (RootObject) JSON.deserialize(res.getBody(), RootObject.class);

 Map<String,String> errorsMap = ValidateData(data);

 //Send the errorsMap (Keyset and Values to the external System again).
 //Do I need to create another request to it? - Means use the Http send method twice?
1

@Salvation yes you need to perform another HTTP callout as you did previously. The thing you want to be mindful is the order of operations for HTTP callouts and DML transactions:

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_callouts_timeouts.htm?search_text=callout

You can’t make a callout when there are pending operations in the same transaction. Things that result in pending operations are DML statements, asynchronous Apex (such as future methods and batch Apex jobs), scheduled Apex, or sending email. You can make callouts before performing these types of operations.

|improve this answer|||||
  • Actually I thought to make the request at the Finish method of the batch - With values of a Map that includes all the Errors - and the finish is working in a separate transaction so it will be Ok I guess. Thank you very much for your help. – Salvation Oct 23 '19 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.