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
David Okner 17David Okner 17 

trigger to input text in custom field when case is created

Hi all,
writing my first trigger and can't seem to get it to work, can you take a look and see what I am doing wrong? Should it be after insert, before update?
It's just not putting the test text in the custom field...
What i am trying to do is eventually put a template in the description field for my users to fill out.
Thanks!

trigger insertText on Case (before insert, before update) {

            for (case c : trigger.new) {
            
                        If (c.triggertest__c == null)
                                    c.triggertest__c = 'Text here';

                        else {
                                    System.debug ('field is already filled in');
                        }
           
            }
}
AnjithKumarAnjithKumar

Dear David,

You have to trigger the trigger before insert and before update if you want to assign a value to same record or you wanna put some custom validations. 

The code you have mentioned should work. Try to add OR condition in if block like following
 
If (c.triggertest__c == null ||  c.triggertest__c == '')

Hope it helps you. Let me know if need help.

thanks,
Anjith kumar.