1

How do I access a list of all of either the values or ids in a map? I thought just this.recTypeChoices.values would work but it does not. Here is my javascript setting the values in the map.

   // 1. get list of opportunity record types to display for selection
    getRecordTypeMap({sObjectName: this.sObjectType, oppTypes: this.oppTypes})
    .then(data => {
        this.recTypeChoices = Object.keys(data).map(key => ({
          label: key, 
          value: data[key]
        }));

        })
        .catch(error => {
            console.log('FAIL here is error ' + JSON.stringify(error));
            this.dispatchEvent(
                new ShowToastEvent({
                    title: 'Error!!',
                    message: error.message,
                    variant: 'error',
                }),
            );
            this.isLoading = false
        });

When I console.log this.recTypeChoices I get the following result:

here are the this.recTypeChoices.values [{"label":"New Business Dental","value":"012700000001e8XAAQ"},{"label":"New Business Dental Voluntary","value":"012700000001fK5AAI"},{"label":"New Business Health","value":"012700000001e8YAAQ"},{"label":"New Business Prescription","value":"012700000001e8ZAAQ"},{"label":"New Business Vision","value":"012700000001e8aAAA"},{"label":"New Business Vision Voluntary","value":"012700000001fK6AAI"}]

Apex is as follows:

   //method to return record type name and Id
  @AuraEnabled(cacheable=true)
  public static Map<string, id> getRecordTypeMap(String sObjectName, String oppTypes){ 
    List<RecordType> rtList = new List<RecordType>();
      //if the request is for the main record types
    if(oppTypes == 'Main New Business') {
       rtList = [
          select id, name
          from RecordType
          where (SObjectType = : sObjectName and (Name = : 'New Business Health' OR Name = : 'New Business Prescription' or Name = : 'New Business Dental'
          or Name = : 'New Business Dental Voluntary' or Name = : 'New Business Vision' or Name = : 'New Business Vision Voluntary'))];
    }
    Map<String, Id> recordTypeMap = new Map<String, Id>();
    for(RecordType rti : rtList) {
        recordTypeMap.put(rti.Name, rti.Id);
    }
    return recordTypeMap;
  }

2 Answers 2

0

data is already a map.

So you'd just write:

idMap = {};

getRecordTypeMap({sObjectName: this.sObjectType, oppTypes: this.oppTypes})
.then(data => {
    this.idMap = Object.keys(data);
}
2

You need to use map in javascript, here is an example

const data = [
  {"label":"New .....Dental","value":"0127.....Q"},
  {"label":"New .....Dental Voluntary","value":"01270....AI"}..etc
];

//define a new map
const dataMap = new Map();

//iterate and store it in map

data.forEach(item => {
  dataMap.set(item.label, item.value);
});

to get id's/values you can use as follow

const ids= Array.from(dataMap.keys());
const values = Array.from(dataMap.values());

for more information about javascript map click here

2
  • Thanks! Isn't my variable, this.recTypeChoices a map? I declare the variable as @track recTypeChoices ? I updated my question to include the apex. thanks!
    – Frank
    Jun 9 at 14:40
  • Yes, you are returning map. You can directly use Object.keys(data) to get id's and use Object.values(data) to get values
    – Lokesh
    Jun 9 at 14:45

You must log in to answer this question.

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