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
Andy Kallio 7Andy Kallio 7 

force:recordData reload not working for Admin

Hello Friends!

I have a server side class that needs the recordtype.developer name of the current record. I am trying to use lightning data service to get the developername. So, I have 2 <force:recordData> attributes. The first stores the current record and the second is for the recordType. So, my intention is get the recordTypeId from the current record and use the reloadRecord method to get LDS to populate the other fields I need. I can see that I am setting recordTypeId just fine but for some reason simpleRecordType remains null after the reloadRecord Command. Can anybody see what I am doing wrong?

 

({  
   
    handleRecordUpdated: function(cmp, event, helper) {
        
        var eventParams = event.getParams();
        if(eventParams.changeType === "LOADED" || eventParams.changeType === "CHANGED") {
      		// get the simpleRecord attribute from the component
      		var simpleRecordTypeId = cmp.get('v.simpleRecord.RecordTypeId');
            
            var recordTypeId = cmp.get('v.recordTypeId');
            // if this is the first time we're loading the record then 
            // populate the ownerid and load the record
            if(recordTypeId == null){
                // assign the base record's ownerid onto the attribute we've
                // attached to the second force:recordData's recordid value.
                cmp.set('v.recordTypeId', simpleRecordTypeId);
                // fire the component event to reload the record now
                // that we've given it a record id.
                cmp.find('recordType').reloadRecord(true);
            }

            var actionVF = cmp.get('c.getVFOptions');
            actionVF.setParams({
                sObjectName : cmp.get('v.sObjectName'),
                recordTypeName : cmp.get('v.simpleRecordType.DeveloperName')
            });
            actionVF.setCallback(this, function(response) {
                debugger;
                var stateVF = response.getState();
                if(stateVF == "SUCCESS") {
                    var vfOptions;
                    vfOptions = response.getReturnValue();
                    var custs = [];
                    var conts = vfOptions.cOpts;
                    for(key in conts) {
                        custs.push({value:conts[key], key:key});
                    } 
                    cmp.set('v.templateOptions', custs);   
                    
                    var vfPage;
                    vfPage = vfOptions.vfPage;
                    cmp.set('v.vfPage', vfPage);
                    
                }
            });
            // Send action off to be executed
            $A.enqueueAction(actionVF);

        } else if(eventParams.changeType === "CHANGED") {
           
        } else if(eventParams.changeType === "REMOVED") {
            // record is deleted
        } else if(eventParams.changeType === "ERROR") {
            var errors = response.getError();
            console.log('errors '+errors);
        }
    },
    
    onSingleSelectChange: function(cmp) {
        var selectCmp = cmp.find("InputSelectSingle");
        cmp.set("v.selectedTemplate", selectCmp.get("v.value"));
	},
    
    savePDF : function(cmp, event, helper) {
        var recordId = cmp.get("v.recordId");
        var selectedTemplate = cmp.get("v.selectedTemplate");
        var qName = cmp.get("v.simpleRecord.Name");
        var pdfName = selectedTemplate;
        var vfPage = cmp.get("v.vfPage"); 
        var action = cmp.get("c.savePDF_Quote");
        action.setParams({
            "vfPage": vfPage, 
            "recordId" : recordId, 
            "selectedTemplate" : selectedTemplate, 
            "pdfName" : qName
        });
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                alert('Attachment saved successfully');
                
            }
            else if (state === "INCOMPLETE") {
                // do something
            }
                else if (state === "ERROR") {
                    var errors = response.getError();
                    if (errors) {
                        if (errors[0] && errors[0].message) {
                            console.log("Error message: " + 
                                        errors[0].message);
                        }
                    } else {
                        console.log("Unknown error");
                    }
                }
        });
        $A.enqueueAction(action);
    }
    
})
 
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,force:hasRecordId,force:hasSObjectName,lightning:isUrlAddressable" 
                access="global" 
                controller="pdfDI_SelectTemplate2_Controller">
    
    <aura:attribute name="selectedTemplate" type="String"/>
    <aura:attribute name="templateOptions" type="List"/>
    <aura:attribute name="simpleRecord" type="Object"/> 
    <aura:attribute name="vfPage" type="String"/>
     
    
    <force:recordData aura:id="recordLoader"
        recordId="{!v.recordId}"
        fields="Name, RecordTypeId"
        targetFields="{!v.simpleRecord}"
        recordUpdated="{!c.handleRecordUpdated}"               
    />
    
    <aura:attribute name="recordTypeId" type="Id"/> <!-- assigned in Javascript after record load -->
    <aura:attribute name="simpleRecordType" type="RecordType"/>
    <aura:attribute name="recordTypeError" type="String"/>
    <force:recordData aura:id="recordType"
        recordId="{!v.recordTypeId}"
        fields="Id, Name, DeveloperName"
        targetFields="{!v.simpleRecordType}"
        targetError="{!v.recordTypeError}"              
        recordUpdated="{!c.handleRecordUpdated}"              
    />
    
    <lightning:layout multipleRows="false">
        <lightning:layoutItem size="6">
        	<div class="row">
        		<!--<p class="title">Select Template:</p>-->
    			<ui:inputSelect class="single" aura:id="InputSelectSingle" change="{!c.onSingleSelectChange}">
        			<option value="">...Select a Template...</option>
            		<aura:iteration items="{!v.templateOptions}" var="t_opt" indexVar="key">
                		<ui:inputSelectOption text="{!t_opt.value}" label="{!t_opt.key}"/>
            		</aura:iteration>
        		</ui:inputSelect>  
    		</div>
        </lightning:layoutItem>
    	<lightning:layoutItem size="6">
        <div class="slds-p-left_large">
    	 	<lightning:button label="Save PDF" title="Save PDF" onclick="{! c.savePDF }"/>
    	</div>
        </lightning:layoutItem>  
    </lightning:layout>
	<div class="slds-p-top_large">
        <p>PDF Preview:</p>
    	<c:pdfDI_DisplayTemplate selectedTemplate="{!v.selectedTemplate}" recordId2="{!v.recordId}" vfPage="{!v.vfPage}"/>
    </div>  
</aura:component>

I have been using this as an example: https://github.com/aheber/ObjectAgnosticLightingComponent