Categories
Apex Salesforce Vlocity

Handling redirect in Vlocity Integration Procedure having response code of 307

CONTROL YOUR EMOTIONS TO EMPOWER YOUR BRAIN

Problem:-Clients api was having redirect policy on the server but its not handled by Vlocity integration procedure(IP), thus its failing with 307 response code

Solution:- 

  • Error:- 307 Temporary Redirect
  • Inside IP create “Remote Action” to refer the Apex class and apex method, which handles the redirect 307 error.
  • Note:- This is not handled by default in IP inside vlocity

    //change this to implement vlocity_openInterface 
    global  class CC_RedirectDemoController {
        global static void getSession(){
            Http http = new Http();
            HttpRequest req = new HttpRequest();
            HttpResponse res = new HttpResponse();
            // do your initial http call here
            req.setMethod('POST');
            req.setHeader('Content-Type', 'application/json');
            req.setHeader('key', 'value');
            req.setEndpoint('URL to hit initially');
            req.setBody('body input data if available');
            system.debug('request ='+req);
            res = http.send(req);
            system.debug('response ='+res);
            // redirection checking
            boolean redirect = false;
            if(res.getStatusCode() >=300 && res.getStatusCode() <= 307 && res.getStatusCode() != 306) {
                do {
                    redirect = false; // reset the value each time
                    String loc = res.getHeader('Location'); // get location of the redirect
                    if(loc == null) {
                        redirect = false;
                        continue;
                    }
                    system.debug('inside redirect loc ='+loc);
                    req = new HttpRequest();
                    req.setEndpoint(loc);
                    req.setMethod('POST');
                    req.setHeader('Content-Type', 'application/json');
                    req.setEndpoint('URL to hit initially');
                   req.setBody('body input data if available');
                  );
                     req.setBody('body input data if available');
                    res = http.send(req);
                    if(res.getStatusCode() != 500) { // 500 = fail
                        if(res.getStatusCode() >=300 && res.getStatusCode() <= 307 && res.getStatusCode() != 306) {
                            redirect= true;
                        }
                        // I do special handling here with cookies
                        // if you need to bring a session cookie over to the
                        // redirected page, this is the place to grab that info
                    }
                } while (redirect && Limits.getCallouts() != Limits.getLimitCallouts());
            }
            //congratulations you're outside of the redirects now
            //read what you need from the res object
            system.debug(res.getBody());
        } 
    }

 

Leave a Reply

Your email address will not be published. Required fields are marked *