You need to sign in to do that
Don't have an account?
lxch
Return the values from Controller to the field in the <force:recordData />
Hi all, I'm working on get the current record with some fields (Name, Current_Balance__c) by <force:recordData /> in aura component. How do we get the value from Controller and update the record field?
Thanks!
Component
Apex Contorller
Thanks!
Component
<aura:component controller="DueBalanceController" implements="force:lightningQuickAction,force:appHostable,flexipage:availableForRecordHome,force:hasRecordId" access="global" > <aura:attribute name="accountRecord" type="Object"/> <aura:attribute name="sumBalance" type="Object"/> <aura:attribute name="today" type="Date" /> <aura:handler name="init" value="{!this}" action="{!c.init}"/> <force:recordData aura:id="recordLoader" recordId="{!v.recordId}" fields="Name,Current_Balance__c" targetFields="{!v.accountRecord}" /> <div> <lightning:card aura:id="warning-box" title="Billing So-far" > <lightning:layout> <lightning:layoutItem class="slds-p-around_x-small" size="12"> <lightning:formattedNumber value="{!v.accountRecord.Current_Balance__c}"/></div> </lightning:layoutItem> </lightning:layout> </lightning:card> </div> </aura:component>Contorller.js
({ init: function(component, event, helper) { var today = $A.localizationService.formatDate(new Date(), "YYYY-MM-DD"); component.set('v.today', today); var accountRecord = component.get("v.accountRecord"); var recordId = component.get ("v.recordId"); var action = component.get("c.sumBalance"); action.setParams({recordId:recordId}); action.setCallback(this, function(response){ let state = response.getState(); if (state === "SUCCESS") { component.set("v.sumBalance",response.getReturnValue()); component.set("v.accountRecord.Current_Balance__c",sumBalance.bl); component.find("recordLoader").reloadRecord(); } else { console.log("Failed with state: " + state); } }); $A.enqueueAction(action); }, })
Apex Contorller
public with sharing class DueBalanceController{ @AuraEnabled public static Object sumBalance (String recordId){ Date d_today = system.Date.today(); AggregateResult[] groupedResults = [ SELECT SUM(Balance__c)bl,SUM(Paid_Total__c)pt,SUM(Billing_Total__c)bt, Tenant__c FROM Billing__c WHERE Tenant__c = :recordId AND (User_Due_Date__c <= :d_today OR Paid_Date__c <=:d_today) AND (Status__c='Unpaid' OR Status__c='Paid' OR Status__c='Captured' OR Status__c ='Chasing Overdue') GROUP BY Tenant__c ]; return groupedResults[0]; } }
Hi Nayana,
This demonstrated what I wanted to do. However, there should be a better way to do with Aura/LWC. If you have any thought on this, please let me know!Still the issue with formData. But I found a different solution to return the aggregatedResults into the record field in Apex Class. Since the aggregatedResults is calculated in Apex Class, I assigned and update within the same Class.
All Answers
Above statement of reloadRecord will cause you to lose your current record, including any changes you’ve made.
Option1:
Comment this line and check whether current balance is displayed as expected.
Option 2: (If Option 1 is not working)
I tried both options but it says I also set random number '555555' in set, but it doesn' update the Current Balance field either. Still looking around the soluton. Anyway thank you for your advice, I'll try a few more tests with your codes, and report back here.
Hi Nayana,
This demonstrated what I wanted to do. However, there should be a better way to do with Aura/LWC. If you have any thought on this, please let me know!Still the issue with formData. But I found a different solution to return the aggregatedResults into the record field in Apex Class. Since the aggregatedResults is calculated in Apex Class, I assigned and update within the same Class.