LoginSignup
9
13

More than 5 years have passed since last update.

任意のオブジェクトをApexから取得する

Posted at

任意のオブジェクトをApexから取得する

チュートリアルなどでは、ApexからsObjectを取得するサンプルが多いのですが、自分で作成したクラスをApexから取得する際にはまったので書きます。

Apexで取得した値を表示するコンポーネント。

AdventCalendarCmp.cmp
<aura:component controller="TLP.AdventCal">
    <aura:attribute name="childList" type="List"/>
    <div>
        <aura:iteration var="item" items="{!v.childList}">
            <div>{!item.Name}</div>
            <div>{!item.parentName}</div>
        </aura:iteration>
    </div>
</aura:component>

Apexを呼び出すHelperメソッド

AdventCalendarCmpHelper.js
    getChilds : function(component){
        var act = component.get('c.getAdvanceCalDto');

        act.setCallback(this,function(a){
            component.set("v.childList",a.getReturnValue());
        });
        $A.enqueueAction(act);
    }

Apexのクラスを作成する際に、値を取得するメソッドに、AuraEnabledaアノテーションを付けます。
さらに・・・取得するオブジェクトのプロパティにAuraEnabledアノテーションを付けます。
つまりはAuraEnabledアノテーションをプロパティにもつけましょうということでした。
これで任意のクラス形式でをApexから値を取得することができます。

AdventCal.cls

    @AuraEnabled
    public static List<AdvanceCalDto> getAdvanceCalDto(){
        List<AdvanceCalDto> retList = new List<AdvanceCalDto>();
        for(TLP__ChildObj__c obj :[SELECT Name ,Parent__r.Name FROM TLP__ChildObj__c]){
            retList.add(new AdvanceCalDto(obj.Name,obj.Parent__r.Name));
        }
        return retList;
    }

    public class AdvanceCalDto{
        AdvanceCalDto(String name,String parentName){
            this.name = name;
            this.parentName = parentName;
        }
        @AuraEnabled   //こいつがポイント
        String parentId{get;set;}
        @AuraEnabled   //こいつがポイント
        String name{get;set;}
        @AuraEnabled   //こいつがポイント
        String parentName{get;set;}
    }
9
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
13