You need to sign in to do that
Don't have an account?
Need helping firing trigger only when record changes please...
Hello, I'm fairly new to apex, but I have been able to complete the following code below to capture the last in person and sales phone call activity date. The trigger works perfectly, but I would like to have the trigger fire when an event is entered and closed immediately or when an event is scheduled for the future and then closed as completed when that future date passes and the event is closed by the user. I don't want the trigger to fire everytime a record is updated that meets the criteria only when a record is updated and has somehow changed.
So my question comes down to how can I add the ischanged functionality to my trigger?
Or better yet how can I incorporate the following which you typically find when creating a workflow using the UI.
"When a record is created, or when a record is edited and did not previously meet the rule criteria"
Thanks for the help in advance.
trigger LastFTFDateforAccountStamp on Event (after insert, after update){
Set<Id> Events = new Set<id> ();
List<Account> AccountList = new List<Account>();
for (Event e : Trigger.new)
Events.add(e.id);
Map<Id, Event> eventdate= new Map<Id,Event> ([Select ActivityDatetime from event
where id in :Events]);
for (Event e : Trigger.new){
if (e.call_type__c =='In Person' && e.EventStatus__c=='Completed'&& e.ActivityDatetime < datetime.now()) {
//update account if event is in Person and completed
Account a = new Account( Id = e.AccountId );
a.Last_FTF_Activity__c=e.endDatetime;
AccountList.add(a);
update AccountList;
}
else if (e.call_type__c =='Sales Phone Call' && e.EventStatus__c=='Completed'&& e.ActivityDatetime < datetime.now())
{
Account a = new Account( Id = e.AccountId );
a.Last_SPC_Activity__c=e.endDatetime;
AccountList.add(a);
update AccountList;
}
}
}
Not 100% sure but I think the easiest way is to compare trigger.old and trigger.new, e.g. "trigger.oldMap.get(e) != trigger.newMap.get(e)".
Maybe you have to compare field by field.
Make sure to use triggerMap all the time as it may happen that there is no trigger.old.
Best,
Hannes
So I tried adding a trigger.oldMap to my coding, but it get an error when I try to create an event. If anyone can assist I would be greatly appreciate it as I'm new to this.
I want this trigger to fire when an event is inserted or updated and the eventstatus__c field is changed to completed when the record is saved. Thank you for any assistance.
error message
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger LastFTFDateforAccountStamp caused an unexpected exception, contact your administrator: LastFTFDateforAccountStamp: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.LastFTFDateforAccountStamp: line 10, column 11
trigger LastFTFDateforAccountStamp on Event (after insert, after update){
Set<Id> Events = new Set<id> ();
List<Account> AccountList = new List<Account>();
for (Event e : Trigger.new)
Events.add(e.id);
for (Event e : Trigger.new)
if (trigger.isInsert||trigger.isUpdate){
if (Trigger.oldMap.get(e.Id).eventstatus__c != e.EventStatus__c)
{
if(e.call_type__c =='In Person' && e.EventStatus__c=='Completed'&& e.ActivityDatetime < datetime.now()) {
//update account if event is in Person and completed
Account a = new Account( Id = e.AccountId );
a.Last_FTF_Activity__c=e.endDatetime;
AccountList.add(a);
}
}
else if (Trigger.oldMap.get(e.Id).eventstatus__c !=e.EventStatus__c){
if(e.call_type__c =='Sales phone Call' && e.EventStatus__c=='Completed'&& e.ActivityDatetime < datetime.now())
{
Account a = new Account( Id = e.AccountId );
a.Last_SPC_Activity__c=e.endDatetime;
AccountList.add(a);
}
}
}
update AccountList;
}
Hi,
Bear in mind that Trigger.oldMap is not available for insert triggers.
Do you know if I can separate them in the trigger where I state for insert to do a command, but on update i use the trigger.oldmap? I can't test right now as I'm not at my machine.
see modifications below.
trigger LastFTFDateforAccountStamp on Event (after insert, after update){
Set<Id> Events = new Set<id> ();
List<Account> AccountList = new List<Account>();
for (Event e : Trigger.new)
Events.add(e.id);
for (Event e : Trigger.new)
if (trigger.isInsert){
if(e.call_type__c =='In Person' && e.EventStatus__c=='Completed'&& e.ActivityDatetime < datetime.now()) {
//update account if event is in Person and completed
Account a = new Account( Id = e.AccountId );
a.Last_FTF_Activity__c=e.endDatetime;
AccountList.add(a);
}
}
else if (trigger.isUpdate){
if((Trigger.oldMap.get(e.Id).eventstatus__c !=e.EventStatus__c){
if(e.call_type__c =='Sales phone Call' && e.EventStatus__c=='Completed'&& e.ActivityDatetime < datetime.now())
{
Account a = new Account( Id = e.AccountId );
a.Last_SPC_Activity__c=e.endDatetime;
AccountList.add(a);
}
}
}
}
update AccountList;
}