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
mshroyermshroyer 

Set Date to Saturday and on trigger update reset to the next Saturday

We have a custom object used to set call schedules. When they set their contact frequency to Weekly we want to set the Next Call Date to the upcoming Saturday. Then when they make a call (Last Call Date is updated to Today) we want to push the Next Call Date out to the next Saturday.

 

But we don't want it to update the Next Call Date unless it's due this week. So if the Next Call Date is this Saturday, then if they make a call we want it to update, but if the Next Call Date has already been updated to next Saturday, then we wouldn't want it to update again if they call again this week. Only update Next Call Date if it is set to this coming Saturday.

 

What I need in a nutshell:

 

IF (CRM_Scheduled_Contact_Frequency__c  == 'Weekly') { Set Next Call Date to Saturday}

IF(CRM_Last_Call_Date__c == Today && CRM_Scheduled_Contact_Frequency__c == 'Weekly' && CRM_Next_Call_Date__c == This Saturday {

       Update Next Call Date to next Saturday}

 

If the Next Call Date != This Saturday then do nothing

 

 

I am not the most experienced in Apex and Day of Week code is rather confusing to me. Any help would be appreciated!

Avidev9Avidev9

Use this method to get the next Saturday.

 

public static DateTime getNextSaturday(DateTime initD) {
    while (initD.format('EEE') != 'Sat') {
        initD = initD.addDays(1);
    }
    return initD;
}