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 

Prevent saving the email field with the apex class

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?
AnudeepAnudeep (Salesforce Developers) 
Hi Célio, 

Yes, you can handle this in your apex code. Please see the sample code below
 
@RestResource(urlMapping='/mypath/*')
global without sharing class MyRest {
  
    @HttpGet 
    global static void get() {
        RestResponse res = RestContext.response;
        if (res == null) {
            res = new RestResponse();
            RestContext.response = res;
        }
        try {
            res.responseBody = Blob.valueOf(JSON.serialize(doGet()));
            res.statusCode = 200;
        } catch (EndUserMessageException e) {
            res.responseBody = Blob.valueOf(e.getMessage());
            res.statusCode = 500;
        } catch (Exception e) {
            res.responseBody = Blob.valueOf(
                    String.valueOf(e) + '\n\n' + e.getStackTraceString()
                    );
            res.statusCode = 500;
        }
    }

    private static Object doGet() {
        ...
    }
}

NOTE: The code provided is an example. You'll need to review and make modifications for your organization.

Let me know if it helps. If it does, please mark this answer as best. It may help others in the community. Thank You!