[SalesForce] How to access variables needed to create a URL for a New Relationship without hard coding it

Created a page for account and one for a contact, with a "Create new Relationship" button. You select with you want to relate with a contact or an account, and based on that it creates a URL for the type of relationship needed (contact-contact, contact-account, account-contact, account-account), and it pre-populates the name of the first object.
The problem is, how can i create these URLs by using salesforce variables/methods instead of hardcoding it?

For example:

  • a0a – means new relationship. How can i not hardcoded a0a to the url?
  • e?retURL – means redirect the url to this Edit page. how can i not hard code that?
  • CF00No00000020GfN- translates to the name of the first contact when creating a new relationship
  • CF00No00000020GfM – translates to the name of the first account
  • %20 – whitespace
  • %26 – & character

I know what most of these mean, but i don't want to hardcode this values, i want to be able to access this values from a salesforce variable, method, or etc.

For example:

  • URL.getSalesforceBaseUrl().toExternalForm() results in login.salesforce.com

where can i access more of these?

Part of my code below:

public PageReference newRelationshipButtonClicked(){

    String urlToGoTo = URL.getSalesforceBaseUrl().toExternalForm()+'/a0a/';

    if(firstObjectIsAccount){
        if(secondObjectIsAccount){ //both are accounts

            String accountName = myAccount.Name.replace(' ','%20');
            accountName = accountName.replace('&', '%26');
            urlToGoTo += 'e?retURL=%2Fa0a&RecordType=012o0000000Jz9n&ent=01Io0000000Y6H1&CF00No00000020GfM='+accountName;

            } else { //im account, person i wanna have a relation to is a contact

                 String accountName = myAccount.Name.replace(' ','%20');
                 accountName = accountName.replace('&', '%26');
                 urlToGoTo += 'e?retURL=%2Fa0a&RecordType=012o0000000Jz9m&ent=01Io0000000Y6H1&CF00No00000020GfM='+accountName;

                 }

        } else { //im a contact
            if(secondObjectIsAccount){ //im a contact, other is an account
                urlToGoTo += 'e?retURL=%2Fa0a&RecordType=012o0000000Jz9l&ent=01Io0000000Y6H1&CF00No00000020GfN='+myContact.FirstName+'%20'+myContact.LastName;
                } else{ //we are both contacts
                    urlToGoTo += 'e?retURL=%2F003o0000002ITxg&CF00No00000020GfN='+myContact.FirstName+'%20'+myContact.LastName;
                    }
            }
            PageReference newRelationshipPage = new PageReference(urlToGoTo);
            newRelationshipPage.setRedirect(true);
             return newRelationshipPage;
             }

Best Answer

I think what you might be looking for is $Action.

In short, it can be used in a VF page like so:

<apex:outputLink value="{!URLFOR($Action.Account.New)}">
    Create New Account
</apex:outputLink>

This link shows you a complete list of what $Action can do: https://www.salesforce.com/us/developer/docs/pages/Content/pages_variables_global_action_valid_values.htm

This is a link to show you how you can accomplish some dynamic routing using $action: https://www.salesforce.com/us/developer/docs/pages/Content/pages_dynamic_vf_globals_action.htm

Best of luck!

-Wes

Related Topic