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 

How do I make this custom community component pull fields from the user's Contact?

How do I make this custom component pull fields from the Contact of the signed in community user instead of their Account?

I've tried changing some of the vairables but can't seem to get it to work, so here is the original component code.

Apex class:
public class AccountDisplayController {
	@AuraEnabled
    public static string getAccountId(){
        User currentUser = [Select id,Contact.AccountId from USER where Id =: userInfo.getUserId()];
        return string.valueOf(currentUser.Contact.AccountId);        
    }
}

cmp:
<aura:component controller="AccountDisplayController" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
    
    <aura:attribute name="recordId" type="string" default = '0010r00000BtdLoAAJ'/>
    <aura:handler name="init" value="{!this}" action="{!c.init}" />
    
    	<aura:if isTrue="{! not( empty( v.recordId ) ) }">
            <div class="billingtitle"><h1>Billing Information</h1></div>
            <lightning:recordViewForm recordId="{!v.recordId}" objectApiName="Account">
                
                <div class="slds-grid">
                    <div class="slds-col slds-size_1-of-2">
                        <lightning:outputField fieldName="Agreement_Date__c"/>
                        <lightning:outputField fieldName="Renewal_Date__c"/>
                    </div>
                    <div class="slds-col slds-size_1-of-2">
                        <lightning:outputField fieldName="Number_of_Seats_Purchased__c"/>
                        <lightning:outputField fieldName=""/>
                    </div>
                </div>
            </lightning:recordViewForm>
        </aura:if>
    
</aura:component>

Helper:
({
	getAccountId : function(component, event, helper) {
		var getAccountIdAction = component.get("c.getAccountId");
        getAccountIdAction.setCallback(this, function(response){
            if(response.getState() === 'SUCCESS'){
                component.set("v.recordId", response.getReturnValue());
            } else {
                alert('Something went wrong!!!');
            }
            
        });
        $A.enqueueAction(getAccountIdAction);
	}
})

 
ANUTEJANUTEJ (Salesforce Developers) 
Hi Spencer,

In case if you place the component in the org and if you are able to fetch the details and display them when placed on the home page or record page then you might want to check the recent update release notes regarding the permission as the auraenabled methods are being used.

As per the release notes, the critical update activates then the guest user will not be able to access the Apex class until you give access to the class on Guest user Profile.

Reference:https://releasenotes.docs.salesforce.com/en-us/winter20/release-notes/rn_lc_restrict_apex_guest_users.htm

I can see that you have already given access to the Apex class. However, I would suggest you double-check if you have given them access to the correct guest profile.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Warm Regards,
Spencer BerkSpencer Berk
@ANUTEJ

I appreciate you providing the Restrict Apex class release notes reference, as that has helped me solve a different issue.

My question was how to change the code of the component from displaying Account fields to displaying Contact fields. I have now been able to display Contact fields using the apex class: AccountDisplayController but this apex class is already being used by another component to display Account fields. I have copied the code for the AccountDisplayController apex class and created ContactDisplayController apex class but it isn't working, even though the code is the exact same.

I can't figure out why it works with the 
public class ContactDisplayController {
	@AuraEnabled
    public static string getAccountId(){
        User currentUser = [Select id,ContactId from USER where Id =: userInfo.getUserId()];
        return string.valueOf(currentUser.ContactId);        
    }
}
<aura:component controller="ContactDisplayController" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
    
    <aura:attribute name="recordId" type="string" default = '0031U000019rNoEQAU'/>
    <aura:handler name="init" value="{!this}" action="{!c.init}" />
    
    	<aura:if isTrue="{! not( empty( v.recordId ) ) }">
            <div class="billingtitle"><h1>Billing Information</h1></div>
            <lightning:recordViewForm recordId="{!v.recordId}" objectApiName="Contact">
                
                <div class="slds-grid">
                    <div class="slds-col slds-size_1-of-2">
                        <lightning:outputField fieldName="Agreement_Date__c"/>
                        <lightning:outputField fieldName="Renewal_Date__c"/>
                    </div>
                    <div class="slds-col slds-size_1-of-2">
                        <lightning:outputField fieldName="MailingAddress"/>
                        <lightning:outputField fieldName=""/>
                    </div>
                </div>
            </lightning:recordViewForm>
        </aura:if>
    
</aura:component>