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
Célio XavierCé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; 
    }
}

 
AnudeepAnudeep (Salesforce Developers) 
I suggest using response.StatusCode

You could do something like this and it will ensure your logic is not executed if response code is 500
 
if(response.statusCode!=500) {
// Execute your logic here
}




 
Célio XavierCélio Xavier
I did the test but unfortunately I still couldn't stop saving the email field for this process, the code looks like this:
 
String Jsons = gen.getAsString(); 
        System.debug(Jsons);         
        request.setBody(Jsons);   
        HttpResponse response = http.send(request); 
        request.setTimeout(60);   
        try{      	
            if(response.getStatusCode() == 200){
            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){
            if((response.getStatusCode() == 500)||(response.getStatusCode() == 504)){
            System.debug('Erro - Serviço de Ateração de e-mail: ' + e);      
            }
        }  
        return null; 
    }
}