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
NehashriNehashri 

How to handle standard Save Event with a lightning component using Lightning Data Service on the same page

Hi all,
I have been struggling this for a while. I have a simple component using Lightning Data Service which basically lists out accounts with Similar ratings on the Current Account page. It loads fine but when I edit the Account Rating on the same page, it doesnt update/refresh the Lightning Component. I tried using force:refresh event. It didnt work. I am not sure how can I use Custom events for this since the Save is standard. May be I am not handling the recordUpdated correctly.
here is my code:

SimilarRatingsController.js
({
	doInit : function(component, event, helper) {
        var eventParams = event.getParams();
        if(eventParams.changeType === "LOADED"){
            console.log('Record loaded');
        //get a list of accounts
        var action = component.get("c.getSimilarRatings");
        action.setParams({
            accrating: component.get("v.account.fields.Rating.value")
            
        });
        action.setCallback(this, function(response){
            var state = response.getState();
            if(component.isValid() && state === "SUCCESS"){
                var similarRatings = response.getReturnValue();
                console.log("Accounts "+similarRatings);
                var lenAccts = similarRatings.length;
                for(var i=0; i<lenAccts; i++){
                    console.log(similarRatings[i].Rating);
                }
            	component.set("v.similarRatings", similarRatings);
            }else{
                console.log("Failed with state :"+state);
				console.log("Component is :"+component);
            }
            
            
        });
        $A.enqueueAction(action);
        }else if(eventParams.changeType === "CHANGED"){
            console.log('Record updated');
            
        }
	}
})

SimilarRatings.cmp
<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId,force:lightningQuickActionWithoutHeader" access="global" controller="NearByAccount">
    <aura:attribute name="recordId" type="Id"/>
    <aura:attribute name="account" type="Account"/>
    <aura:attribute name="similarRatings" type="Object[]"/>


    
    <!--<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>-->
    <!--<aura:handler event="c:recordUpdated" action="{!c.doInit}"/>-->
    <force:recordData recordId="{!v.recordId}"
                      targetRecord="{!v.account}"
                      targetFields="{!v.simpleAccount}"
                      fields="Id, Name, Rating"
                      
                      recordUpdated="{!c.doInit}"/>
    <lightning:card iconName="standard:account" title="Similar Accounts" class="slds-is-relative">
        <div class="slds-p-left_medium slds-p-right_medium">
            <ul>
            <aura:if isTrue="{!v.nearbyAccounts.length &gt; 0}">
     			<aura:iteration items="{!v.similarRatings}" var="acc">
         			<li class="slds-list__item">
             			<c:NearByAccount account="{!acc}"/>
         			</li>
    	
    			</aura:iteration>
                <aura:set attribute="else">
                	<li class="slds-list__item">
                        <h3 class="slds-text-small slds-text-color_error">No nearby Accounts found.</h3>
                    </li>
                </aura:set>
            </aura:if>
            </ul>
        </div>
    </lightning:card>
</aura:component>
SimilarAccount.cmp
<aura:component >
    <aura:attribute name="account" type="Account"/>
    <!--<aura:registerEvent name="recordUpdated" type="c:recordUpdated"/>-->
    <lightning:recordViewForm aura:id="viewForm" recordId="{!v.account.Id}" objectApiName="Account">
        <div class="slds-tile__detail">
        <div class="slds-grid slds-grid_align-spread slds-has-flexi-truncate">
        	<a onclick="{!c.navToRecord}" >
            	<h3  class="slds-tile__title slds-truncate" title="Salesforce UX">
                
                    {!v.account.Name} 
                   
               
            	</h3>
            </a>
        </div>
    
    </div>
    </lightning:recordViewForm>
    
	
</aura:component>

SimilarAccount.js
({
	 navToRecord : function (component, event, helper) {
        var navEvt = $A.get("e.force:navigateToSObject");
        navEvt.setParams({
            "recordId": component.get("v.account.Id")
        });
        navEvt.fire();
	},
})
SimilarAccount
public with sharing class SimilarAccount {
   
    
    @AuraEnabled
    public static List<Account> getSimilarRatings(String accrating){
        System.debug('Rating '+accrating);
        List<Account> accList = [SELECT Id, Name, Rating FROM Account
                                WHERE Rating = :accrating];
        System.debug('Account '+accList);
        return accList;
    }

}





 
Ravi Dutt SharmaRavi Dutt Sharma
Try changing the doInit method to below. Let me know if it works.
 
({
	doInit : function(component, event, helper) {
	
        //get a list of accounts
        var action = component.get("c.getSimilarRatings");
        action.setParams({
            accrating: component.get("v.account.fields.Rating.value")
            
        });
        action.setCallback(this, function(response){
            var state = response.getState();
            if(component.isValid() && state === "SUCCESS"){
                var similarRatings = response.getReturnValue();
                console.log("Accounts "+similarRatings);
                var lenAccts = similarRatings.length;
                for(var i=0; i<lenAccts; i++){
                    console.log(similarRatings[i].Rating);
                }
            	component.set("v.similarRatings", similarRatings);
            }else{
                console.log("Failed with state :"+state);
				console.log("Component is :"+component);
            }
        });
        $A.enqueueAction(action);
	}
})

 
NehashriNehashri
Hi Ravi,
So basically you want me to remove the If Loaded statement from my INIT method?