2

I'm trying to write a trigger that creates a custom object record on the first of every month.

So, essentially, I want the trigger to fire:

trigger CreateCustObjRecord on Object()
{
    Date current = Date.today(); 
    if(current.day = 1) 
    {
        Object custObj = new Object();
        Object.Name = 'New Custom Object';
        Object.CreationDate = current;
        insert custObj; 
    }
}

But I can't figure out how ot make this trigger fire. Can anyone point me in the right direction for this?

Thank you!

4

This is something that would be a better fit for scheduled apex - this will allow you to execute some code according to a date/time schedule. You can schedule the job from the UI or from code, via the developer console.

Take a look at the docs at:

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_scheduler.htm

There's a blog post at:

http://blog.deadlypenguin.com/blog/2012/05/26/scheduled-actions-in-salesforce-with-apex/

that includes an example of executing code on the first of every month.

4

Triggers only fire when a record is created, modified or deleted. You can't use triggers to fire at a certain time. You should create a scheduled job. See http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_scheduler.htm.

The code would look something like this (untested):

global class CreateCustObjRecord implements Schedulable{

    global void execute (SchedulableContext SC) {
        Object custObj = new Object();
        Object.Name = 'New Custom Object';
        Object.CreationDate = current;
        insert custObj;   
    }
}

You then schedule it by clicking "Schedule Apex" in the Setup->Develop->Apex Classes

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.