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
Rick SF AdminRick SF Admin 

Trigger to auto populate a date field when a picklist value is selected

I am trying to create a trigger that will auto populate a date field when a picklist value is selected. I'm trying to achieve this without creating another custom object. 

Ex. PIcklist Values: Test, Demo, Draft

If Test; then date value = 12/1/2018
If Demo, then date value = 11/1/2018
If Draft then, date value = 10/1/2018
Best Answer chosen by Rick SF Admin
Nikhil Verma 6Nikhil Verma 6
Hi Rick,
You can simply use a Formula field with return type date in order to populate your date field. The formula would look something like:
CASE(TEXT(PicklistField__c), 'Test', DATE(2018,12,1), 'Demo', DATE(2018,11,1), 'Draft', DATE(2018,10,1), null)
In a similar manner, you can go on and add your 20 different picklist values in value/result pair. Please NOTE  that the last value is the value that will be populated in case none of the conditions meet the criteria.

Hope this helps.

All Answers

Abdul KhatriAbdul Khatri
You can use Workflow Update Field instead of trigger.
Rick SF AdminRick SF Admin
What would be the formula? I'm trying to create this workflow for about 20 different picklist values. I need to use the Date field to populate the date if possible.
 
Nikhil Verma 6Nikhil Verma 6
Hi Rick,
You can simply use a Formula field with return type date in order to populate your date field. The formula would look something like:
CASE(TEXT(PicklistField__c), 'Test', DATE(2018,12,1), 'Demo', DATE(2018,11,1), 'Draft', DATE(2018,10,1), null)
In a similar manner, you can go on and add your 20 different picklist values in value/result pair. Please NOTE  that the last value is the value that will be populated in case none of the conditions meet the criteria.

Hope this helps.
This was selected as the best answer
Rick SF AdminRick SF Admin
Perfection. Thank you Nikhil. 
Sumit Bhati 7Sumit Bhati 7
yes definitely future is of salesforce and it looks interesting to everyone. Try these Happy birthday wishes for sister in hindi (https://mahitrack.com/birthday-wishes-for-sister-in-hindi/) to get more about this.