function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Spencer BerkSpencer Berk 

Community component that redirects to custom object detail page?

This component redirects a community user to the user's Account Detail page in a community. How would I change the code to redirect to a custom object (Billing__c) Detail page? I've tried changing the recordId path to LoggedInUser.Contact.AccountId.Billing__c with no luck. My path might be wrong. I am not a developer so any advice would be greatly appreciated.
({
    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);
    }
})

 
Best Answer chosen by Spencer Berk
Maharajan CMaharajan C
Hi Berk,

Update your class like below:

Public class BillingController{
    @AuraEnabled
    public static Id getLoggedInUser() {
        Id billingId;
        try
        {
            User u = [SELECT Id, Contact.AccountId FROM User WHERE Id =: UserInfo.getUserId()];    
            Id accId = u.Contact.AccountId;
            Billing__c billingRec= [Select Id from Billing__c where Account__c =: accId limit 1];  // use the correct Api name for Account__c master detail field from Biiling Object in this line
            billingId = billingRec.Id;
        }
        catch(Exception ex){
            system.debug(' Exception ==> ' + ex.getMessage());
        }
        return billingId;
    }
}


Updated JS Controller:

({
    redirectToBilling: function(component, event, helper) {
        var billingId;
        var state;
        var navEvt;
        
        var action = component.get("c.getLoggedInUser");
        action.setCallback(this, function(response) {
            state = response.getState();
            if (state === "SUCCESS") {
                billingId = response.getReturnValue();
                navEvt = $A.get("e.force:navigateToSObject");
                navEvt.setParams({
                    "recordId": billingId,
                    "slideDevName": "detail"
                });
                navEvt.fire();
            }
        });
        $A.enqueueAction(action);
    }
})


Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Berk,

First you have to change the Apex Class for this.

Find the related apex class of this component.

You can easily identify the Releated Apex Class by using  your aura component file ( In your Aura Component the first line will hold the Apex Class Name in Controller  property <aura:component   Controller="Apex Class Name">

Open the related apex class then go to the  getLoggedInUser  method there you can see some Query like below.

user u   =  [ Select id,Contact.AccountId from user where Id =: UserInfo.getUserId()  ]; 

Change the above Query like below:

user u = [Select Id, Contact.Account.Billing__c  from User where Id =: UserInfo.getUserId()​​​​​​​ ];

then update the javascript file in Aura Component like below.

navEvt.setParams({
     "recordId": loggedInUser.Contact.Account.Billing__c,
     "slideDevName": "detail"
});

Thanks,
Maharajan.C

 
Spencer BerkSpencer Berk
I'm receiving an error in the Problems pane. SELECT Id, Contact.Account.Billing__c FROM ^ ERROR at Row:1:Column:12 No such column 'Billing__c' on entity 'Accont'. Billing (child) has a Master-Detail relationship to Account. Would that change the relationship path?
APEX CLASS:
public class BillingController {
    @AuraEnabled
    public static User getLoggedInUser(){
        return [SELECT Id, Contact.Account.Billing__c FROM User WHERE Id =: UserInfo.getUserId() LIMIT 1];
    }
}


CONTROLLER:

({
    redirectToBilling: 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.Account.Billing__c,
                    "slideDevName": "detail"
                });
                navEvt.fire();
            }
        });
        $A.enqueueAction(action);
    }
})
Maharajan CMaharajan C
Yes we have to change the Logic.

One question this Account will have multiple billings right or only one billing?

If it's multiple then which billing record you want to fetch under this Account?
Spencer BerkSpencer Berk
Each account will only have one Billing record.
Maharajan CMaharajan C
Hi Berk,

Update your class like below:

Public class BillingController{
    @AuraEnabled
    public static Id getLoggedInUser() {
        Id billingId;
        try
        {
            User u = [SELECT Id, Contact.AccountId FROM User WHERE Id =: UserInfo.getUserId()];    
            Id accId = u.Contact.AccountId;
            Billing__c billingRec= [Select Id from Billing__c where Account__c =: accId limit 1];  // use the correct Api name for Account__c master detail field from Biiling Object in this line
            billingId = billingRec.Id;
        }
        catch(Exception ex){
            system.debug(' Exception ==> ' + ex.getMessage());
        }
        return billingId;
    }
}


Updated JS Controller:

({
    redirectToBilling: function(component, event, helper) {
        var billingId;
        var state;
        var navEvt;
        
        var action = component.get("c.getLoggedInUser");
        action.setCallback(this, function(response) {
            state = response.getState();
            if (state === "SUCCESS") {
                billingId = response.getReturnValue();
                navEvt = $A.get("e.force:navigateToSObject");
                navEvt.setParams({
                    "recordId": billingId,
                    "slideDevName": "detail"
                });
                navEvt.fire();
            }
        });
        $A.enqueueAction(action);
    }
})


Thanks,
Maharajan.C
This was selected as the best answer
Spencer BerkSpencer Berk
Thank you for your quick help! Much appreciated! You saved me a lot of head ache.
rfgrfg
@Maharajan C  I am trying to do something similar but not sure where to put the JS controller code. I am familiar with VF pages and Apex controllers but have not worked with JS controllers in Salesforce before. Do I need to save that code in a separate file or can it go in the .vfp file with the rest of the Visualforce for the page? If separate file, do I just save it with a .js extension? How does the .vfp reference it? If there is some basic documentation on how these files work together you could just link me to that, thank you!