[SalesForce] Need to get the key of an Object Array given a Value

I have a registration process that differs from each record type, I need to get the recordType Name (or label in this case) so I can give proper flow to each user.

The record type selection is made with a radio button group element.

Record Type Selection

Console Output

Console output

Here is how I'm retrieving the RT from controller.

Controller method to get RecordTypes

@AuraEnabled
public static Map<Id, String> GetAvailableContactRecordTypes() {
    List<Schema.RecordTypeInfo> infos = Schema.getGlobalDescribe().get('Contact').getDescribe().getRecordTypeInfos();
    recordTypeMap = new Map<Id, String>();
    for (RecordTypeInfo rt : infos) {
        if ((rt.getName() == 'Student' || rt.getName() == 'Employer' || rt.getName() == 'Partners') && rt.getName().trim() != '') {
            recordTypeMap.put(rt.getRecordTypeId(), rt.getName());
        }
    }
    return recordTypeMap;
}

My component Markup:

<aura:attribute name="recordTypeMap" type="Map"/>
<aura:attribute name="recordTypeId" type="String"/>
<aura:attribute name="retrievedTypes" type="Boolean"/>
<!-- Record Type Selection -->
    <aura:if isTrue="{!equals(v.currentSteps, 'step1')}">
        <aura:renderIf isTrue="{!v.retrievedTypes}">
            <fieldset class="slds-form-element">
                <lightning:radioGroup aura:id="selectedRecordType"
                                      class="radioGroup slds-text-heading_small"
                                      name="radioGroup"
                                      options="{!v.recordTypeMap}"
                                      value="{!v.newStudent.RecordTypeId}"
                                      variant="label-hidden" />
            </fieldset>
        </aura:renderIf>
    </aura:if>

My JS Helper method:

getRecordTypes : function(component){
    var action = component.get("c.GetAvailableContactRecordTypes");
    action.setCallback(this, function(response) {
        var state = response.getState();
        if (state === "SUCCESS") {
            var recordTypeMap = [];
            var recordTypes = response.getReturnValue();
            for(var key in recordTypes){
                recordTypeMap.push({label:recordTypes[key], value:key});
            }
            component.set("v.recordTypeMap", recordTypeMap);
            component.set("v.retrievedTypes",true);
        }
    });
    $A.enqueueAction(action);
}

And in my controller method I'll do something like this

    var rtMap = component.get("v.recordTypeMap");
    var recordTypeId = component.get("v.newStudent.RecordTypeId");

    console.log(rtMap);
    console.log(recordTypeId);

    var rtName = // Magic should go here 

    if (rtName === 'Partners') {
        // Partners flow
    }
    if (rtName === 'Employers') {
    // Employers flow
    }
    if (rtName === 'Students') {
    // Students flow
    }

    rtMap.forEach(function (arrayItem) {
        var x = arrayItem.label;
        console.log(x);
    });

What I want to do is to get the label of the selected recordtype or object in this case, I can't find the proper way to do this.

If it worth to mention, this lightning component is inside a VFP via Lightning Out.

Best Answer

Once you have the RecordTypeId, you can find its label:

var rtMap = component.get("v.recordTypeMap");
var recordTypeId = component.get("v.newStudent.RecordTypeId");
var rtOption = rtMap.find(option => recordTypeId === option.value) || { label: null };
var rtName = rtOption.label;
// You now have the record type name (null if not found)

Where Array.prototype.find accepts a function, and returns the first value that returns true, and the || operator provides a default value if nothing was found.

Related Topic