0

I would like to know how to how redirect from Contact to related User record page via Lightning component quick action. Currently I have created Component which gets Contact record Id and passes it to JS controller then calls server to query User based upon this Id. The User Id value is returned successfully, just need a function for redirect and would like to know how to implement in existing code. I've read about "navigateToURL" one but have no clue how to use it. Sorry for beginner-like question.

Code:

<aura:component implements="force:lightningQuickActionWithoutHeader, force:hasRecordId" controller="RedirectToUser" >
    <aura:attribute name="recordId" type="String" />
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />

public with sharing class RedirectToUser {
    @AuraEnabled
    public static Id redirect(String contactId) {
        if(String.isNotBlank(contactId)){
            return [SELECT Id FROM User WHERE ContactId =: contactId].Id;
        }
        else{
            return null;
        }

    }
}

({
    doInit : function(component, event, helper) {
        var action = component.get("c.redirect");
        action.setParams({"contactId": component.get("v.recordId")});

        action.setCallback(this, function(response) {
            var state = response.getState();
            if(state === "SUCCESS") {
               // component.set("v.account", response.getReturnValue());
               console.log(response.getReturnValue());
               var userId = response.getReturnValue();
               redirectToUser(component, event, userId);

            } else {
                console.log('Problem getting contact, response state: ' + state);
            }
            $A.get("e.force:closeQuickAction").fire();
        });
         $A.enqueueAction(action);


    },

    redirectToUser : function(component, event, userId) {
            var urlEvent = $A.get("e.force:navigateToURL");
            var str = "/" + userId;
            urlEvent.setParams({
                "url": str
            });
            //console.log(str);
            urlEvent.fire();

        }
})
0

You can use the force:navigateToSObject event to redirect to a record page:

redirectToUser : function (component, event, userId) {
    var navEvt = $A.get("e.force:navigateToSObject");
    navEvt.setParams({
      "recordId": userId
    });
    navEvt.fire();
}
  • Ok, the question is more then about if I can call redirectToUser function from doInit function in setCallback "Success" case like I do, because with the code that I have I ran into error - – Pmal12 Mar 23 '18 at 14:08
  • Uncaught Error in $A.getCallback() [redirectToUser is not defined] Callback failed: apex://RedirectToUser/ACTION$redirect – Pmal12 Mar 23 '18 at 14:08
0

Assuming that your server-side redirect method returns valid data (which by I suggest you look into), I use something v similar and it works fine:

    redirectToUser : function(component, event, userId) {
        var redirectUrl = '/' + userId;
        $A.get("e.force:navigateToURL").setParams({
           "url": redirectUrl
        }).fire();
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.