+ Start a Discussion
Yogesh BiyaniYogesh Biyani 

How do I convert HTTPRequest Apex call to External Services

I am currently using Apex to query an endpoint and parse the JSON result. I am making a call to an endpoint with consumer key/secret to get the access token and then using the token to query the final endpoint as shown in the code below.

I would like to convert this to External Service so that I can use it in a flow. Can someone give me detailed steps on how to convert this to external service? 

TIA.
public static void findidbyEmail(String email)
    {
        String idAPIKeyString = 'KEY:SECRET';
        
        Blob idBlob = Blob.valueof(idAPIKeyString);
        
        String idAuthorization = 'basic ' + EncodingUtil.base64Encode(idBlob);
        HttpRequest idRequest = new HttpRequest();
        
        idRequest.setEndpoint('https://api.sitename.com/token');
        idRequest.setMethod('POST');
        
        idRequest.setBody('grant_type=client_credentials');
        idRequest.setHeader('Authorization', idAuthorization);
        idRequest.setHeader('Content-Type', 'application/x-www-form-urlencoded');
        
        Http http = new Http();
        
        HTTPResponse res = new HTTPResponse();
        
        if (Test.isRunningTest() && (mock!=null)) {
            System.debug('Mock Response');
            res = mock.respond(idRequest);
            
        } else {
            System.debug('Callout Response');
            res= http.send(idRequest);
            
        }
        
        
        System.debug(res.getBody());     
        
        
        idAuthorizationJSON2Apex objidAuthJSON2Apex = new idAuthorizationJSON2Apex(System.JSON.createParser(res.getBody()));
        System.debug(objidAuthJSON2Apex.access_token);
  
        String access_token= objidAuthJSON2Apex.access_token;
        system.debug(access_token);
        
        
        //    Get the id        
        HttpRequest req2= new HttpRequest();
        
        req2.setEndpoint('https://api.sitename.com/users?email='+email);
        req2.setMethod('GET');
        req2.setHeader('Authorization', 'bearer '+ access_token);
        
        req2.setHeader('Content-Type', 'application/json');
        
        Http http2 = new Http();
        
        HTTPResponse res2 = new HTTPResponse();
        
        if (Test.isRunningTest() && (mock!=null)) {
            res2 = mock.respond(req2);
            System.debug('Mock Response ' + res2);
        } else {
            res2= http.send(req2);
            System.debug('Callout Response '+res2);
        }
        
        
        if(res2.getBody()=='')
            System.debug('id Not found');
        else 
        {
            System.debug(res2.getBody());
            System.debug('id found');
            idJSON2Apex objidJSON2Apex = new idJSON2Apex(System.JSON.createParser(res2.getBody()));
            System.debug(objidJSON2Apex.theEmail.uniqueid);
           
        } 
    }

 
ANUTEJANUTEJ (Salesforce Developers) 
Hi Yogesh,

I think you showld be able to call a apex class as you have already writtten the necessary call out functioanlity in an apex class you can just simply invoke to the flow which inturn would be invoking the apex class which should be able to solve your issue.

In case if this helped you could you please close the thread and mark this as best answer so that it can be used in the future by others and also helps in keeping the community clean.

Regards,
Anutej
Kirby JonasKirby Jonas
The callout makes a call to an external web service or sends an HTTP request from Apex code, and then receives the response My Lowes Life (https://www.myloweslife.vip/).
Yogesh BiyaniYogesh Biyani
Hello Anutej, I have quite a few services following the above pattern. So I am migrating to a point and click solution so that it is easy to maintain. 

Hello Kirby, I am not sure I understand your comments. Did you mean to add more information to the reply? BTW great implementation of the community cloud.

Yogesh