I have a button on a Lead. When clicked it sends a POST request to a third party non-salesforce. In order to to do the POST request I need authorization from the third party. Getting authorization means logging in to the third party with a pop up window, then getting redirected to my org.

The few redirects make me loose the Lead Id I was working with.


public class Infusionsoft {

public static String response {get; set;}
public static String accessToken {get; set;}
public static String accessCode {get; set;}
public Id currentLeadId {get;set;}

public Infusionsoft(ApexPages.StandardController con) {
  currentLeadId = con.getId();

  system.debug('Lead Id=' + currentLeadId);


//Get authorization code
public PageReference firstRedirect() {
    if( ApexPages.currentPage().getParameters().get('code') != null) {
        accessCode = ApexPages.currentPage().getParameters().get('code');
        return secondRedirect();
    else {
        return new PageReference('https://signin.infusionsoft.com/app/oauth/authorize?client_id=*****************&redirect_uri=https://touchpointcrm--dev--c.cs19.visual.force.com/apex/InfusionsoftAuthorization&response_type=code&scope=full');
//Get access token & refresh token
public PageReference secondRedirect() {
    Http h = new Http();
    HttpRequest req = new HttpRequest(); 


    string bodyRequest = ''; 
    string bodyResponse = ''; 

    bodyRequest += 'client_id=*********************';
    bodyRequest += '&client_secret=**********';
    bodyRequest += '&code=' + accessCode;
    bodyRequest += '&grant_type=authorization_code';
    bodyRequest += '&redirect_uri=https://touchpointcrm--dev--c.cs19.visual.force.com/apex/InfusionsoftAuthorization';

    req.setHeader('Content-Type', 'application/x-www-form-urlencoded'); 
    req.setHeader('Content-length', string.ValueOf(bodyRequest.length())); 


    HttpResponse res = h.send(req); 

    //Parse returned JSON
    JSONParser  parser = JSON.createParser(res.getBody());
    while (parser.nextToken() != null) {
     if ((parser.getCurrentToken() == JSONToken.FIELD_NAME)){
      String fieldName = parser.getText();
      String fieldValue=parser.getText();
      if(fieldName == 'access_token') {

        accessToken= parser.getText();

    system.debug('Access Token=' + accessToken);

    //query lead using Id

    //send another POST request with lead info using accessToken.

    return null;

VF page

<apex:page standardController="Lead" extensions="Infusionsoft" standardStylesheets="false" action="{!firstRedirect}">
        Lead id is {!currentLeadId}

Test Class

private class InfusionsoftTest {

    static void sendLeadCallout() {

        String token = 'sdafklj3kj22wfdfsa';

        Lead testLead = [SELECT Id, FirstName, LastName, Title, Company, Street, City, State, PostalCode, Country, Phone, MobilePhone, Email, LeadSource
                  FROM Lead LIMIT 1];

        Infusionsoft cont = new Infusionsoft(new ApexPages.StandardController(testLead));

        Test.setCurrentPageReference(new PageReference('Page.InfusionsoftAuthorization'));

        System.currentPageReference().getParameters().put('code', 'asdfjhkjhwlajksdhf3w');
        System.currentPageReference().getParameters().put('state', testLead.Id);
        Test.setMock(HttpCalloutMock.class, new InfusionsoftMock());




1 Answer 1


Use the state parameter in your initial OAuth call, and it should be given back to you when you're returned to your page. The state can be set here:

  return new PageReference(

You'll be given this parameter back upon return so that you can re-populate your page's state.

  • so on the second redirect I can access the value in state?
    – Tyler Zika
    Oct 11, 2016 at 0:14
  • 1
    @TylerZika Yes, it should appear in your page's parameter (e.g. ApexPages.currentPage().getParameters().get('state'))
    – sfdcfox
    Oct 11, 2016 at 0:22
  • How would I test for the state of the url? Like this?System.currentPageReference().getParameters().put('state', testLead.Id);
    – Tyler Zika
    Oct 12, 2016 at 15:28
  • 1
    @TylerZika Yes, for your unit test, you can manipulate the parameters directly.
    – sfdcfox
    Oct 12, 2016 at 15:30
  • 1
    @TylerZika Yes, you'd only need to call the firstRedirect method to get code coverage. Just make sure you add some asserts to make sure you're getting the correct results back.
    – sfdcfox
    Oct 12, 2016 at 15:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .