[Salesforce]@RemoteAction

I will share about Salesforce’s @RemoteAction.

Features

  • You can execute Apex class processes from JavaScript.
  • It has the same functionality as actionFunction, but you can use the return value of the process in JavaScript.

For example, you can execute a process to get accounts and use the list of accounts retrieved.

Sample code

  • Apex class
 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 pages

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

[Salesforce]TestMethod로 정의된 메서드는 웹 서비스 콜아웃을 지원하지 않습니다.

TestMethod로 정의된 메서드는 웹 서비스 콜아웃을 지원하지 않습니다.

오류가 발생한 예

@isTest
private class PostTest {
    @isTest
    static void test() {

        Test.startTest();
        Service.execute();
        Test.stopTest();
    }
}

public with sharing class Service {
  @future(callout=true)
  public static void execute() {
    ServiceC sc = new ServiceC();
    sc.post();
   }

}

public with sharing class ServiceC {

  private HttpResponse post() {
    HttpRequest request = new HttpRequest();

    (생략)

    HttpResponse response = new Http().send(request);

    return response;
  }

}

오류 해결 예

@isTest
private class PostTest {
    @isTest
    static void test() {

        Test.startTest();
        Test.setMock(HttpCalloutMock.class, new ServiceHttpCalloutMock());
        Service.execute();
        Test.stopTest();
    }
}

@isTest
public class ServiceHttpCalloutMock implements HttpCalloutMock {
  public HTTPResponse respond(HTTPRequest req) {
    HttpResponse res = new HttpResponse();
    res.setStatusCode(200);
    res.setBody('test');
    return res;
  }
}

[Salesforce] Methods defined as TestMethod do not support Web service callouts.

Methods defined as TestMethod do not support Web service callouts.

Example of an error that occurred

@isTest
private class PostTest {
    @isTest
    static void test() {

        Test.startTest();
        Service.execute();
        Test.stopTest();
    }
}

public with sharing class Service {
  @future(callout=true)
  public static void execute() {
    ServiceC sc = new ServiceC();
    sc.post();
   }

}

public with sharing class ServiceC {

  private HttpResponse post() {
    HttpRequest request = new HttpRequest();

    (omission)

    HttpResponse response = new Http().send(request);

    return response;
  }

}

Example of resolving errors

@isTest
private class PostTest {
    @isTest
    static void test() {

        Test.startTest();
        Test.setMock(HttpCalloutMock.class, new ServiceHttpCalloutMock());
        Service.execute();
        Test.stopTest();
    }
}

@isTest
public class ServiceHttpCalloutMock implements HttpCalloutMock {
  public HTTPResponse respond(HTTPRequest req) {
    HttpResponse res = new HttpResponse();
    res.setStatusCode(200);
    res.setBody('test');
    return res;
  }
}

[Salesforce]Apex ジョブのスケジュール

Apex ジョブのスケジュール

同時に使用できる有効なジョブまたはスケジュール済みジョブの数は 100 件です。

  1. [設定] から、[クイック検索] ボックスに「Apex クラス」と入力し、[Apex クラス] を選択して、[Apex をスケジュール] をクリックします。
  2. スケジュールを設定するクラスの名前を指定します。
  3. [Schedule Builder] または [Cron Expression] を選択して Apex ジョブをスケジュールします。
  4. スケジュールビルダーを選択した場合:
    1. Apex クラスを実行する頻度を指定します。
      • [毎週] の場合、ジョブを実行する曜日を 1 つ以上指定します ([月曜] と [水曜] など)。
      • [毎月] の場合、ジョブを実行する日付または曜日 (毎月第 2 土曜など) を指定します。Apex のスケジュール済みクラスの開始日と終了日を指定します。日付を 1 つ指定した場合は、ジョブは 1 回のみ実行されます。
      • 希望開始時刻を指定します。ジョブが開始する正確な時刻は、サービスの可用性によって異なります。
  5. [Cron Expression] を選択した場合は、cron 式を使用して時間を指定し、設定した時間にクラスを実行するようにスケジュールします。
  6. [保存] をクリックします。

[Technical terms]リアクティブプロパティ

リアクティブプロパティとは、値が変更されたときに、変更を検知して自動的にUIを更新するような、プロパティのことです。具体的には、変更を監視し、変更があった場合に、自動的に関連するUIを更新するための仕組みを指します。

例:

  • Vue.jsでは、データが変更されると、自動的にHTMLに反映されます。これは、Vue.jsのリアクティブシステムによるものです。

[SalesforceDeveloperCertification]認定 Platform デベロッパー資格の更新 (Winter ’25)

public class MyIterable implements Iterable<String> {
    private List<String> strings {get; set;}

    public MyIterable(List<String> strings) {
        this.strings = strings;
    }

    public Iterator<String> iterator() {
       return this.strings.iterator();
    }
}

@IsTest
private class MyIterableTest {
    
    @IsTest static void testIterableForLoop() {
        
            List<String> strings = new List<String>{'Hello','World'};
                
            MyIterable mi = new MyIterable(strings);

            Test.startTest();
            
            for (String str : mi) {
                System.debug(str);
            }

            Test.stopTest();
    }
}

[Salesforce]Failed to run tests synchronously.: Your query request was running for too long.

테스트 클래스 실행 시 “Failed to run tests synchronously.: Your query request was running for too long.” 오류 대응

Summary
Test classes failing with QUERY_TIMEOUT Failed to run tests synchronously when running in synchronous mode

Login to the org which has quite number of managed package apex classes.
Run the Apex Test Class from Developer Console in Sync Mode
You may notice below exception when running the apex test class:
Error Message:

“QUERY_TIMEOUT Failed to run tests synchronously: Your query request was running for too long”

Workaround
As a workaround, please try to run the test class in asynchronous mode.

[Salesforce]Failed to run tests synchronously.: Your query request was running for too long.

Handling the error “Failed to run tests synchronously.: Your query request was running for too long.” when running a test class

Summary
Test classes failing with QUERY_TIMEOUT Failed to run tests synchronously when running in synchronous mode

Login to the org which has quite number of managed package apex classes.
Run the Apex Test Class from Developer Console in Sync Mode
You may notice below exception when running the apex test class:
Error Message:

“QUERY_TIMEOUT Failed to run tests synchronously: Your query request was running for too long”

Workaround
As a workaround, please try to run the test class in asynchronous mode.