Sandbox_Public_Group

kinkun's blog
Sandbox_Public_Group

wait(10000);//Wait
//Wait
public static void wait(Integer millisec) {
if(millisec == null || millisec < 0) {
millisec = 0;
}
Long startTime = DateTime.now().getTime();
Long finishTime = DateTime.now().getTime();
while ((finishTime - startTime) < millisec) {
//sleep for parameter x millisecs
finishTime = DateTime.now().getTime();
}
// System.debug(‘>>> Done from ‘ + startTime + ‘ to ‘ + finishTime);
}
wait(10000);//Wait
//Wait
public static void wait(Integer millisec) {
if(millisec == null || millisec < 0) {
millisec = 0;
}
Long startTime = DateTime.now().getTime();
Long finishTime = DateTime.now().getTime();
while ((finishTime - startTime) < millisec) {
//sleep for parameter x millisecs
finishTime = DateTime.now().getTime();
}
// System.debug(‘>>> Done from ‘ + startTime + ‘ to ‘ + finishTime);
}
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>
I will share about Salesforce’s @RemoteAction.
Features
For example, you can execute a process to get accounts and use the list of accounts retrieved.
Sample code
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>
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;
}
}
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;
}
}
Apex ジョブのスケジュール
同時に使用できる有効なジョブまたはスケジュール済みジョブの数は 100 件です。
リアクティブプロパティとは、値が変更されたときに、変更を検知して自動的にUIを更新するような、プロパティのことです。具体的には、変更を監視し、変更があった場合に、自動的に関連するUIを更新するための仕組みを指します。
例:


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