2

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;
    }      
}
  • can you show full code in both .cmp and JS, if possible Apex also because generally it will be list of objects of label and value but you are saying list of strings – salesforce-sas Aug 5 at 13:51
  • How about using array deconstruction operation? component.set("v.lstOfRecordType", [...response.getReturnValue() , "--None--"]); – Pranay Jaiswal Aug 5 at 13:58
  • @salesforce-sas I added the full code. – Thomas Aug 5 at 14:03
2

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);

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.