• Kirby Jonas
  • NEWBIE
  • 0 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 3
    Replies
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);
           
        } 
    }

 
Tells me to post this code
ZXSEXMAN
I've alreay tried to make the same step into another DE , but still the same issue.

That's the module
https://trailhead.salesforce.com/pt-BR/content/learn/projects/flow_reassign/flow_reassign_reassign