Friday, March 31, 2017

How to pass inner wrapper class to lightning components

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


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!!!



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    

11 comments:

  1. for the second approach i beleive this should be the declaration as wrapper is a separate class

    ReplyDelete
  2. the aura attrivute type will be RecordTypeWrapper[]

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Yes, Attribute type should be RecordTypeWrapper[]. i.e.

    ReplyDelete
  5. Thanks Ganesh and Abhik. I have updated the attribute type.

    ReplyDelete
  6. The directorate of secondary and higher education has given this assignment task to the nine grader in Bangladesh. class 9 assignment 2021 answer

    ReplyDelete
  7. or new understudies who enter schools or colleges 1 out of 3 are conceivable not make it to second year. This dropout out rate ruins public turn of events, on the grounds that numerous individuals don't get the full preparing they should be utilitarian in the public eye. Public improvement would be colossally cultivated, if more grown-ups get schooling, all together that they become practical in the public eye. education

    ReplyDelete
  8. Accordingly, in the event that you end up on a seat in a homeroom, benefit as much as possible from your time while you're in a decent situation to do as such. When you start down your vocation way, you'll be happy that your conventional schooling is behind you, since you'll have a lot of difficulties concerning hands on preparing and proceeding with instruction as you travel through your functioning years. kindergarten international school

    ReplyDelete