I need to create a post with a message of happy birthday when it's the birthday of a user.

I have seen the code to create the post but I don't know how when to run it.

Should I write a trigger?

  • added from comment:

This is my code:

Global class HappyBirthday implements Schedulable{

global void execute(SchedulableContext sc) {

 List<user> lst=[Select id,name from user where date_of_birth__c =today];
   for(User u:lst){
FeedItem post = new FeedItem();

post.ParentId = u.id;

 post.Body = 'Happy birthday '+u.name;

   insert post;

} }

I don’t want use the button SCHEDULE CLASS. To run the class above every day where i should insert this code?

HappyBirthday happy = new HappyBirthday();

String sch = '0 0 8 * * ?';

   system.schedule('HappyJob', sch, Happy);
  • 3
    Can you post the link that shows how to write the code so that we can guide you from there based on what the post suggests!! – Rao Apr 26 '13 at 20:48
  • Relevant birthday formula – Matt and Neil Apr 29 '13 at 8:11

Instead of doing this with a trigger, you should just write a schedulable batch job that runs once every day. (Unfortunately workflow doesn't support chatter posts (yet))

Where your batch job's query basically is, get all the users which birthday equals today, and make a Chatter Post to their feed.

  1. Using Batch Apex
  2. Apex Scheduler
  3. Manually Schedule Apex

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.