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
Ana K. BertuzziAna K. Bertuzzi 

I need to create an url with a dynamic instance

Hi,

I had an apex class that works if I write the instance I'm running. But if I try to change it to a "dynamic" instance, meaning that it detects the instance I'm logged in, I'm getting a "null"

I found a piece of code and I tried to make it work here, not sure what I'm doing wrong (cause I actually have no idea of coding). Please help!

I wonder if has anything to do with using both PageRefence and gerSalesforceBaseUrl in the same class.

Thank you!!

public class MyflowController {
   public Flow.Interview.Tickets Myflow {get; set;}
   public PageReference getLinkID() {
      String U1 = 'https://';
      String U2 = '.salesforce.com/a1G/e?retURL=%2Fa1G%2Fo&RecordType=';
      String U3 = '&ent=02I400000013Z1q';
      PageReference p = new PageReference (U1+Instance+U2+RTid()+U3);
      p.setRedirect(true);
      return p;
      }
   public String RTid() {
      if (Myflow==null) return '';
      else return Myflow.varRecordType;
      }
  
   private String Instance = null;
   private void GetURL(){
   Instance = GetInstance(Instance);
   }
   
   public String GetInstance(String Instance) {
      if (Instance == null) {
         List<String> parts = System.URL.getSalesforceBaseUrl().getHost().replace('-api','').split('\\.');
            if (parts.size() == 3) Instance = parts[0];
            else if (parts.size() == 5) Instance = parts[1];
            else Instance = null;
         } 
      return Instance;
      }
   }


Best Answer chosen by Ana K. Bertuzzi
Ana K. BertuzziAna K. Bertuzzi
I was able to make it work, here's the code:
public class MyflowController {
   public Flow.Interview.Tickets Myflow {get; set;}
   public String fullURL {get; set;}
   private String Instance = null;
   private void GetURL() {
      Instance = GetInstance(Instance);
      fullURL = 'https://' + Instance + '.salesforce.com/a1G/e?retURL=%2Fa1G%2Fo&RecordType=' + RTid() + '&ent=02I400000013Z1q';
      }
   public PageReference getLinkID() {
      GetURL();
      PageReference p = new PageReference (fullURL);
      p.setRedirect(true);
      return p;
      }
   public String RTid() {
      if (Myflow==null) return '';
      else return Myflow.varRecordType;
      }
   public String GetInstance(String Instance) {
      if (Instance == null) {
          List<String> parts = System.URL.getSalesforceBaseUrl().getHost().replace('-api','').split('\\.');
          if (parts.size() == 3) Instance = parts[0];
          else if (parts.size() == 5) Instance = parts[1];
          else Instance = null;
          } 
          return Instance;
       }
   }
Anyway, now I'm supposed to create a test for it. If anyone knows an easy way to do it... I would appreciate it!