+ Start a Discussion
dmchengdmcheng 

getSalesforceBaseUrl returns http: not https:

Hello. When I execute Url. getSalesforceBaseUrl().toExternalForm() in the system console, the returned URL uses https:// However, when I execute this in the finish() method of Batch Apex, it returns http:// 

Is this a bug? I need https:// returned in the Batch Apex. 

Thanks 
David
 
Best Answer chosen by Admin (Salesforce Developers) 
Christopher_Alun_LewisChristopher_Alun_Lewis

Hi David,

 

I had a similar problem. I was using the URL.getSalesforceBaseUrl().toExternalForm() to construct a link inside an email template. When the templates were sent, the 'https://' portion of the link morphed into 'http://', which did not work correctly.

 

I made the following utility method in Apex to ensure I could retrieve the base url beginning with 'https://'. Its a bit crude, and this may well be a bug, but for now it guarantees that I get the right link.

 

    public static String getHttpsSalesforceBaseURL(){
    	
    	String baseURL = URL.getSalesforceBaseUrl().toExternalForm();
    	
    	if (baseURL.startsWith('http:')){
    		baseURL = baseURL.replaceFirst('http:', 'https:');
    	}
    	
    	return baseURL;
    }

 

Regards,

 

Christopher Alun Lewis

 

 

All Answers

Christopher_Alun_LewisChristopher_Alun_Lewis

Hi David,

 

I had a similar problem. I was using the URL.getSalesforceBaseUrl().toExternalForm() to construct a link inside an email template. When the templates were sent, the 'https://' portion of the link morphed into 'http://', which did not work correctly.

 

I made the following utility method in Apex to ensure I could retrieve the base url beginning with 'https://'. Its a bit crude, and this may well be a bug, but for now it guarantees that I get the right link.

 

    public static String getHttpsSalesforceBaseURL(){
    	
    	String baseURL = URL.getSalesforceBaseUrl().toExternalForm();
    	
    	if (baseURL.startsWith('http:')){
    		baseURL = baseURL.replaceFirst('http:', 'https:');
    	}
    	
    	return baseURL;
    }

 

Regards,

 

Christopher Alun Lewis

 

 

This was selected as the best answer
Chirag MehtaChirag Mehta
This is still a bug, replace seems to be only solution.
mark lee 46mark lee 46
I also found the same bug please help https:// returned in the Batch Apex. I also visited on this site gacha life 2 (https://www.gachalife2.net/) Mac for the solution.