2

I have a <lightning:recordViewForm in a custom lightning component.

When I hardcode the recordId attribute it works, but when I pass an attribute in it fails without any error messages in either the console or the debug log.

Component

<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
<aura:attribute name="recordId" type="Id"></aura:attribute>

<lightning:recordViewForm recordId="{! v.recordId }" objectApiName="MyCustomObject__c">

    <lightning:outputField fieldName="Name" />

</lightning:recordViewForm>

JavaScript Controller

doInit : function(cmp, event, helper) {

    // var recordId = helper.getQueryStringParameter('recordId');

    cmp.set("v.recordId", "a0C4E000003YMkmUAG");
}

The above does not work, even if I put the recordId directly into the attribute.

It only works when I set the Id in the recordId attribute of the <lightning:recordViewForm like below:

<lightning:recordViewForm recordId="a0C4E000003YMkmUAG" objectApiName="MyCustomObject__c">

    <lightning:outputField fieldName="Name" />

</lightning:recordViewForm>
2
  • Are you certain that var recordId is set to a valid Id e.g. the same one you hardcoded?
    – Keith C
    Commented Jun 23, 2018 at 15:51
  • @KeithC yup, because I am copying and pasting it from the Url into the recordId attribute, and I am logging it to the console to check
    – Robs
    Commented Jun 23, 2018 at 15:53

3 Answers 3

6

The recordViewForm (and similar siblings) are very finicky with the recordId attribute. Not sure if this is just a quirk or bug but try wrapping it with an <aura:if isTrue="{! not( empty( v.recordId ) ) }">

Basically, in my experience working with it, it needs the attribute ready before it's placed in the render cycle.

It just so happens <aura:if> doesn't initiate a render of the component until its conditions are truthy.

1
  • Thanks for this. Helped me solve a problem w/ somewhat different behavior. Mine was a custom component based on recordViewForm. The component threw an on-screen err in LEX on load (err was "Uncaught Action failed: recordViewForm$controller$handleReocrdIdChange"), then worked. But in a Community, which is where I first tried it, it worked if I had one instance of the component on a page, but not if I had two instances. In the latter case, the first would work, but the second would fail silently/mysteriously. This fixed both issues.
    – mscholtz
    Commented Sep 12, 2018 at 18:46
0

I was getting

AuraFriendlyError: Error in fetching record or record metadata. [Invalid record id]

I added the validation before the lightning:recordViewForm and it is working perfectly. Thank you.

 <aura:if isTrue="{! not( empty( v.Case.Id ) ) }">
    <lightning:recordViewForm recordId="{!v.Case.Id}" objectApiName="Case">
        <div class="slds-box">
            <lightning:outputField fieldName="CaseNumber" />
            <lightning:outputField fieldName="Status" />
            <lightning:outputField fieldName="RecordTypeId" />
            <lightning:outputField fieldName="Priority" />                
        </div>
    </lightning:recordViewForm>
</aura:if>
-1

For me, it was record access for a guest site user. Apex class was without sharing, Sharing Settings for External were set to Full Access/Public Read/Write... but the offender was a Critical Update regarding record access for Guest Site Users. Turning that off did the trick.

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .