0

I have a requirement to invoked my code on a schedule and since I'm new I could not able to wrap my head around how to implement but here is what I have got so far please correct me if I'm doing anything wrong.

I have got so far the following:

  1. I have trigger on a custom object (employee__c)
  2. I have class (employeeClass) that do some manipulation of data on certain condition and update the custom object.
  3. Apex Schedule class

here is my trigger looks like:

trigger EmployeeTrigger on PM_Schedule__c (before insert, before update) 
{
    EmployeeClass emp = new EmployeeClass();      
    emp.EmployeeClass(Trigger.new); 
}

//EmployeeClass.cls

public void EmployeeClass(List<Employee__c> employee)
{ 
   for(Employee__c em : employee)
   {
      //do more work
   }
}

//Apex Schedule

public class EmployeeScheduled Implements Schedulable
{
  public void execute(SchedulableContext sc)
  {
     startEmployeeUpdates();
  }

  public void startEmployeeUpdates()
  {
     my question here, should i call the EmployeeClass from here?
     or 
     fire trigger from here?
  }
}

So my question is:

Should I call EmployeeClass from EmployeeScheduled ? How do I invoke the EmployeeClass?

2

If all you want is to run a job on a schedule, all you need is the schedulable class. Going into Setup > Develop > Apex Classes you will see your classes, and you can click the button on the top right of the table (Schedule Apex) to actually schedule the class to do something for you.

However, if you want to do this in Apex, you can do so.

And if you want it to do something when the object is updated/inserted, you can keep what you have done so far, but instead put the code that will do the things you want in the class, and then call it in the scheduled class and in the Trigger.

Edit: Updated with code for exemplification.

// the class that will be called by the trigger and the scheduled class
public class MyClass
{
    public List<Employee__c> myMethod(List<Employee__c> empList)
    {
        // do your things here
        for (Employee__c e : empList)
        {
            // do what you need to do with your employees
        }

        // return the list to whichever thing called it
        // (the trigger or the scheduled class)
        return empList;
    } 
}

// the trigger 
trigger MyTrigger on Employee__c (before update, before insert)
{
    // remember you can call different methods of your class, depending
    // on the situation and/or what you want to do
    if (Trigger.isUpdate)
    {
        // do what you must if the trigger is called
        // in an update action by the system or the
        // user

        // example
        List<Employee__c> employees = Trigger.new;

        MyClass mc = new MyClass();
        List<Employee__c> updatedList = mc.myMethod(employees);

        // commit the changes to the db
        update updatedList;
    }
    if (Trigger.isInsert)
    {
        // do what you must if the trigger is called
        // in an insert action by the system or the
        // user
    }
    // you can also use
    if (Trigger.isInsert || Trigger.isUpdate)
    {
        // if your trigger will execute the same code
        // in both situations
    }
}

// the class that will be scheduled
global class ScheduledJob implements Schedulable
{
    global void execute(SchedulableContext SC)
    {
        // query your objects
        // pass it to the class
        // update or insert what you need to

        List<Employee__c> employees = [SELECT Id, Name FROM Employee__c];

        MyClass mc = new MyClass();
        List<Employee__c> updatedList = mc.myMethod(employees);

        // commit the changes to the db
        update updatedList;
    }
}
  • so do I have to repeat the same code in EmployeeScheduled class as I have in EmployeeClass ? – Nick Kahn Jun 18 '15 at 1:25
  • I'm confused with your last statement you can keep what you have done so far, but instead put the code that will do the things you want in the class would you please give me some sample lines of code how would you do that? based on the code I have provided.. which class are you referring to? – Nick Kahn Jun 18 '15 at 1:26
  • In the scheduled class you can make a query, and pass it as a list to a new instance of the EmployeeClass, in the same way you are passing Trigger.new in the trigger right now. – Renato Oliveira Jun 18 '15 at 1:27
  • I've updated my answer with some code with comments. See if that helps. – Renato Oliveira Jun 18 '15 at 1:53

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.