0

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.

1

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.

  • Awesome, you did it again, thank you very much sensei, you pointed me in the right direction, I had to edit instead of option.key -> option.value . I did research before posting and found the MDN documentation but didn't understand properly how to apply it, tho this solution is using Using ES2015 arrow function, if it not much to ask, would you explain me a little the syntax and I'll try to build the other option explained in MDN, thanks again Mr. Brian. – Adrian Apr 12 at 21:29
  • 1
    @Adrian MDN... There's several different "modes" you can use arrow functions, but basically, LHS is a list of parameters (use parentheses if not exactly 1 parameter), and the RHS's value is returned to the caller (as if you'd used return). If you need more than one statement, you need {} around it and a return statement. Arrow functions are really useful for sort, find, etc. – sfdcfox Apr 12 at 21:39
  • 1
    @Adrian Also, PS, Arrow Functions don't work in IE 11, if you need to support it still. I just prefer using it when I can because it's more efficient to type and use. – sfdcfox Apr 12 at 21:40
0

This is from the documentation for lightning:radioGroup:

You can check which values are selected by using cmp.find("mygroup").get("v.value"). To retrieve the values when the selection is changed, use the onchange event handler and call event.getParam("value").

So in your case, you will need to do something as:

// gets the record type id
var recordTypeId = component.find("selectedRecordType").get("v.value"); 

// find the corresponding name from the Map<Id, String> that you already have from Apex
var recordTypeMap = component.get("v.recordTypeMap"); 
var recordTypeName = recordTypeMap[recordTypeId];
  • Thank you Jayant, but this returns me the Id string, and if I use .get("v.label"); returns me undefined – Adrian Apr 12 at 21:12
  • Because you have the Id set as the value in the radio group. Because you already have the return value from the apex in recordTypeMap, you can use that to get the label for respective record type. I will update my answer. – Jayant Das Apr 12 at 21:16
  • Thanks Jayant for you time I really appreciate it, I tried what you said, but still returns undefiend. Anyway our friend @sfdcfox has given a solution, still thank you very much Jayant. – Adrian Apr 12 at 21:35
  • 1
    Glad that you got a working solution. The intent here is that once you have fetched the selected value from the radio group then you can get the corresponding label from from the map you already have. – Jayant Das Apr 12 at 21:45

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.