function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sylvie SerpletSylvie Serplet 

Chatter Post for staff anniversary

Hi,
I have a requirement to automatically post on Chatter a message to wish staff members a happy anniversary.
I have created 2 customs fields on Contact. The first one (Birthday__c) is a formula field (date - type) : DATE(YEAR (TODAY()), MONTH(Birthdate), DAY(Birthdate)). The second one (Birthday_is_today__c) is a formula field (checkbox - type): IF(Birthday__c = TODAY(),true, false). The second field update properly at runtime.
I have also created a Process Builder on Contact (when a record is created or edited), my criterias are Birthday_is_today__c = True and Contact Record Type = Staff, my action is Post to Chatter with a message Happy Birthday @staff!
The post is not created, it seams that something is missing between the 2 processes. The field update is not captured by the process builder to start the process.  What am I missing?
Thank you for your help.
Best Answer chosen by Sylvie Serplet
Sylvie SerpletSylvie Serplet
Please find below the solution I found to make it works. Hope that will help some of you.

First I have to change the data type of my custom field Birthday is today from formula(checkbox) to checkbox with a default value to Unchecked.

I have also created 2 scheduled Apex classes:

One to check if Birthday = today then tick the checkbox Birthday is today. This is scheduled everyday at 8.00am and it triggers the process builder.
Below my code and the test.
global class UpdateBirthday implements Schedulable { 

   global void execute(SchedulableContext ct) { 

        List<Contact> contactlist = [Select Id, Birthday_is_today__c, Birthday__c From Contact Where Birthday__c = Today and RecordType.Name = 'Head Office Staff'];
        
        for (Contact c :contactlist){
        c.birthday_is_today__c = true;    
        }
       update contactlist; 
      }   
   }

@isTest
private class UpdateBirthdayTest {

    static testMethod void testschedule() {

            Test.startTest();                 
                        
        UpdateBirthday p = new UpdateBirthday();
        String sch = '0 00 8 * * ?';
        system.schedule('Test', sch, p);
        
                   Test.stopTest();
    }
}


The second one untick the checkbox the next day at 1.00am to allow the process to start again. Below my code and the test.
global class UpdateDelBirthday implements Schedulable { 

   global void execute(SchedulableContext ct) { 

        List<Contact> contactlist = [Select Id, Birthday_is_today__c, Birthday__c From Contact Where Birthday_is_today__c = true and RecordType.Name = 'Head Office Staff'];
        
        for (Contact c :contactlist){
        c.birthday_is_today__c = false;    
        }
       update contactlist; 
      }   
   }

@isTest
private class UpdateDelBirthdayTest {

    static testMethod void testschedule() {

            Test.startTest();                 
                        
        UpdateDelBirthday b = new UpdateDelBirthday();
        String sc = '0 00 8 * * ?';
        system.schedule('Test', sc, b);
        
                   Test.stopTest();
    }
}

All Answers

AshlekhAshlekh
Hi,

I think you  can do this through the schedule a class which run daily and check daily that today's some have special day or not, than post on chatter through te code.

-Thanks
Ashlekh Gera
Sylvie Serplet 1Sylvie Serplet 1
Thank you for your answer. Could you provide code for the APEX class?
Sylvie SerpletSylvie Serplet
Please find below the solution I found to make it works. Hope that will help some of you.

First I have to change the data type of my custom field Birthday is today from formula(checkbox) to checkbox with a default value to Unchecked.

I have also created 2 scheduled Apex classes:

One to check if Birthday = today then tick the checkbox Birthday is today. This is scheduled everyday at 8.00am and it triggers the process builder.
Below my code and the test.
global class UpdateBirthday implements Schedulable { 

   global void execute(SchedulableContext ct) { 

        List<Contact> contactlist = [Select Id, Birthday_is_today__c, Birthday__c From Contact Where Birthday__c = Today and RecordType.Name = 'Head Office Staff'];
        
        for (Contact c :contactlist){
        c.birthday_is_today__c = true;    
        }
       update contactlist; 
      }   
   }

@isTest
private class UpdateBirthdayTest {

    static testMethod void testschedule() {

            Test.startTest();                 
                        
        UpdateBirthday p = new UpdateBirthday();
        String sch = '0 00 8 * * ?';
        system.schedule('Test', sch, p);
        
                   Test.stopTest();
    }
}


The second one untick the checkbox the next day at 1.00am to allow the process to start again. Below my code and the test.
global class UpdateDelBirthday implements Schedulable { 

   global void execute(SchedulableContext ct) { 

        List<Contact> contactlist = [Select Id, Birthday_is_today__c, Birthday__c From Contact Where Birthday_is_today__c = true and RecordType.Name = 'Head Office Staff'];
        
        for (Contact c :contactlist){
        c.birthday_is_today__c = false;    
        }
       update contactlist; 
      }   
   }

@isTest
private class UpdateDelBirthdayTest {

    static testMethod void testschedule() {

            Test.startTest();                 
                        
        UpdateDelBirthday b = new UpdateDelBirthday();
        String sc = '0 00 8 * * ?';
        system.schedule('Test', sc, b);
        
                   Test.stopTest();
    }
}
This was selected as the best answer