function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
LudivineLudivine 

How to replace a hard-coded url by a function in an Apex Class?

Hi,

 

I have received a mesage from salesforce which say that they are going to move our org to a new one. from EU0 to EU3.

I have searched in all my org and I have found an Apex Class where this url is hard-coded.

I need to know how to re-write this with a function because it is not "nice" to have this and I don't want to replace eu0 by EU3...

 

str='https://c.cs4.visual.force.com/apex/eventview?id='+eid;
            //prod : str='https://c.eu0.visual.force.com/apex/eventview?

 

Could you help me here?thank you very much in advance.

 

 

public PageReference redirect1()
    {    
    
     String eventId=ApexPages.currentPage().getParameters().get('newid');
     String eid=ApexPages.currentPage().getParameters().get('id');
     string str;
     System.debug('#######'+eventId+'@@@@@@@'+eid);
       // if((eid.length()<6) && (eventId.length()>10))
       if(eid.length()>10)
        {
            str='https://c.cs4.visual.force.com/apex/eventview?id='+eid; 
            //prod : str='https://c.eu0.visual.force.com/apex/eventview?id='+eventId;    
            system.debug(str);     
          }
    PageReference r= new PageReference(str);
      r.setRedirect(true);
      System.debug('##str=https://c.cs4.visual.force.com/apex/eventview?id=+eventId =##'+str);
  //    return null;
        return r;
        
    } 

 

Best Answer chosen by Admin (Salesforce Developers) 
Devendra@SFDCDevendra@SFDC
Hi,

You can try this:

Pagerefence page = new Pagereference('/apex/MyFirstPage?id='+eid);
page.setRedirect(true);
return page;

As we are not hardcoding any URL here it will work without worrying about change of salesforce instance numbers.

Thanks,
Devendra

All Answers

Devendra@SFDCDevendra@SFDC
Hi,

You can try this:

Pagerefence page = new Pagereference('/apex/MyFirstPage?id='+eid);
page.setRedirect(true);
return page;

As we are not hardcoding any URL here it will work without worrying about change of salesforce instance numbers.

Thanks,
Devendra
This was selected as the best answer
LudivineLudivine

Thank you!!! I have amended the Apex class and it is working fine, could you check my code , do you see anything that should be simplified?

 

public PageReference redirect1()
 {    
    
     String eventId=ApexPages.currentPage().getParameters().get('newid');
     String eid=ApexPages.currentPage().getParameters().get('id');
     string str;
     System.debug('#######'+eventId+'@@@@@@@'+eid);
       // if((eid.length()<6) && (eventId.length()>10))
       if(eid.length()>10)
        {
            str='/apex/eventview?id='+eid; 
            //prod : str=/apex/eventview?id='+eid;    
            system.debug(str);     
          }
    PageReference r= new PageReference('/apex/eventview?id='+eid);
      r.setRedirect(true);
      //System.debug('##str=https://c.cs4.visual.force.com/apex/eventview?id=+eventId =##'+str);
  //    return null;
        return r;
        
    } 

 

 

Devendra@SFDCDevendra@SFDC
Hi,

I dont know what logic your are applying here.

But, about Pagereference statements, now its perfect..Thumps Up!!

Thanks,
Devendra