2

I am still a beginner and I trying to create a trigger that will create a task on a weekly basis (task once a week) until a field in the opportunity is label complete. This Trigger and Class work, as well as 100% Code Coverage, but I know this isn't how you create a class that can be scheduable. Where do I go from there?

Thank You!

//Trigger:

trigger Opportunitytask on Opportunity (after insert, after update){
  List<Task> carry=new List<Task>();
  for(Opportunity opp:trigger.new)
  {
    if(opp.PM__c!=null && opp.PM_close_Out_Checklist__c!='complete')
     {
        task t=new task();
        t.whatid=opp.id;
        t.ownerid=opp.PM__c;
        t.subject='Email BDM Weekly Update';
        t.priority='Normal';
        t.status='Not Started';
        carry.add(t);
     }

     insert carry;

  }
}

//Test Class:

@isTest
public class TestOpportunitytask {

static testMethod void insertTask() {

        Test.StartTest();
        Account acc = new account();
        acc.Name = 'Test Account';
        acc.Industry = 'Healthcare';

        insert acc;

        Opportunity opp = new Opportunity();
         opp.Name = 'Test Opportunity'; 
        opp.CloseDate = System.today();
        opp.StageName = 'Proposal';
        opp.Type = 'Repeat Business - Wrkforce HC';
        opp.AccountId = acc.Id;
        opp.PM__c=null;
        insert opp;

        opp.PM__c='005U0000004Y1wf';

        update opp;

        Task t=new Task();

        t.whatid='006U000000QS6x5';
        t.ownerid='005U0000004Y1wf';
        t.Subject='Email BDM Weekly Update';
        t.priority='Normal';
        t.status='Not Started';

        insert t;

        System.assertEquals('006U000000QS6x5',t.whatid);

              Test.StopTest();
     }
    }
  • 2
    You have couple of bad coding practices: 1- never hard code IDs 2- try to avoid DML inside for loops especially in triggers. – raym0nd Mar 24 '16 at 21:07
2

Your scheduled class should be something like this:

(The below code is not guaranteed to run, I wrote that pretty fast)

Note: you can play with the params of Schedule() method to set the running of the scheduled job.

global class ProcessOpportunityTask implements Database.Batchable<sObject>, Schedulable  {


    global Database.QueryLocator start(Database.BatchableContext BC){
        return Database.getQueryLocator([SELECT Id , PM__c
                                         FROM Opportunity  
                                         PM__c!=null 
                                         && 
                                         PM_close_Out_Checklist__c <> 'complete' ]);
    }

    global static void Schedule(string sch) {
        ProcessOpportunityTask a = new ProcessOpportunityTask();
        if (String.isBlank(sch))
            sch = '0 0 01 * * ?'; // defaults to 1am every day
        System.schedule('ProcessOpportunityTask', sch, a);
    }   

    global void execute(SchedulableContext SC)  {
       ProcessOpportunityTask n = new ProcessOpportunityTask();
       Database.executeBatch(n,1);
    } 

    global void execute(Database.BatchableContext BC, List<sObject> scope) {

         List<Task> carry=new List<Task>();
        for(sobject s : scope) {
            Opportunity opp = (Opportunity)s;

            task t=new task();
            t.whatid=opp.id;
            t.ownerid=opp.PM__c;
            t.subject='Email BDM Weekly Update';
            t.priority='Normal';
            t.status='Not Started';
            carry.add(t);
        }      
        Insert carry;
    }

    global void finish(Database.BatchableContext BC) {

    }


}
  • (1) why scope size = 1? (2) Missing declaration of carry; as an aside .... (3) global could be public and (4) Schedule method most useful when called by Apex. If class is scheduled from UI, then not required – cropredy Mar 24 '16 at 23:06
  • 1- just a sample code, he can set the scope to any size 2- carry is declared inside the execute function 3- you are right 4- true – raym0nd Mar 29 '16 at 20:10

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.