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 

Apex Trigger to update a Date field when a specific picklist value is selected

The link below is my original post that was answered. However, when I enter the complete advance formula for my Date formula field I get "Error: Compiled formula is too big to execute (8,069 characters). Maximum size is 5,000 characters". I have over 70+ picklist values with long names that have to be displayed completely and each value has a corresponding date. Abbreviating is not an option. I spoke with a SF dev support analyst and was told that a possible solution maybe to create an Apex Trigger. 

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

This is the code I used for testing purposes for the Date formula field. 
CASE(TEXT( Project1__c ), 'Test1', DATE(2018,12,02),'Test2', DATE(2018,11,02), null)

https://developer.salesforce.com/forums/ForumsMain?id=9060G000000Xj73QAC#!/feedtype=SINGLE_QUESTION_SEARCH_RESULT&id=9060G000000MVBiQAO

 
Best Answer chosen by Rick SF Admin
Rick SF AdminRick SF Admin
Thanks Tuan. I created a WR and added my formula there. My issue is resolved. 

All Answers

Tuan LuTuan Lu
You could create a workflow rule for each picklist value too. That way you are not limited by formula length or number of picklist values. Let me know if you want help with the trigger. I do Salesforce consulting.
Rick SF AdminRick SF Admin
@Tuan - I thought about that too but that can get extremely meesy. In my post I only show 3 examples, but I have a picklist with 70+ values which would mean 70+ WFRs. On top of that, I have 3 other picklist fields that require the same behavior so those WFRs will accumulate fast.  If I can create an Apex Trigger for one, then I can just clone it and just update the respective picklist's API name in the code. But if I'm missing the idea that you are presenting, please let me know. Thanks 

 
Tuan LuTuan Lu
Yeah thats the gist. If youre familiar with the Apex migration tool, you can easily export the WFR and copy paste the rules you want to change and deploy the changes back into your org. For more commercial implementations I would use a trigger and global picklist with date coded API names. The trigger would read the API name and map that to some standardized date offset and set the the date field appropriately.
Rick SF AdminRick SF Admin
"I would use a trigger and global picklist with date coded API names. The trigger would read the API name and map that to some standardized date offset and set the the date field appropriately."

Can you show me an example of what that would look like? And thanks for the repsonse!
Tuan LuTuan Lu
If you create a pick list option, lets say "TEST", "TEST-2WKS" for Label and API Name respectively. The users would see TEST in the picklist but your trigger can get the API Name and parse everything after the "-" and use that value to add 2 weeks to the current date. And similarly you can have "PROD-4WKS" which would add 4 weeks to the date. Your case statement would be only based on the date parts.
Rick SF AdminRick SF Admin
Thanks Tuan. I created a WR and added my formula there. My issue is resolved. 
This was selected as the best answer