[SalesForce]

I'm using the following lightning component using <force:recordData
when my component loads I get the following error message

You don't have access to this record. Ask your administrator for help
or to request access.

I could not able to figure out what does it mean and what do I need to do?

I have a created a new object called "EmployeeForm__c" insert some records and here is my component looks like:

<aura:component implements="flexipage:availableForAllPageTypes" 
                controller="EmpFormCtrl"
                access="global" >

    <aura:attribute name="Id" type="String" access="public" default="" />

    <aura:attribute name="obj" type="Object" access="Private" />
    <aura:attribute name="objFields" type="Object" access="Private" />
    <aura:attribute name="objError" type="String" access="Private"  /> 

    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    <aura:handler name="change" value="{!v.Id}" action="{!c.onChangeId}" />



    <aura:if isTrue="{!empty(v.Id)}">
        <force:recordData aura:id="recordOps" 
                          targetRecord="{!v.obj}" 
                          targetFields="{!v.objFields}" 
                          targetError="{!v.objError}" 
                          mode="EDIT"
                          fields="Name,start_Date__c" />

        <aura:set attribute="else">
        <force:recordData aura:id="recordOps" 
                          recordId="{!v.Id}"
                          targetRecord="{!v.obj}" 
                          targetFields="{!v.objFields}" 
                          targetError="{!v.objError}" 
                          mode="EDIT"
                          fields="Name,start_Date__c" /> 
                          recordUpdated="{!c.onBlur}"/>

        </aura:set>
    </aura:if>

    <aura:if isTrue="{!not(empty(v.objError))}">
      <div>
            <ui:message title="error here..." severity="error" closable="true">
          {!v.objError}
            </ui:message>
      </div>
    </aura:if>

    <lightning:card  >
        <form class="slds-form—stacked" style="margin: 10px">

            <lightning:input type="text"
                             name="Name"
                             label="Emp Name"
                             value="{!v.objFields.Name}" 
                             aura:Id="iName"
                             required="true" />
            <lightning:input name="startdate"
                             type="date"        
                             label="Start Date"   
                             value="{!v.objFields.start_Date__c}"
                             aura:Id="startdate"
                             required="true" />


            <div class="slds-text-align--center" 
                 style="margin-top:5px">

                <lightning:button variant="Brand" 
                                  label="Save" 
                                  aura:id="btnSubmit" disabled="true" onclick="{!c.onSave}" />

                <lightning:button label="Cancel" />

            </div> 
    </form>
    </lightning:card>
</aura:component>

Controller:

({
    doInit : function(component, event, helper) {
       helper.onInit(component,event,helper);
    },

    onChangeId: function(component,event,helper) {
      helper.onChangeId(component);  
    }, 
})

Helper:

({
    onChangeId: function(component) {
        debugger;
        var Id = component.get('v.Id');
        var recordOps = component.find("recordOps");
        if (recordOps && recordOps.getNewRecord && (Id == '' || !Id)) {
             debugger;
            recordOps.getNewRecord(
                "Employee__c ", // sObject type (objectApiName)
                null,      // recordTypeId
                false,     // skip cache?
                $A.getCallback(function() {                    
                    var rec = component.get("v.obj");
                    var error = component.get("v.objError");
                    if(error || (rec === null)) {
                        console.log("Error initializing record: " + error);
                        return;
                    }
                    debugger;
                    console.log("Record initialized: ",rec);

                })
            );
        }
    },

    onInit: function(component) {
       //initialize....        
      this.onChangeId(component);
    }, 

})

After debugging the code I see the error throwing in the helper class where the onChangeId function is on the line recordOps.getNewRecord(....

Best Answer

Lightning Data Service applies sharing and FLS rules. Verify that the user has access to the specified entity, record and fields and that such a record id exists.