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
natty dreadnatty 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

Best Answer chosen by Admin (Salesforce Developers) 
natty dreadnatty dread

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: 

trigger time_test on Test__c (before insert, before update) {
    Set<Id> ids = new Set<Id>();
 
    list<Test__c> tlist = [SELECT Id, Name, Time_Stamp__c, Custom_Time__c FROM Test__c Where Id IN : ids];
    
    for(Test__c tests : trigger.new){
    	tests.Custom_Time__c = tests.Time_Stamp__c.format('hh:mm a');
    }
    insert tlist;
}

 

And here's the class with 100% coverage: 

@isTest
public with sharing class test_time_update {
	static testMethod void test_unit(){
		Test__c t = new Test__c();
		t.Time_Stamp__c = datetime.newInstance(2012, 12, 31, 23, 59, 00);
		insert t;
	}
}

 Thanks again everyone. 

 

-Natty

All Answers

steve456steve456

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

Damien_Damien_

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.

 

trigger time_test on Test__c (after insert)
{
	List<Test__c> test_list = [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 test_list;
}

 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.

steve456steve456

Damien why are you using after insert when its on the same object

Damien_Damien_

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.

steve456steve456

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

natty dreadnatty dread

@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

natty dreadnatty dread

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: 

trigger time_test on Test__c (before insert, before update) {
    Set<Id> ids = new Set<Id>();
 
    list<Test__c> tlist = [SELECT Id, Name, Time_Stamp__c, Custom_Time__c FROM Test__c Where Id IN : ids];
    
    for(Test__c tests : trigger.new){
    	tests.Custom_Time__c = tests.Time_Stamp__c.format('hh:mm a');
    }
    insert tlist;
}

 

And here's the class with 100% coverage: 

@isTest
public with sharing class test_time_update {
	static testMethod void test_unit(){
		Test__c t = new Test__c();
		t.Time_Stamp__c = datetime.newInstance(2012, 12, 31, 23, 59, 00);
		insert t;
	}
}

 Thanks again everyone. 

 

-Natty

This was selected as the best answer