4

Currently I develop a Lightning Component, which should show record details based on specific record field values.

Therefore I want to set dynamically component attributes based on some record values.

My current approach is to use a map attribute in the component, which contains the field name and the related values (<aura:attribute name="attribValuesMap" type="Map" />).

The related APEX controller returns a Map @AuraEnabled public static Map<String,String> getSpecificRecordValues(Id recordId){} and in the JS controller I set the component map attribute via callback.

Using debugging I see that the map contains the correct values - so this part works.

In the JS controller (doInit method) I want to iterate over this map to set other component attributes based on the map content, but this fails.

I always receive an error message like ".keys is no function", if I try something like:

var keyArr = Array.from(attribValuesMap.keys());

It seems that no "real" map is used, but a generic object.

Do you have another solution for me how to set dynamically component attributes based on record field values or can you let me know based on my approach how to iterate over maps in a JS controller?

2
  • Please share more of your code so we can pinpoint what the issue is. Even though Map is a valid aura:attribute type it is not the same as a Map object in JavaScript. I would recommend using Object.keys(attribValuesMap) instead or, even better, using force:recordData to load a record with the fields you need, no custom Apex necessary.
    – nbrown
    Feb 25, 2021 at 15:51
  • Using the provided iteration from sfdcfox solved my issue. Thanks, @nbrown for your further recommendations and hints. Feb 25, 2021 at 20:09

1 Answer 1

4

You can use Object.keys on this return value:

 var keyArr = Object.keys(attribValuesMap);

This is automatically already an Array, no further changes are necessary.

For iteration, you can use the normal:

for(var keys in attribValuesMap) {
1
  • Using the iteration you provided (for(var keys in attribValuesMap)) solved already my issue. The separate step using Object.keys() was not longer required. Thanks. Feb 25, 2021 at 18:11

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.