You need to sign in to do that
Don't have an account?
Célio Xavier
Do not save record when callout returns code 500
I carried out a development that when the email field is changed in Salesforce, a Process Builder is triggered that according to some criteria calls a class that makes a callout to send data to an external service, it is working, but in order to implement it in production I need to deal code 500 that I receive if in case the external service is out, is there any way to prevent the saving of the email field according to the response of the external service request?
public class ChangeEmailAPI { public static HttpResponse CallChangeEmailAPI(String Email, String Inscription_Number) { Integer Inscription_Number_Int = integer.valueOf(Inscription_Number); if(Email == null){Email = '';} System.debug('E-mail: ' + Email + ' / ' + 'Matrícula: ' + Inscription_Number_Int); Http http = new Http(); HttpRequest request = new HttpRequest(); request.setEndpoint('https://test.company.com.br/crm/atendimentos'); request.setMethod('PUT'); request.setHeader('Content-Type', 'application/json'); request.setHeader('x-api-key', 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'); request.setHeader('canalOperacao', '14'); JSONGenerator gen = JSON.createGenerator(true); gen.writeStartObject(); gen.writeNumberField('idCanalSolicitacao', 14); // Channel 14 - Salesforce gen.writeNumberField('idUsuario', 65200); //User Test Salesforce gen.writeNumberField('numeroMatricula', Inscription_Number_Int); gen.writeStringField('email', Email); gen.writeNumberField('idUsoEmail', 3); //3 - E-mail for sending communications and services *Mock Brasilprev // 4 - E-mail for Billing gen.writeEndObject(); String Jsons = gen.getAsString(); System.debug(Jsons); request.setBody(Jsons); try{ HttpResponse response = http.send(request); request.setTimeout(20000); System.debug(response.getStatusCode()); System.debug(response); System.debug('Alteração de e-mail processada no legado com sucesso.'); return response; }catch(System.CalloutException e){ System.debug('Erro - Serviço de Ateração de e-mail: ' + e); } return null; } }
You could do something like this and it will ensure your logic is not executed if response code is 500