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
PRepakaPRepaka 

how to retrieve server URL in APEX class or in trigger?

hi
 
i want to retrieve the Server URL in trigger or in APEX class?
 
Actual my requirement is:
 
i had an object RelatedAttachement. in that Hyperlink field is of URL type. in that i am saving the URL like /servlet/servlet.FileDownload?file=00PR0000000MqVH. For this i want to retrieve the server URL and concatenate that Server URL to this field and updating this field.  in one server it looks like http://cs2.salesforce.com/servlet/servlet.Download?file=00PR0000000MqVH and in another server it looks like http://na2.salesforce.com/servlet/servlet.Download?file=00PR0000000MqVH so for this one i want to retrieve the server URL and i want to concatenate that URL to that field.
 
So please tell me how to retrieve the Server URL in trigger or in APEX class? 
hisrinuhisrinu
Hi Padma,

 Have you tried with the geturl() function?
Doug ACFDoug ACF
Not sure if this fits your use case, but in Visualforce controllers, you can use ApexPages.currentPage().getHeaders().get('Host') to get the server name (e.g., na1.salesforce.com).


Message Edited by Doug ACF on 11-02-2008 10:16 AM
beenerbeener
Thanks so much. The resulting links require a login, even though we were just logged in. This is what I'm trying to solve. Any Ideas would be much appriciated.

Thanks

Here's what I've done.
In the VF page datatable:
<apex:outputLink value="http://{!Host}/{!t.id}" target="_blank">

In the Controller Class :

public string getHost(){
return ApexPages.currentPage().getHeaders().get('Host');
}


The resulting links require a login, even though we were just logged in. This is what I'm trying to solve.

(I've checked that the resulting links are well formed: example: http://c.eu0.visual.force.com/0032000000IBor6AAD )
just by way of a reminder: relative links are working, but offcourse not when I render the content as Excel.

Also, I'm sure there's a way of obtaining these parameters using code in the page, and not in the Apex class. any ideas?

Thanks so much


Message Edited by beener on 01-03-2009 04:39 AM
wesnoltewesnolte

Change the output link to point to 'https' instead of 'http'.

 

Wes

Eric_SantiagoEric_Santiago

This should not have been marked solved. The original question was "So please tell me how to retrieve the Server URL in trigger or in APEX class? "

 

Neither Triggers nor standard Apex classes (that are not controllers) have access to ApexPages.currentPage().

 

It still baffles me that there is just not something as simple as {!Organization.ServerHost} yet. 

Force2b_MikeForce2b_Mike

Try using the following. It pulls the URL using the standard controller from any object.

 

String url = new ApexPages.StandardController(acct).view().getHeaders().get('Host')

 

Mike 

wesnoltewesnolte
@ hmmm, are you sure? I've used this code in a number of places and it works.
Message Edited by wesnolte on 06-08-2009 12:58 AM
Eric_SantiagoEric_Santiago

Yeah. I'm positive that ApexPages.currentPage().getHeaders().get('Host') fails in a Trigger because ApexPages.currentPage() has no context in a class or trigger not associated with a VF page. It will give you a nice System.NullPointerException.

 

ScrappyDog's suggestion looked more promising, but all it returned was NULL. Here's my code. Let me know if I'm doing anything  wrong. 

 

Here's what I've tried;

 

  • String host = new ApexPages.StandardController(sol).view().getHeaders().get('Host'); returns null
  • String host = pageRef.getHeaders().get('Host'); returns null
  • String host = pageRef.getURL(); returns only the partial url

 

 

 

trigger AttachedSolutions on CaseSolution__c (after delete, after insert, after undelete,
after update) {

List<CaseSolution__c> impactingSolutions = new List<CaseSolution__c>();
List<Id> caseIds = new List<Id>();
List<Case> casesToUpdate = new List<Case>();
Map<Id, String> caseSolutions = new Map<Id, String>();

If (Trigger.isDelete || Trigger.isUnDelete) {
ImpactingSolutions = Trigger.old;
} else {
ImpactingSolutions = Trigger.new;
}

//build list of cases we need to update
for (CaseSolution__c sol : impactingSolutions) {
caseIds.add(sol.Case__c);
}

if (caseIds.size() > 0){
List<Case> updatedCases = [select Id
,KB_Solution_Title_Links__c
from Case
where Id in :caseIds
];

List<CaseSolution__c> solAttached = [select Id
,Name
,Case__c
from CaseSolution__c
where Case__c in :caseIds
];

for (CaseSolution__c sol : solAttached) {
PageReference pageRef = new PageReference('/' + sol.id);
String host = new ApexPages.StandardController(sol).view().getHeaders().get('Host'); //returns null
String Url = 'https://' + host + pageRef.getUrl();
String cseSolution = sol.Name + ' - ' + Url + '\n\n';
if (!caseSolutions.containsKey(sol.Case__c)){
caseSolutions.put(sol.Case__c,cseSolution);
} else {
cseSolution += caseSolutions.get(sol.Case__c);
caseSolutions.put(sol.Case__c,cseSolution);
}
}


for (Case cse : updatedCases){
cse.KB_Solution_Title_Links__c = caseSolutions.get(cse.Id);
casesToUpdate.add(cse);
}

update casesToUpdate;
}

}

 

 

 

Message Edited by Eric_Santiago on 06-08-2009 08:36 AM
wesnoltewesnolte

Ah, I see. Sorry bud.

 

Conceptually it makes sense that you don't have access to that information at a trigger level, as a trigger is usually associated with operating within(or on) the database.

 

The only thing I can think of immediately would be to write the value of the afformentioned method to the database at some earlier point and fetch the value out again when needed. Of course this may not be applicable for your situation.

 

Good luck.

sofogabesofogabe
thanks for this post guys - exactly what I was looking for.
DevNVDevNV

What I've done in the past is create a formula field on the object I'm working with in apex and then I can access it at anytime, plus it is dynamic to sandbox or production servers.  The only time this won't work is if you are using My Domain since it is getting the server name, not your custom domain.

 

So in the Task scenario where I am creating an email alert about a new task, I have added a field called Server_URL to the Activity table with the following formula:

 

 

left($Api.Enterprise_Server_URL_25 , FIND("/", $Api.Enterprise_Server_URL_25, 9))

 

and then, in either my VF email template or the email object I create in apex, I can reference the task.server_url__c+task.id to get the URL. 

 

 

Hope that helps.

 

Niki

www.vankerksolutions.com

aliozdenaliozden

Modified the formula to remove "-api"..

 

Try this..

 

Substitute(left($Api.Enterprise_Server_URL_25 , FIND("/", $Api.Enterprise_Server_URL_25, 9)),"-api","")

 

 

Ali

Prafulla N PatilPrafulla N Patil

Create a custom label named - SFDC_Server_URL with value of your server URL 'https://cs17.salesforce.com/'  and access it in the apex code as follows -

 

 

System.label.SFDC_Server_URL

 

Creating formula field on every object is the worst option

DevNVDevNV

Salesforce.com has also released a new URL method that can pull your system URL in Apex.  The only issue I've seen is that depending on your context it could bring in the api or VF URL when you want the user to see their regular na##.salesforce.com link.  But something to investigate that wasn't an option a few years ago.

 

http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_methods_system_url.htm

 

Custom Setting is a good idea as well but it needs adjusting if you are running code in Sandbox and also when the sandbox is regenerated you have to add the entry again in your custom setting - why don't they copy our custom setting data over like they do Products and other setup data?  Wouldn't that be nice...

WeipingWeiping

You can try this one.  I used in Apex Trigger and it works.

 

URL.getSalesforceBaseUrl().toExternalForm() + '/' + myAccount.Id;

Mario G.Mario G.
Old question but just to confirm...
Weiping's solution is the right answer!
ref. https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_url.htm