Javascript Record-Type – How to Add a String Value to String Array in Lightning Component

I am fetching some recordtype values as a string array. I want to add the string — None– to the array so its obvious nothing is selected by default. How do I achieve this.

I tried the following but this returns a number and all my recordtypelabels are lost.
Component

 <aura:handler name="init" value="{!this}" action="{!c.fetchListOfRecordTypes}"/>
    <aura:attribute name="lstOfRecordType" type="String[]"/>
    <aura:attribute name="genericObject" type="String" />

      <aura:attribute name="selectedValue" type="String" default="Hoofd"/>


    <!-- Model Box Start -->    
    <div role="dialog" tabindex="-1" aria-labelledby="header43" >
            <div class="slds-grid slds-wrap">
                  Select a Record Type<br/>
  <ui:inputSelect aura:id="selectid" required="true">
                                <aura:iteration items="{!v.lstOfRecordType}" var="rectype">                            
                                    <ui:inputSelectOption text="{!rectype}" label="{!rectype}"  />
                                </aura:iteration>
                            </ui:inputSelect>
                <lightning:button class="slds-button slds-button-#brand" onclick="{!c.createRecord}">Next</lightning:button>
            </div>                   
    </div>

Javascript

 fetchListOfRecordTypes : function(component, event, helper) {
    var genericObject = component.get("v.genericObject");
    var action = component.get("c.fetchRecordTypeValues");
    action.setParams({ "objectStr" : genericObject });
    action.setCallback(this, function(response) {
        var state = response.getState();
        if (state === "SUCCESS") {
            // Fetch recordtypes
            action.setCallback(this, function(response) {
                component.set("v.lstOfRecordType", response.getReturnValue().push("--None--"));
            });
            $A.enqueueAction(action);
        } 
        else {
            console.log(state);
        }
    });
    $A.enqueueAction(action);
}

Apex

public class recordtypeSelector {
    public static Map<Id, String> recordtypemap {get;set;}

   @AuraEnabled        
    public static List<String> fetchRecordTypeValues(string objectStr){
        SObjectType sObjType = ((SObject) Type.forName(objectStr).newInstance()).getSObjectType();
        List<Schema.RecordTypeInfo> recordtypes = sObjType.getDescribe().getRecordTypeInfos();    
        recordtypemap = new Map<Id, String>();
        for(RecordTypeInfo rt : recordtypes){
            if(rt.getName() != 'Master')
            recordtypemap.put(rt.getRecordTypeId(), rt.getName());
        }        
        return recordtypemap.values();
    }
    @AuraEnabled
    public static Id getRecTypeId(String recordTypeLabel, string objectStr){
         system.debug('recordTypeLabel: '+recordTypeLabel);
           system.debug('objectStr: '+objectStr);
       Id recid = Schema.getGlobalDescribe().get(objectStr).getDescribe().getRecordTypeInfosByName().get(recordTypeLabel).getRecordTypeId();
        system.debug('recid: '+recid);
        return recid;
    }      
}

Best Answer

You should first get the Apex Method in the Javascript and set the callback function as described below:

var action = component.get("ApexMethodName");  //get the Apex method in local Js variable
    // handle the callback from the server
    action.setCallback(this, function(response) {

        // get the status of the server call in var "state"
        var state = response.getState(); 

        // if the state is success then do the processing
        if (state == "SUCCESS") {

           //get the returned value in local js var
            var returnedArrayOfString = response.getReturnValue();

           //use Js unshift method which will add the value passed as parameter at the top of the array
            returnedArrayOfString.unshift('--None--');

          //set the updated Js array to the attribute defined in the component
          component.set('v.lstOfRecordType', returnedArrayOfString);

        } else {
         //Handle apex error here 
        }
    });

    // enqueue the Apex method using the below syntax which will call the server apex method asynchnously.
    $A.enqueueAction(action);
Related Topic