Creating a inner wrapper class is common practice to create new custom data type which can be used in VF page to display information. Lightning components can not access the inner class. The reason is that apex class cannot be marked as @AuraEnabled. You can only specify @AuraEnabled annotations to methods or member variables of class.
So now if you have to pass inner wrapper class to lightning then you can convert wrapper variable into JSON and return as string to lightning component.
Below is apex class performing this operation:
public with sharing class SkRecordTypeSelectionController {
@AuraEnabled
public static string findRecordTypes(string objName){
string returnString='';
string queryString='Select id, name from RecordType where sobjectType =: objName and IsActive=true';
List<sobject> recordList= Database.query(queryString);
List<RecordTypeWrapper> wrapperList=new List<RecordTypeWrapper>();
for(sobject sb : recordList) {
RecordTypeWrapper rw=new RecordTypeWrapper();
rw.recordTypeLabel=string.valueof(sb.get('name'));
rw.recordTypeId=string.valueof(sb.get('id'));
wrapperList.add(rw);
}
returnString= JSON.serialize(wrapperList);
return returnString;
}
public class RecordTypeWrapper{
public string recordTypeLabel{get;set;}
public string recordTypeId{get;set;}
}
}
In above code, you can see that we have created List of wrapper class and store all recordType information in that. After that we are converting it into JSON and returning it as string to lightning component.
Create an attribute to store this information:
<aura:attribute name="recordTypeList" type="Object[]"/>
In lightning component, you can convert back response into JSON by using below code:
var jsonObject=JSON.parse(response.getReturnValue());
Now you set attribute value by using below code:
component.set('v.recordTypeList',jsonObject);
RAISING AND HANDLING CUSTOM EVENTS IN sALESFORCE lIGHTNING
WHY TO USE DESIGN RESOURCE AND HOW TO ADD DYNAMIC OPTION TO DATASOURCE
FETCHING FILE FROM EXTERNAL/PUBLIC URL AND STORING IT IN SALESFORCE
So now if you have to pass inner wrapper class to lightning then you can convert wrapper variable into JSON and return as string to lightning component.
Below is apex class performing this operation:
public with sharing class SkRecordTypeSelectionController {
@AuraEnabled
public static string findRecordTypes(string objName){
string returnString='';
string queryString='Select id, name from RecordType where sobjectType =: objName and IsActive=true';
List<sobject> recordList= Database.query(queryString);
List<RecordTypeWrapper> wrapperList=new List<RecordTypeWrapper>();
for(sobject sb : recordList) {
RecordTypeWrapper rw=new RecordTypeWrapper();
rw.recordTypeLabel=string.valueof(sb.get('name'));
rw.recordTypeId=string.valueof(sb.get('id'));
wrapperList.add(rw);
}
returnString= JSON.serialize(wrapperList);
return returnString;
}
public class RecordTypeWrapper{
public string recordTypeLabel{get;set;}
public string recordTypeId{get;set;}
}
}
In above code, you can see that we have created List of wrapper class and store all recordType information in that. After that we are converting it into JSON and returning it as string to lightning component.
Create an attribute to store this information:
<aura:attribute name="recordTypeList" type="Object[]"/>
In lightning component, you can convert back response into JSON by using below code:
var jsonObject=JSON.parse(response.getReturnValue());
Now you set attribute value by using below code:
component.set('v.recordTypeList',jsonObject);
Another approach
You can create separate class as wrapper and then refer it in component controller as mentioned below:
public class RecordTypeWrapper{
@AuraEnabled
public string recordTypeLabel{get;set;}
@AuraEnabled
public string recordTypeId{get;set;}
}
Then you can create seperate apex class to refer this wrapper:
public with sharing class SkRecordTypeSelectionController {
@AuraEnabled
public static List<RecordTypeWrapper> findRecordTypes(string objName){
string queryString='Select id, name from RecordType where sobjectType =: objName and IsActive=true';
List<sobject> recordList= Database.query(queryString);
List<RecordTypeWrapper> wrapperList=new List<RecordTypeWrapper>();
for(sobject sb : recordList) {
RecordTypeWrapper rw=new RecordTypeWrapper();
rw.recordTypeLabel=string.valueof(sb.get('name'));
rw.recordTypeId=string.valueof(sb.get('id'));
wrapperList.add(rw);
}
return wrapperList;
}
}
Now you can create an attribute in lightning component as mentioned below:
<aura:attribute name="recs" type="RecordTypeWrapper[]"/>
For sample code example explaing above approach, please refer my next blog "Lightning Component for RecordType Selection for any Sobject"
Hope this will help!!!
For sample code example explaing above approach, please refer my next blog "Lightning Component for RecordType Selection for any Sobject"
Hope this will help!!!
More Blogs>>:
DYNAMICALLY CREATING AND DESTROYING LIGHTNING COMPONENTS
RAISING AND HANDLING CUSTOM EVENTS IN sALESFORCE lIGHTNING
WHY TO USE DESIGN RESOURCE AND HOW TO ADD DYNAMIC OPTION TO DATASOURCE
FETCHING FILE FROM EXTERNAL/PUBLIC URL AND STORING IT IN SALESFORCE