0

Ik have found this example of a controller directing to the account detailpage lightning component of account which works. I am trying to reproduce this for a custom component but my links shows /s/test123 and keeps loading when I remove test123 and replace with detail/id it works

where does this link test123 come from? am I missing something?

Class Account

public class NavigateToAccountController {
    @AuraEnabled
    public static String getLoggedInUser(){
        User currentUser = [SELECT Id, Contact.AccountId FROM User WHERE Id =: UserInfo.getUserId() LIMIT 1];
        if(currentUser.contact.AccountId != null){
            return currentUser.contact.AccountId;
        }else{
            return 'NULL';
        }
    }
}

Controller lightning account

({
    redirectToAccount: function(component, event, helper) {
        var AccountId;
        var state;
        var navEvt;

        var action = component.get("c.getLoggedInUser");
        action.setCallback(this, function(response) {
            state = response.getState();
            if (state === "SUCCESS") {

                AccountId = response.getReturnValue();
                //console.log(AccountId);
                if (AccountId != 'NULL'){
                    navEvt = $A.get("e.force:navigateToSObject");
                    navEvt.setParams({
                        "recordId": AccountId,
                        "slideDevName": "detail"
                    });
                    navEvt.fire();
                } else{
                    var toastEvent = $A.get("e.force:showToast");
                    toastEvent.setParams({
                        "title": "Error!",
                        "message": "Logged in user don't have Account Id associated"
                    });
                    toastEvent.fire();
                }               
            }
        });
        $A.enqueueAction(action);
    }
})

Class custom object

public class NavigateToProfielController {
    @AuraEnabled
    public static String getLoggedInUser(){
        User currentUser = [SELECT Id, Contact.Profiel__r.name FROM User WHERE Id =: UserInfo.getUserId() LIMIT 1];
        if(currentUser.Contact.Profiel__r.name!= null){
            system.debug('currentUser.Contact.Profiel__r.name '+ currentUser.Contact.Profiel__r.name);
            return currentUser.Contact.Profiel__r.name;

        }else{
            return 'NULL';
        }
    }
}

Controller lightning custom object

({
    redirectToProfiel: function(component, event, helper) {
        var Profielid;
        var state;
        var navEvt;        
        var action = component.get("c.getLoggedInUserContact");
        action.setCallback(this, function(response) {
            state = response.getState();
            if (state === "SUCCESS") {

                Profielid = response.getReturnValue();
                //console.log(Profiel__c.id);
                if (Profielid != 'NULL'){
                    navEvt = $A.get("e.force:navigateToSObject");
                    navEvt.setParams({
                        "recordId": Profielid,
                        "slideDevName": "detail"
                    });
                    navEvt.fire();
                } else{
                    var toastEvent = $A.get("e.force:showToast");
                    toastEvent.setParams({
                        "title": "Error!",
                        "message": "Logged in user don't have Contact Id associated"
                    });
                    toastEvent.fire();
                }               
            }
        });
        $A.enqueueAction(action);
    }
})

Component code

<aura:component controller="NavigateToProfielController" implements="forceCommunity:availableForAllPageTypes" access="global">
<aura:handler name="init" value="{!this}" action="{!c.redirectToProfiel}"/>

  • I do not understand the intent of 2 lightning controllers. For the second controller check in your org if there is a profile by name "test123" and if the user account from which you are testing this component has the profile "test123" assigned. From your code you are setting recordId to be response.getReturnValue() which is currentUser.Contact.Profiel__r.name – Rao Sep 26 '17 at 21:56
  • @rao the reason is om both object I see the related list which is 1 relation I want to show the detail page directly for both of these cases. Looked ad profiles but no test123. Besides this its a custom object Profiel and not Proflile dont know if jou saw this – Jenssen Sep 26 '17 at 22:24
  • can you post your component code and when you mean link which link are you referring to, the url that forms on navigation? – Rao Sep 26 '17 at 22:35
  • @Rao I added the component code above. And yes reffering to url from navigation : force.com/s/test123 – Jenssen Sep 26 '17 at 23:17
  • What do you see in the console when you do console.log(Profeilid); – Rao Sep 27 '17 at 0:13
0

In your controller method (NavigateToProfielController.getLoggedInuser), you are returning the name of your user, and not the Id for that user (return currentUser.Contact.Profiel__r.name), which is then inserted to ProfielId in your lightning js controller, and finally placed in your e.force:navigateToSObject param for recordId. As a result, instead of force.com/s/*record_id_here, you will get force.com/s/*name_of_record_here. Try updating your query to return the Contact.Profiel__c to get your Id, and return that.

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.