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
Jason FeldmanJason Feldman 

Help with IF statement

I am working on an IF statement for tasks.  If the status picklist is equal to completed, I want the due date field to automatically update to todays date or to throw a message that says the user needs to update due date to today.  This is what I have so far.  Can somebody assist me?

IF (ISPICKVAL(Status, "Completed"),
ActivityDate=TODAY(),NULL)
Best Answer chosen by Jason Feldman
pconpcon
This formula should do what you want
 
AND (
    OR (
        ISCHANGED(Status),
        ISNEW()
    ),
    AND(
        ISPICKVAL(Status, 'Completed'),
        ActivityDate != TODAY() 
    )
)

This will allow you to change other fields afterwards without hitting the validation rule everytime.

All Answers

pconpcon
Are you trying to do this in a trigger or a validation rule?  If you are doing this in a trigger it's simply
 
for (Task t: Trigger.new) {
    if (t.Status == 'Completed') {
        t.ActivityDate = Date.today();
    }
}
It would be a different if you are doing a validation rule.
Jason FeldmanJason Feldman
Sorry, forgot to mention that this is for a validation rule.
pconpcon
This formula should do what you want
 
AND (
    OR (
        ISCHANGED(Status),
        ISNEW()
    ),
    AND(
        ISPICKVAL(Status, 'Completed'),
        ActivityDate != TODAY() 
    )
)

This will allow you to change other fields afterwards without hitting the validation rule everytime.
This was selected as the best answer
Jason FeldmanJason Feldman
That worked.  Thank you very much!
Jason FeldmanJason Feldman
Just curious, but is there a way to make the date automatically change to the current day if status = completed?  The validation rule works great and displays an error when trying to save, but a few users complained and want the date to automatically change for them.
pconpcon
To do that, you'd have to do a VisualForce Page and some re-rendering onChange of the status field to update the ActivityDate.