3
public class AsyncExecutionExample implements Queueable{
    public void execute(QueueableContext context){
        Account a = new Account(Name='Acme',Phone='(415) 555-1212');
        insert a;
    }
}

How to execute the above class in Developer console?

2 Answers 2

5

Make sure that your class is implementing Queueable interface.

Then you can execute it by below code. Consider AsyncExecutionExample is the name of class which implements Queueable interface.

ID jobID = System.enqueueJob(new AsyncExecutionExample());

For more information you can refer below documentation: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_queueing_jobs.htm

In order to check status of job you can use the jobId returned and make below use of below query:

AsyncApexJob jobInfo = [SELECT Status,NumberOfErrors FROM AsyncApexJob WHERE Id=:jobID];

Now jobInfo will be able to tell you status of the job initiated.

10
  • I have Executed in Developer console,but No Account record is inserting,only Job Id and status is created
    – user28921
    Commented Mar 29, 2016 at 9:07
  • 1
    Elavarsan, what is the status that you got after querying with jobID. If the status is completed then only you can confirm that job has been executed. Commented Mar 29, 2016 at 9:08
  • status is completed
    – user28921
    Commented Mar 29, 2016 at 9:09
  • No Account record is inserting with name Acme
    – user28921
    Commented Mar 29, 2016 at 9:10
  • 2
    Above code is perfectly fine. You would be able to insert account. I tried this in dev org and faced no issues. Account got created. Just make sure that you are executing below code. ID jobID = System.enqueueJob(new AsyncExecutionExample()); This should kick off the job. Please check job status by making another query in developer console. Once its status found to be completed then only check whether account is created or not. Commented Mar 29, 2016 at 9:30
2

I'm going to take a stab at this one and say that there is no problem with your code and the account record is being inserted, but you aren't seeing it in the UI since it isn't a recently viewed record. Viewing the record after creating it should resolve your issue:

public class AsyncExecutionExample implements Queueable{
    public void execute(QueueableContext context){
        Account a = new Account(Name='Acme',Phone='(415) 555-1212');
        insert a;
        list<Account> alist = [SELECT Id FROM Account WHERE Id = :a.Id FOR VIEW];
    }
}
4
  • Thank You Martin,Now it is showing Account record is inserted in Account object,please explain what you done in the code
    – user28921
    Commented Mar 29, 2016 at 10:03
  • Excellent. Actually, the Account was being inserted before; if you had done a global search for "Acme", or filtered for the account in a list view, you would have been able to find it. The additional line of code I added just made it so that the record was counted as being recently viewed, so that it would show up in the Recently Viewed lists.
    – martin
    Commented Mar 29, 2016 at 10:18
  • No problem​​​​​​​​‌​‌​‌​‌​‌​‌​‌​‌​‌​
    – martin
    Commented Mar 29, 2016 at 10:25
  • @martin, That was an excellent guess on what OP's complain is. You are the man!
    – javanoob
    Commented Mar 29, 2016 at 16:24

You must log in to answer this question.