0

I want to take the field value dynamically in lightning controller . I got the object result but unable to the field value.

({
doInit: function(component, event, helper) {

    var action = component.get("c.getFieldValues");
    action.setParams({
        sObjectName :component.get("v.sObjectName"),
        fieldNames  :component.get("v.fieldNames")
    });

    action.setCallback(this, function(response) {

        var state = response.getState();            
        var fieldArray = [];
        fieldArray = component.get("v.fieldNames").split(',');
        console.log('###'+fieldArray[0]);
        var accFieldName=fieldArray[0];

        if (state === "SUCCESS") {
            var objData = response.getReturnValue();
            console.log('####gotFieldValue'+objData.Name); // this line value is getting values
            console.log('####NoValue'+objData.accFieldName); //this line value is not getting 
        }
    });
    $A.enqueueAction(action);
}

})

Here for objData I am getting the value. If i try to take (objData.Name) the account name is coming .But i want to pass the field value dynamically from fieldNames.It is not working .

Please help me !!

  • do you want to get field values dynamically from list of fields? – rahul gawale Apr 26 at 6:29
  • 1
    yes,I want to get the fields dynamically – jessy Apr 26 at 6:51
1

In JavaScript, to dynamically get field of object you should use obj[FIELD].

Try this line instead:

console.log('####NoValue'+objData[accFieldName]);

Please mark it as solution once it works.

0

If you want to get all fields, try this.

//read key
for (var key in objData) {
   console.log(key);
   console.log(objData[key]);
}
0

We can get the field value like below :

 console.log('####gotFieldValue'+objData[accFieldName]); 
  • No idea why you respond to yourself instead of marking the current answer for who actually helped you – Derminal Apr 26 at 20:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.