Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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!

share|improve this question

2 Answers 2

up vote 4 down vote accepted

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.

share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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