You need to sign in to do that
Don't have an account?
Spencer 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); } })
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
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
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?
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