You need to sign in to do that
Don't have an account?
natty dread
Trigger for after insert OR after update
I have a trigger that simply pulls the time out of a DateTime field and displays it in the 'hh:mm a' format. I'd like this trigger to fire after a new record is inserted OR if an existing record is updated.
I'm very new to the dev side of force.com but I managed to get a working trigger and 100% test coverage but I'm only updating after insert. How would I get this to fire after an update as well? Code below:
trigger time_test on Test__c (after insert) { Set<Id> Ids = new Set<Id>(); for (Test__c tests : Trigger.new){ Ids.add(tests.Id); } List<Test__c> test_list = new List<Test__c>([SELECT Id, Name, Time_Stamp__c, Custom_Time__c FROM Test__c l WHERE CreatedDate = today]); for(Test__c tests : test_list){ String my_time = tests.Time_Stamp__c.format('hh:mm a'); tests.Custom_Time__c = my_time; update tests; } }
Any help at all would be greatly appreciated.
-Natty
Thanks to both Damien and Steve. Using your suggestions along with some more research I was able to fix my issue.
Here's the working trigger:
And here's the class with 100% coverage:
Thanks again everyone.
-Natty
All Answers
Change it to before insert,before update
Use only after insert, after update when you want update a field in second object if theres a change of field in the first object
I'm not quite sure what you're trying to accomplish... but making this after update will create an infinite loop for you because it will keep grabbing the same records and updating them.
FYI, to help you learn a little more let me help you with your code. Your top part with the Ids, you aren't using at all so I completely cut it out since its not doing anything.
List<Test__c> test_list = [SELECT Id, Name, Time_Stamp__c, Custom_Time__c FROM Test__c l WHERE CreatedDate = today];
I removed the new List<Test__c> from your second part here, because doing the query automatically returns you a list already.
update test_list;
Never put a DML statement inside of a loop. You can do DML on an entire list a time saving you from running into governor limits.
Damien why are you using after insert when its on the same object
I took the 'after insert' from his code.
What do you mean by same object? If by same object, you mean the object that was just inserted, then this statement is only partially true. The query he is specifying is grabbing all of the records there were created on the current day. (At least thats the way it appears its supposed to be doing.)
I simply took his code and modified it.
As long as you writing the trigger on an Object X
and you need a field updatte or modication of any field on Object X
We always use before,before update
@Damien
Thanks for pointing that out. I cobbled this code together from snippets I found through Google.
I'm trying to take just the time portion of a user defined DateTime field and dump into a text field in the 'hh:mm AM/PM' format. (ie '3/12/2012 4:55 PM' in SFDC DateTime field should read '04:55 PM' in Text field) I attempted this with a formula field but, after accounting for DLS, I ran into the 'too big to execute' error.
Currently the trigger is only working on new records but I need it to print the new time in my text field if the time is changed on an existing record. How can I accomlish that?
-Natty
Thanks to both Damien and Steve. Using your suggestions along with some more research I was able to fix my issue.
Here's the working trigger:
And here's the class with 100% coverage:
Thanks again everyone.
-Natty