[Salesforce]@RemoteAction

Salesforce의 @RemoteAction에 대해 공유합니다.

특징

· JavaScript에서 Apex 클래스의 처리를 실행할 수 있습니다.

· actionFunction과 같은 기능이지만, 이것은 JavaScript 내에서 처리의 반환 값을 사용할 수 있습니다.

예를 들어, 거래처 취득 처리를 실행해 취득한 거래처 리스트를 사용한 처리를 실시할 수 있습니다.

샘플 코드

· Apex 클래스

 global with sharing class SampleRemoteController {
    @RemoteAction
    global static String setString(String str) {
        return  '----- ' + str + ' -----';
    }
    
    @RemoteAction
    global static List<sObject> exequery(String str){
        return Database.query(str);
    }
}

· Visualforce 페이지

 <apex:page controller="SampleRemoteController">
    <apex:form >
        <!-- @RemoteActionSample:StringSet -->
        <apex:commandButton value=" String " onClick="return  setString('Yaho');" />
         
        <!-- @RemoteActionSample:SOQLGet -->
        <apex:commandButton value=" GetAcc " onClick="return getAcc();" />
        <hr/>
        <div id="output">Output</div>
    </apex:form>

    <script type="text/javascript">
        /*
         * @RemoteActionSample
         * StringSet
         */
        function setString(str) {
            {!$RemoteAction.SampleRemoteController.setString}(str, function(result, event){
                if(event.status) {
                    document.getElementById("output").innerHTML = result;
                }
            });
            return false;
        }
        
        /*
         * @RemoteActionSample
         * SOQLAccountGet
         */
        function getAcc() {
            var query = 'select Id,Name from Account limit 10';
            {!$RemoteAction.SampleRemoteController.exequery}(query, function(result, event){
                if(event.status) {
                    var names = '';
                    for (var i = 0; i < result.length; i++) {
                        names = names + result[i].Name + ' | ';
                    }
                    document.getElementById("output").innerHTML = names;
                }
            });
            return false;
        }
    </script>
</apex:page>   

投稿者: kinkun

保有資格 Salesforce Certified Platform App Builder T Salesforce Certified Platform Developer I Salesforce Certified Platform Developer II Salesforce Certified Administrator

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です