I've seen this question on the Salesforce.com message boards but have never seen an answer so I thought I would post it here. Here's my dilemma... we are writing some customization that requires us to construct the URL to a Salesforce.com record (e.g. https://na5.salesforce.com/02u300000000ACi) and send it via email. I've seen a solution to this using Visualforce but never in Apex.

Here is what I came up with but it seems that there must be a better way. The only constant that I could come up with is our Production org ID. So my Apex class holds a reference to this ID and then I have a method that checks the current user's org ID and constructs the URL.

public class MyCustomClass {

    // set the production org id so we can see what system we are on
    private static final String PRODUCTION_ORG_ID = '00Z990000001ZZZ';

    // returns the correct system url for the current org
    private String orgUrl {
        get {
            return UserInfo.getOrganizationId() == PRODUCTION_ORG_ID ? 'http://na5.salesforce.com' : 'http://cs1.salesforce.com';
        }
        set;
    }

    // write some code that calls orgUrl....

}