[SalesForce] How to pass URL to visualforce page with pagereference method without scattering the url

below is the apex method through which i am passing a URL string to a button in Visual force Page.

public Pagereference Submit(){

  loanId1 = 'a2529000000MIBg';
  String SessionId1 = Userinfo.getSessionID();
  system.debug('here is your SessionId = '+ SessionId1);

  string ServerURL1 = ApexPages.currentPage().getHeaders().get('Host');
  system.debug('here is your ServerURL = '+ ServerURL1);

 HttpRequest req = new HttpRequest();
 req.setEndpoint('https://composer.congamerge.com/composer8/index.html');
 req.setMethod('GET');

 string Startdate1 = Startdate.format();
 system.debug('Startdate1 = '+ Startdate1);

 string Enddate1 = Enddate.format();
 system.debug('Enddate1 = '+ Enddate1);

 string Organizationid = UserInfo.getOrganizationId();
 system.debug('Organizationid = '+ Organizationid);

  String URL = 'https://composer.congamerge.com/composer8/index.html?sessionId='+SessionId1+'&serverUrl='+serverurl1+'/services/Soap/u/37.0/'+Organizationid+'&id='+LoanId1+'&templateid=a1U29000000l0vd&reportid=[trn]00O29000000Oo8o?pv1='+Startdate1+'&pv2='+Enddate1+'&defaultpdf=&ds7=3';
   system.debug('Here is your URL =' + URL);

   Pagereference Pageref = New pagereference(URL);
   pageRef.setRedirect(true);
   system.debug('here is your Pageref ='+ Pageref);

   return Pageref;
}

}

the Pagereference method is breaking the URL.
how to pass the URL in correct format as defined in the String URL.
like this https://composer.congamerge.com/composer8/index.html? sessionId='+SessionId1+'&serverUrl='+serverurl1+'/services/Soap/u/37.0/'+Organizationid+'&id='+LoanId1+'&templateid=a1U29000000l0vd&reportid=[trn]00O29000000Oo8o?pv1='+Startdate1+'&pv2='+Enddate1+'&defaultpdf=&ds7=3';

Best Answer

You need encode the serverURL parameter

CODE SNIPPET

public Pagereference Submit(){

  loanId1 = 'a2529000000MIBg';
  String SessionId1 = Userinfo.getSessionID();

  String ServerURL1 = Url.getSalesforceBaseUrl().toExternalForm();

  HttpRequest req = new HttpRequest();
  req.setEndpoint('https://composer.congamerge.com/composer8/index.html');
  req.setMethod('GET');

  String Startdate1 = Startdate.format();
  system.debug('Startdate1 = '+ Startdate1);

  String Enddate1 = Enddate.format();
  system.debug('Enddate1 = '+ Enddate1);

  String Organizationid = UserInfo.getOrganizationId();
  system.debug('Organizationid = '+ Organizationid);


  String salesforceURL = EncodingUtil.urlEncode(ServerURL1+'/services/Soap/u/37.0/'+Organizationid,'UTF-8');

  Pagereference newPage = new pagereference('https://composer.congamerge.com/composer8/index.html');
  newPage.getParameters().put('sessionId',SessionId1);
  newPage.getParameters().put('id',loanId1);
  newPage.getParameters()-put('templateid','a1U29000000l0vd');
  newPage.getParameters()-put('reportid','[trn]00O29000000Oo8o?pv1='+Startdate1+'&pv2='+Enddate1);
  newPage.getParameters()-put('ds7','3');
  newPage.getParameters()-put('defaultpdf','true');
  newPage.getParameters().put('serverUrl',salesforceURL);
  return Pageref;
}
Related Topic