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
HobieHobie 

Apex Trigger Help for an Event

I'm a newbie just learning to write Apex Code.  I wrote an Apex Trigger to set Type_Of_Activity__c  to equal Type.  This works great:

 

trigger SyncType on Task (before insert, before update){
  Task myTask = trigger.new[0];
        if(myTask.Type != null){
            myTask.Type_of_Activity__c = myTask.Type;
        }
}

 

How do you do this same thing when doing an Event?  I want to set Type_of_Activity__c equal to Event Type.

 

Thanks!

Suresh RaghuramSuresh Raghuram

Try this

 

trigger SyncType on Event (before insert, before update){
  Event myEvent= trigger.new[0];
        if(myEvent.Type != null){
            myEvent.Type_of_Activity__c = myEvent.Type;
        }
}

 

HobieHobie

I actually tried this and got failures.  Got 0% coverage and it failed.  What am I doing wrong?

Ryan DRyan D

Hard to say what you are doing wrong but your test code has to simulate what happens when you insert/update an event object.  So you have to create an event and insert it.  Then if there are conditions that must be met in the trigger you have to set your object to meet and/or not meet those conditions.