0

I'd like to validate a field in Salesforce, against some other external REST API.

I thought I could do this with triggers, but when I tried something like this:

trigger testvalidate on Account (before insert) {

        for (Account a : trigger.new)
        {
            ... 

            HttpRequest req = new HttpRequest();
            req.setEndpoint('https://my url here ...');
            req.setMethod('POST');
            req.setBody(json);

            Http http = new Http();
            HttpResponse resp = http.send(req);

            ...
        }

}

I get an error like this:

estvalidate: execution of BeforeInsert caused by: System.CalloutException: Callout from triggers are currently not supported. Trigger.testvalidate: line 15, column 1

Is there a way to do validation against external services in Salesforce?

Thanks!

0

You can't make a synchronous call but, you can do an asynchronous call with the help of a future method as below.

trigger testvalidate on Account (before insert) {

        for (Account a : trigger.new)
        {
            AccountHelper.callWebService(a.Id);
        }

}

public with sharing class AccountHelper {

    @future(callout=true)
    public static void callWebService(Id accId){
        //Do the webservice callout here
    }
}

Please note there is a Limit of 10 callouts in a single context. So you have to design according to your requirement to avoid hitting the limit.

| improve this answer | |

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.