Last active
August 29, 2015 14:09
-
-
Save tyoshikawa1106/aadcae086d8dfc94c150 to your computer and use it in GitHub Desktop.
Queueable インターフェースの実装サンプルです。(テストクラスでエラーでてます。。)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<apex:page controller="QueueableDemoController" showHeader="true" sidebar="false"> | |
<apex:form id="form"> | |
<apex:pageBlock title="Queueable Demo"> | |
<apex:commandButton value="Execute!!" action="{!doExecute}" reRender="form" /> | |
| |
<apex:commandButton value="Refresh!!" action="{!doRefresh}" reRender="form" /> | |
</apex:pageBlock> | |
<apex:pageBlock title="{!$ObjectType.AsyncApexJob.Label}" rendered="{!jobInfos.size > 0}"> | |
<apex:pageBlockTable value="{!jobInfos}" var="item"> | |
<apex:column headerValue="{!$ObjectType.AsyncApexJob.Fields.Id.Label}"> | |
<apex:outputField value="{!item.Id}" /> | |
</apex:column> | |
<apex:column headerValue="{!$ObjectType.AsyncApexJob.Fields.Status.Label}"> | |
<apex:outputField value="{!item.Status}" /> | |
</apex:column> | |
<apex:column headerValue="{!$ObjectType.AsyncApexJob.Fields.NumberOfErrors.Label}"> | |
<apex:outputField value="{!item.NumberOfErrors}" /> | |
</apex:column> | |
</apex:pageBlockTable> | |
</apex:pageBlock> | |
</apex:form> | |
</apex:page> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public with sharing class QueueableDemoController { | |
public List<AsyncApexJob> jobInfos {get; set;} | |
private Id jobID = null; | |
public QueueableDemoController() { | |
this.jobInfos = new List<AsyncApexJob>(); | |
} | |
public void doExecute() { | |
this.jobID = System.enqueueJob(new AsyncExecutionExample()); | |
if (String.isNotEmpty(this.jobID)) { | |
this.jobInfos = getAsyncApexJob(this.jobID); | |
} | |
} | |
public void doRefresh() { | |
if (String.isNotEmpty(this.jobID)) { | |
this.jobInfos = getAsyncApexJob(this.jobID); | |
} | |
} | |
private List<AsyncApexJob> getAsyncApexJob(Id jobID) { | |
return [SELECT Status,NumberOfErrors FROM AsyncApexJob WHERE Id=:jobID]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@isTest | |
private class QueueableDemoControllerTest { | |
static testMethod void QueueableDemoControllerTest() { | |
Test.startTest(); | |
QueueableDemoController cls = new QueueableDemoController(); | |
cls.doExecute(); | |
System.assertEquals(cls.jobInfos.isEmpty(), true); | |
Test.stopTest(); | |
cls.doRefresh(); | |
System.assertEquals(cls.jobInfos.isEmpty(), false); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public with sharing class AsyncExecutionExample implements Queueable { | |
public void execute(QueueableContext context) { | |
Account a = new Account(Name='Acme',Phone='(415) 555-1212'); | |
insert a; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@isTest | |
public class AsyncExecutionExampleTest { | |
static testmethod void test1() { | |
// startTest/stopTest block to force async processes | |
// to run in the test. | |
Test.startTest(); | |
ID jobID = System.enqueueJob(new AsyncExecutionExample()); | |
Test.stopTest(); | |
// Validate that the job has run | |
AsyncApexJob jobInfo = [SELECT Status,NumberOfErrors FROM AsyncApexJob WHERE Id=:jobID]; | |
System.assertEquals('Completed', jobInfo.Status); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment