Search code examples
salesforceapex

Salesforce Redirect to Salesforce ID based on External ID


I am wanting to send someone into Salesforce using our External ID (that is stored in Salesforce) then have some process bounce them to the correct page in Salesforce.

So, for example, I have an External ID for Account and Opportunity. Lets assume they are Account.AccountExtID__c and Opportunity.OpportunityExtID__c.

What I would like to be able to do is construct URLs using my External ID (i.e. https://thisismyorg.my.salesforce.com/OpportunityExtID__c) then have Salesforce lookup the OpportunityExtID__c on the relevant object (via SOQL or whatever), then redirect the user to the correct page (https://thisismyorg.my.salesforce.com/006xxxxxxxxxxx).

What would be the best way to go about this (if this is even possible)?

Thanks in advance!!


Solution

  • You can create a visualforce page to redirect to the opportunity using an external Id (any). the url would be something like: https://thisismyorg.my.salesforce.com/apex/OppExt?extId=OpportunityExtID__c

    create an apex controller similar to this one:

    public class OpportunityRedirectController {
    
    public Opportunity opp { get; set; }
    public String extId { get; set; }
    
    public OpportunityRedirectController() {
        extId = ApexPages.currentPage().getParameters().get('extId');
        opp = [SELECT Id FROM Opportunity WHERE External_Id__c = :extId limit 1];
    }
    
    public PageReference redirect() {
        PageReference pageRef = new PageReference('/' + opp.Id);
        pageRef.setRedirect(true);
        return pageRef;
    }
    }
    

    change the External_Id__c field to your external Id, if you have several change it to this: public class OpportunityRedirectController {

    public Opportunity opp { get; set; }
    public String extId { get; set; }
    
    public OpportunityRedirectController() {
        extId = ApexPages.currentPage().getParameters().get('extId');
        opp = [SELECT Id FROM Opportunity WHERE External_Id__c = :extId OR External_Id_2__c = :extId limit 1];
    }
    
    public PageReference redirect() {
        PageReference pageRef = new PageReference('/' + opp.Id);
        pageRef.setRedirect(true);
        return pageRef;
    }
    }
    

    Than create a page with these contents:

    <apex:page controller="OpportunityRedirectController" action="{!redirect}">
    </apex:page>
    

    this should get you started, it will however fail when there is no opportunity found.

    possible test class:

    @isTest
    private class OpportunityRedirectControllerTest {
    
        @isTest static void testRedirect() {
            // Create a test opportunity
            Opportunity testOpp = new Opportunity(Name='Test Opp', External_Id__c='1234');
            insert testOpp;
    
            // Instantiate the controller
            OpportunityRedirectController controller = new OpportunityRedirectController();
    
            // Set the external ID parameter
            ApexPages.currentPage().getParameters().put('extId', '1234');
    
            // Test that the controller retrieves the correct opportunity and redirects to its page
            System.assertEquals(controller.opp.Id, testOpp.Id);
            System.assertEquals(controller.redirect().getUrl(), '/' + testOpp.Id);
        }
    }