0

In my lightning component I have sObject list which I render as data table. Some of the cells/fields are editable and onChange of those fields I recalculate other fields.
In my lightning component Helper method (called via controller when field value changes), I call another helper method and pass the field name as String which I need to update. Is there a way to dynamically reference that field and update it's value?

 //Helper Method 1
calculateSegmentsAmounts : function(component, event, helper) {

    var prepayRecord = component.get("v.prepaymentRecord");
    var segmentsList = component.get("v.prepaymentSegmentsList");
    var calculatedTotalPercentage = 0;
    var calculatedTotalPrincipal = 0;
    var calculatedTotalInterest = 0;
    var calculatedTotalPremium = 0;
    var calculatedTotalBreakage = 0;
    var calculatedTotalOther = 0;

    for(var i=0; i < segmentsList.length; i++) {

        var percentage = 0;
        var principal = 0;

        if(segmentsList[i].Selected__c) {

            percentage = segmentsList[i].PAR_Percentage__c / 100;
            principal = percentage * prepayRecord.Native_Principal__c;                

            segmentsList[i].Interest__c = percentage * prepayRecord.Native_Interest__c;
            segmentsList[i].Premium__c = percentage * prepayRecord.Native_Premium__c;
            segmentsList[i].Breakage__c = percentage * prepayRecord.Native_Breakage__c;
            segmentsList[i].Other__c = percentage * prepayRecord.Native_Other__c;
            segmentsList[i].Total_Segment__c = principal + segmentsList[i].Interest__c + segmentsList[i].Premium__c + segmentsList[i].Breakage__c + segmentsList[i].Other__c;
        }
        percentage = percentage * 100;
        calculatedTotalPercentage += percentage;
        segmentsList[i].Principal__c = principal;

        calculatedTotalPrincipal += segmentsList[i].Principal__c;
        calculatedTotalInterest += segmentsList[i].Interest__c;
        calculatedTotalPremium += segmentsList[i].Premium__c;
        calculatedTotalBreakage += segmentsList[i].Breakage__c;
        calculatedTotalOther += segmentsList[i].Other__c;
    } // Loop ends

    // Update attribute value because it will be referred in below Helper methods
    component.set("v.prepaymentSegmentsList", segmentsList);

    var reCalcTotal = false;
    if(calculatedTotalPercentage == 100.00000000000) {
        if(calculatedTotalPrincipal != 0 && calculatedTotalPrincipal != prepayRecord.Native_Principal__c) {
            helper.adjustExtraAmount(component, event, helper, prepayRecord.Native_Principal__c, calculatedTotalPrincipal, "Principal__c"); // Passing Field name as String
            reCalcTotal = true;
        }
        if(calculatedTotalInterest != 0 && calculatedTotalInterest != prepayRecord.Native_Interest__c) {
            helper.adjustExtraAmount(component, event, helper, prepayRecord.Native_Interest__c, calculatedTotalInterest, "Interest__c"); // Passing Field name as String
            reCalcTotal = true;
        }
        if(calculatedTotalPremium != 0 && calculatedTotalPremium != prepayRecord.Native_Premium__c) {
            helper.adjustExtraAmount(component, event, helper, prepayRecord.Native_Premium__c, calculatedTotalPremium, "Premium__c"); // Passing Field name as String
            reCalcTotal = true;
        }
        if(calculatedTotalBreakage != 0 && calculatedTotalBreakage != prepayRecord.Native_Breakage__c) {
            helper.adjustExtraAmount(component, event, helper, prepayRecord.Native_Breakage__c, calculatedTotalBreakage, "Breakage__c"); // Passing Field name as String
            reCalcTotal = true;
        }
        if(calculatedTotalOther != 0 && calculatedTotalOther != prepayRecord.Native_Other__c) {
            helper.adjustExtraAmount(component, event, helper, prepayRecord.Native_Other__c, calculatedTotalOther, "Other__c"); // Passing Field name as String
            reCalcTotal = true;
        }
        if(reCalcTotal) {
            helper.reCalculateSegmentsRowTotal();
        }
    }

    component.set("v.prepaymentRecord", prepayRecord);
    component.set("v.prepaymentSegmentsList", segmentsList);
},

//Helper Method 2
adjustExtraAmount : function(component, event, helper, nativeAmount, calculatedTotal, fieldName) {

    var segmentsList = component.get("v.prepaymentSegmentsList");
    var multiplier = 100;
    var difference = (Math.round(multiplier * nativeAmount) - Math.round(multiplier * calculatedTotal)) / multiplier;    
    var absdiff = Math.abs(difference);
    var calculatedMoreThanActual;
    if(nativeAmount < calculatedTotal) {
        calculatedMoreThanActual = true;
    }
    else {
        calculatedMoreThanActual = false;
    }
    if (absdiff >= 0.01 && absdiff < 1) {

        var affliatedInvIndex = helper.getLargestaffiliatedINVIndex(component, event, helper, fieldName);  // Another Helper to which fieldName is passed 
        if (affliatedInvIndex == -1) {
            affliatedInvIndex = helper.getLargestSegmentIndex(component, event, helper, fieldName); // Another Helper to which fieldName is passed 
        }       
        var nonAffliatedInvIndex = helper.getLowestNonAffiliatedINVIndex(component, event, helper, fieldName); // Another Helper to which fieldName is passed 
        if (nonAffliatedInvIndex == -1) {
            nonAffliatedInvIndex = helper.getLowestSegmentIndex(component, event, helper, fieldName); // Another Helper to which fieldName is passed 
        }
        var runOnce = false;
        console.log('>>>> Recalculating ' + fieldName);
        for(var i=0; i < segmentsList.length; i++) {

            if(segmentsList[i].Selected__c) {

                var oldValue = 0;
                oldValue = segmentsList[i].fieldName;   // Need to dynamically fetch the value
                if(calculatedMoreThanActual) {
                    if (i == nonAffliatedInvIndex && !runOnce) {
                        var delta = oldValue + difference;          
                        segmentsList[i].fieldName = delta;     // Need to dynamically Update the value                       
                        runOnce = true;
                    }   
                }
                else {
                    if (i == affliatedInvIndex && !runOnce ) {
                        var delta = oldValue + difference;
                        segmentsList[i].fieldName = delta;  // Need to dynamically Update the value
                        runOnce = true;
                    }
                }               
            }
        }
    }
    component.set("v.prepaymentSegmentsList", segmentsList);
},
1
  • try using segmentsList[i][fieldName]. It works fine for me.
    – Sikha Baid
    Oct 18, 2017 at 11:29

2 Answers 2

1

You can use below to fetch field values passing fieldName as dynamic String

segmentsList[i][fieldName]

Here,

  • segmentsList is the list of records from an sObject,
  • i is the index of record you want to fetch,
  • fieldName is a string which is the API name of the field value to get or set.
1

You can easily do this and can get the dynamic field reference in controller

<aura:component controller="GetAFewContacts">
    <aura:attribute name="contacts" type="Contact[]" />
    <aura:attribute name="fieldNames" type="String[]" default='["Id","Name"]'/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    {!v.body}
</aura:component>

Now in controller

var contacts = component.get("{!v.contacts}");
var fieldNames = component.get("{!v.fieldNames}");
var NewBody = "";
for (var i=0; i<contacts.length; i++){
    for (var ii=0; ii<fieldNames.length; ii++){
        NewBody += contacts[i][fieldNames[ii]];
    }
    NewBody += "\n";
}
console.log(NewBody);

You must log in to answer this question.

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