0

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';

1
  • 1
    You problem is that you need to URLEncode the serverURL param
    – Torsen
    Sep 21, 2016 at 7:14

2 Answers 2

0

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;
}
7
  • I tried the above approach.but still after the serverurl1 it is not getting session id.sessionid si getting placed at the last.below is the debug log statement.
    – karthik
    Sep 21, 2016 at 17:04
  • |Debug| here is your Pageref = System.Pagereference[composer.congamerge.com/composer8/…
    – karthik
    Sep 21, 2016 at 17:12
  • @karthik where are you setting the sessionId1??
    – Torsen
    Sep 22, 2016 at 6:12
  • string ServerURL1 = ApexPages.currentPage().getHeaders().get('Host'); that is incorrect if you are in a visualforce because the visualforce server is xx.visual.force.com not xx.salesforce.com. In 5 sec I send you the correct way to obtain the server
    – Torsen
    Sep 22, 2016 at 6:15
  • your serverURL1 should be Url.getSalesforceBaseUrl().toExternalForm()
    – Torsen
    Sep 22, 2016 at 6:17
1

Instead of appending those parameters to URL string, just add it to Parameters Map, like below:

pageRef.getParameters().put('sessionId',SessionId1);

Also, please note that, for example, you won't see forward slash (/) as a part of your URL parameters in the browser, as that character will be encoded to %2F. But that's normal, and you should still be able to read those parameters in its original form on the page you navigated to.

You must log in to answer this question.

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