If I click the button in the Aura Component, I want to go to the Url I want

auracontroller

JS Controller

onconfirm : function(component, event) {
        var windowHref = window.location.href;
        var url = new URL(windowHref);
        var recordId = url.searchParams.get("c__recordId");

        var recordtype = component.get("v.selectedRecordType");
        console.log('recordType : ' + recordtype);
        console.log('parameter : ' + recordId);
        var action = component.get("c.CreateNewCon");
        action.setParams({
            "recType" : recordtype 
        });
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                console.log("This is Success");
                // I'd like to move from here to url.
            } else {
                console.log('This is Error');
            }
        });

        $A.enqueueAction(action);

    }

I want to process the following code successfully in apex controller and go to the URL when the status is SUCCESS. What should I do?

Best Answer

You can use any of the following event based on where you want to navigate.

  • force:navigateToSObject
  • force:navigateToRelatedList
  • force:navigateToSObject
  • force:navigateToURL
  • force:navigateToObjectHome

E.g you want to navigate to certain URL then use force:navigateToURL event.

Add gotoURLfunction to your component and Inside if condition use it.

    if (state === "SUCCESS") {
            const URL = 'your url here';
            this.gotoURL(URL);
        }

gotoURL : function (url) {
    var urlEvent = $A.get("e.force:navigateToURL");
    urlEvent.setParams({
      "url": url
    });
    urlEvent.fire();
}

Refer component library for more information - https://developer.salesforce.com/docs/component-library/bundle/force:navigateToURL/documentation

Related Topic