Snippets

jprichter Dynamic Navigation in Napili Communities

Created by jprichter

File NapiliCommunityController Added

  • Ignore whitespace
  • Hide word diff
+public class NapiliCommunityController {
+    @AuraEnabled
+    public static User getLoggedInUser(){
+        return [SELECT Id, Contact.AccountId FROM User WHERE Id =: UserInfo.getUserId() LIMIT 1];
+    }
+}

File myAccountRedirect Added

  • Ignore whitespace
  • Hide word diff
+<aura:component controller="NapiliCommunityController" implements="forceCommunity:availableForAllPageTypes" access="global">
+	<aura:handler name="init" value="{!this}" action="{!c.redirectToAccount}" />
+</aura:component>

File myAccountRedirectController.js Added

  • Ignore whitespace
  • Hide word diff
+({
+    redirectToAccount: function(component, event, helper) {
+        var loggedInUser;
+        var state;
+        var navEvt;
+        
+        var action = component.get("c.getLoggedInUser");
+        action.setCallback(this, function(response) {
+            state = response.getState();
+            if (state === "SUCCESS") {
+                loggedInUser = response.getReturnValue();
+                navEvt = $A.get("e.force:navigateToSObject");
+                navEvt.setParams({
+                    "recordId": loggedInUser.Contact.AccountId,
+                    "slideDevName": "detail"
+                });
+                navEvt.fire();
+            }
+        });
+        $A.enqueueAction(action);
+    }
+})
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.