You need to sign in to do that
Don't have an account?
Tyler Makula
Apex trigger for assigning value to custom field on Event
I am trying to write an Apex Trigger that will automatically assign a value of 'Meeting' to the custom picklist field Activity_Type__c on the Event object. Because this is a required field, the only way it can be Null is if the Event was created through Lightning Sync from Outlook to Salesforce. I am getting a code coverage error for this. Does anyone have any ideas on improving this code?
trigger Update_Event_Activity_Type on Event (before insert) {
try {
//iterate over the new activities
for(Event newEvent : trigger.new){
if(newEvent.Activity_Type__c == Null){
newEvent.Activity_Type__c = 'Meeting';
}
}
}
Catch(Exception e){
system.debug(e);
}
}
trigger Update_Event_Activity_Type on Event (before insert) {
try {
//iterate over the new activities
for(Event newEvent : trigger.new){
if(newEvent.Activity_Type__c == Null){
newEvent.Activity_Type__c = 'Meeting';
}
}
}
Catch(Exception e){
system.debug(e);
}
}
A few options to try before tackling the Apex trigger.
1. Did you try using a default value on the picklist values so that it defautls to 'Meeting' on creation?
2. Have you checked using a process builder to handle the field update?
both these options would obviously avoid the need for test coverage.
If you want to go with a Trigger, first questions -
1. Is it a validation rule that is enforcing the field value? or
2. a required field at Object level? or
3. a 'required field' setting on the page layout?
If you have a required field at the Page Layout level, (my preferred option), your test data can avoid the need to populate the field when being inserted.
Regards
Andrew