We have service contracts in our system which have an end date. Every month the Apex code should check if any Service Contracts are about to expire this month, and create an opportunity for that Service Contract.

I am trying to schedule an apex class but it stays on the status Queued when I check 'Apex Jobs' in setup. I wrote the below Apex code and scheduled it through the UI via setup > Apex Classes > Schedule Apex.

 global class serviceContract_Scheduled implements Schedulable
            global void execute(SchedulableContext sc)

            public void createOpportunityfromServiceContract()

                list<ServiceContract> listServiceContracts = [SELECT ID,Name, AccountId, EndDate FROM ServiceContract WHERE EndDate = This_month];

                for(ServiceContract serv : listServiceContracts)
                  Opportunity opp = new Opportunity();
                  opp.Name = serv.Name;
                  opp.AccountId = serv.AccountId;
                  opp.Type = 'Annual Maintenance';
                  opp.StageName = 'Closed Won';
                  opp.CloseDate = serv.EndDate;

                update listServiceContracts;


Is it the code which is incorrect or am I scheduling it wrong? Any advise would be appreciated!

Based on Mr.Frodo's answer, it seems that by design it stays on 'Queued' status. However, the opportunities are not being created. When I type the query in the developer console, it does find 2 contracts which match the criteria.


Look at this:

Why do I see Apex jobs in Queued status on the "Apex Jobs" page?

By design, scheduled Apex jobs will always stays in the Queued status until the job has no NextFireTime, then it gets marked Completed, or if the job is aborted, then the AsyncApexJob row gets marked Aborted.


In provided code you are not inserting opportunity.

  • The issue with this is that the class does not seem to fire as well, no opportunities are created with this code. I'll update the question to better reflect that. – Johan de Wild Jan 27 '17 at 11:27
  • @JohandeWild You are not inserting opportunity in your code. – Mr.Frodo Jan 27 '17 at 11:40
  • 1
    facepalms Thanks, that was the issue... Can't believe I missed that. Thanks! – Johan de Wild Jan 27 '17 at 11:44
  • 1
    @JohandeWild I can understand the frustration :P :) – Mr.Frodo Jan 27 '17 at 11:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.