I have a trigger on a custom object called CustomActivity__c
that calls a class on before insert and before update. I have a custom object similar to Opportunity Teams (StudyTeam__c)
where there is a record for every User who has access to a record for a custom object called Study__c
. StudyTeam__c
and CustomActivity__c
are child objects of master Study__c
. StudyTeam__c
and CustomActivity__c
both have lookups to the standard User object, the fields being User__c
.
A Validation Rule will not work because of the object relationships. I need code to evaluate that the User lookup ID indicated on CustomActivity__c
record matches a User lookup ID indicated on any of the StudyTeam__c
records for the Study__c
. If the User lookup on CustomActivity__c
does not match the User lookup on a StudyTeam__c
record for the Study__c
, then the system should throw an error saying "Only team members can be assigned to the activity."
Can you help me with the class? I don't have much written because I don't know how to write code well.
I assume it'll be:
- Get a list of StudyTeam__c records for the Study__c
- Compare if User__c for CustomActivity__c matches User__c for StudyTeam__c in the list of StudyTeam__c records
- If FALSE, display an error message
Trigger
trigger CustomActivityTrigger on CustomActivity__c (after insert, after update, after undelete, after delete, before insert, before update, before delete) {
if(trigger.isBefore && (trigger.isInsert || trigger.isUpdate)){
CustomActivityUserValidation.validation(trigger.new);
}
}
Class
public class CustomActivityUserValidation {
public static void validation(List<CustomActivity__c> val) {
Set<Id> studyIds = new Set<Id>();
for(CustomActivity__c ca : val){
studyIds.add(ca.Study__c);
}
}// End public static void
}// End class