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
sfdcdudesfdcdude 

friday date

If user enters a Weekend (Saturday, or sunday) date in field A, I would like to see the fridays date in B field. And My field A datatype is datetime not Date. Please guide me or post your answer please. Thank you.
 
Best Answer chosen by sfdcdude
ravi soniravi soni
Hi pradeep,
create a formula for this requirment.
IF(WEEKDAY( DATEVALUE(Submit_deal_datetime__c) )=1,DATEVALUE(Submit_deal_datetime__c)-2 , 
  IF(WEEKDAY( DATEVALUE(Submit_deal_datetime__c) )=7,DATEVALUE(Submit_deal_datetime__c)-1 ,
  null))

If you want to make populate friday's date then you will have to create deal_report__c as a formula.
and if user select Submit_deal_datetime__c apart of weekend(Saturday, or sunday), it will return null.
let me know if it helps you and marking it as best answer.
Thank you
 

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Pradeep,

Can you elaborate on the scenario you want to implement and the issue you are facing to check further and respond.

Thanks.
Suraj Tripathi 47Suraj Tripathi 47

Hi Pradeep,

Please find the solution.

 global class PopulateData {
    public static void populateDay(){        
        List<Account> accountList=new List<Account>([Select Id,Name,FieldA__c,FieldB__c from Account limit 10]);
        for(Account ac:accountList){
            String day=ac.FieldA__c.format('EEE');
            if(day=='Sat' || day=='Sun'){
                ac.FieldB__c='Friday';
            }
        }
        update accountList;
    }

Please let me know it is working or not?

Please mark it as the Best Answer if it helps you.

Thank You

sfdcdudesfdcdude
Hi Anutej/Suraj,

To elaborate more, I am having a datetime field called (Submit_deal_datetime__c). When user selects a date (Lets say, weekend date (06/12/2021) or (06/13/2021) which falls under saturday and sunday respectively), then in another field called (submit deal_report__c) should populate firday'date( 06/11/2021). Please let me know if you are clear. Else will connect and solve this.

Thanks,
Pradeep. P
sfdcdudesfdcdude
Hi Anutej/Suraj, To elaborate more, I am having a datetime field called (Submit_deal_datetime__c). When user selects a date (Lets say, weekend date (06/12/2021) or (06/13/2021) which falls under saturday and sunday respectively), then in another field called (submit deal_report__c) should populate firday'date( 06/11/2021). Please let me know if you are clear. Else will connect and solve this. Thanks, Pradeep. P
Suraj Tripathi 47Suraj Tripathi 47
global class PopulateData {
    public static void populateDay(){        
        List<Account> accountList=new List<Account>([Select Id,Name,FieldA__c,FieldB__c from Account limit 10]);


 for(Account ac:accountList){
            String day=ac.Interview_Complete_Date__c.format('EEE');
             DateTime dt =ac.Interview_Complete_Date__c;
                Date myDate = date.newinstance(dt.year(), dt.month(), dt.day());
            if(day=='Sat'){
                ac.Description=string.valueOf(myDate.addDays(-1));
            }else  if(day=='Sun'){
                ac.Description=string.valueOf(myDate.addDays(-2));
            }
        }
       
        update accountList;
    }

Please let me know it is working or not?
Please mark it as the Best Answer .

Thank You

Suraj Tripathi 47Suraj Tripathi 47
global class PopulateData {
    public static void populateDay(){        
        List<Account> accountList=new List<Account>([Select Id,Name,FieldA__c,FieldB__c from Account limit 10]);


  for(Account ac:accountList){
            String day=ac.Interview_Complete_Date__c.format('EEE');
             DateTime dt =ac.Interview_Complete_Date__c;            
            Integer d=dt.day();
            Integer m=dt.month();
            integer y=dt.year();
           

            if(day=='Sat'){
                integer dayDiff=d-1;
                 String finalDate=m+'/'+dayDiff+'/'+y;
                ac.Description=finalDate;
            }else  if(day=='Sun'){
                 integer dayDiff=d-2;
                 String finalDate=m+'/'+dayDiff+'/'+y;
                ac.Description=finalDate; 
            }
        }
       
        update accountList;
    }


if you want to print friday date in 06/11/2021 then use the above code.

don't forget to mark Best Answer

Thank you

ANUTEJANUTEJ (Salesforce Developers) 
so, I was able to find a solution in the apex trigger in the below link:

>> https://developer.salesforce.com/forums/?id=9062I000000g6DAQAY

Using the snippet mentioned you can modify it to make sure that any new record entered or updated if the selected date is lying on Saturday or Sunday you can use adddays function to modify date accordingly.

Let me know if it helps you and close your query by marking it as the best answer so that it can help others in the future.  

Thanks.
sfdcdudesfdcdude
Hi Suraj,

Thank you. Your logic is working for weekend perfectly. Sorry that I have missed one scenario to explain you.

Another scenario is, If it is a weekday then same date should be printed in (ac.Description). In my case i am using this field as a date field. 

could you please add this logic aswell. so that will close this answer as a best answer. Thank you.
Suraj Tripathi 47Suraj Tripathi 47
global class PopulateData {
    public static void populateDay(){        
        List<Account> accountList=new List<Account>([Select Id,Name,FieldA__c,FieldB__c from Account limit 10]);


  for(Account ac:accountList){
            String day=ac.Interview_Complete_Date__c.format('EEE');
             DateTime dt =ac.Interview_Complete_Date__c;            
            Integer d=dt.day();
            Integer m=dt.month();
            integer y=dt.year();
           

           if(day=='Sat' || day=='Sun'){
                 if(day=='Sat'){
                integer dayDiff=d-1;
                 String finalDate=m+'/'+dayDiff+'/'+y;
                ac.Description=finalDate;
            }else  if(day=='Sun'){
                 integer dayDiff=d-2;
                 String finalDate=m+'/'+dayDiff+'/'+y;
                ac.Description=finalDate; 
            }
                
                
            }else{
                 String finalDate=m+'/'+d+'/'+y;
                ac.Description=finalDate;
            }
            
           
        }
       
        update accountList;
    }

Be happy
ravi soniravi soni
Hi pradeep,
create a formula for this requirment.
IF(WEEKDAY( DATEVALUE(Submit_deal_datetime__c) )=1,DATEVALUE(Submit_deal_datetime__c)-2 , 
  IF(WEEKDAY( DATEVALUE(Submit_deal_datetime__c) )=7,DATEVALUE(Submit_deal_datetime__c)-1 ,
  null))

If you want to make populate friday's date then you will have to create deal_report__c as a formula.
and if user select Submit_deal_datetime__c apart of weekend(Saturday, or sunday), it will return null.
let me know if it helps you and marking it as best answer.
Thank you
 
This was selected as the best answer
Suraj Tripathi 47Suraj Tripathi 47
all the issues are solved??
sfdcdudesfdcdude
Thanks much @ Suraj Tripathi 47, Veer Soni and AnuTej for your effort on this.