Pages

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    

17 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
  9. This comment has been removed by the author.

    ReplyDelete
  10. Overall this framework relies upon understudy obligations not thinking about the less lucky families. Because of these components, the hole between center pay workers and the helpless who go to school has fundamentally expanded.토토사이트

    ReplyDelete
  11. The climate being referred to should incite workers to excitedly take part in choices basic to the training system. This can be accomplished if pioneers make a culture helpful for open correspondence, where adherents can take an interest by communicating their thoughts unafraid of scorn.留学生代写

    ReplyDelete
  12. Strong schooling with positive outcomes as far as execution is a method of "getting your ticket punched." as such, a few managers will not consider up-and-comers except if they have a specific degree of training or a particular course of review. coding for kids

    ReplyDelete
  13. In the new period of instruction today there are parcel many changes in the worldwide training framework. Attributable to a heap of changes, the new age schooling framework embraces abundant of chances to investigate and find. Best School in Electronic City

    ReplyDelete
  14. Explore Ahmad Shahjahan's latest collection of winter clothes for men. We provide a classy selection of unstitched fabric alternatives to fit your taste, along with high-quality products at reasonable costs. This winter, stay stylish and warm.

    ReplyDelete