You need to sign in to do that
Don't have an account?
Manuella Pinheiro
Apex Trigger before insert
I am trying to write an apex trigger in a custom object, but i have one doubt. I have two custom objects, one called "Evaluation" and other called "Evaluation Result". They have a master-detail relationship, in which "Evaluation" is the master and "Evaluation Result" is detail.
"Evaluation" contains a field with the total possible points of the evaluation (test / exam) and "Evaluation Result" contains a field for the student's grade to be recorded. I would like to implement a trigger on the "Evaluation Result" object before inserting the record, which verifies that the registered points do not exceed the total possible points of the related Evaluation. If the points recorded are greater than the total points possible for the assessment, an error should be returned. If they are less than or equal, the "Evaluation Result" record should be entered normally. So, i don't know how to compare the two fields and make the trigger works.
"Evaluation" contains a field with the total possible points of the evaluation (test / exam) and "Evaluation Result" contains a field for the student's grade to be recorded. I would like to implement a trigger on the "Evaluation Result" object before inserting the record, which verifies that the registered points do not exceed the total possible points of the related Evaluation. If the points recorded are greater than the total points possible for the assessment, an error should be returned. If they are less than or equal, the "Evaluation Result" record should be entered normally. So, i don't know how to compare the two fields and make the trigger works.
download videos from facebook (https://www.fbvideodl.com)
Hi Manuella,
You can try something as below:
Trigger erTrigger on Evaluation_Result__c(before inset, before update){
Set<Id> parentIds = new Set<Id>();
if(Trigger.isBefore){
if(Trigger.isInsert || Trigger.isUpdate){
for(Evaluation_Result__c er : Trigger.new){
if(er.Evaluation__c!=null) //checking parentId for being Null
parentIds.add(er.Evaluation__c); // adding parentIds
}
}
}
System.debug('parentIds --'+parentIds );
Map<Id,Evaluation__c> eveMap = new Map<Id,Evaluation__c>([SELECT Id, total_points__c FROM Evaluation__c WHERE Id IN : parentIds]);
if(eveMap.size()>0){
for(Evaluation_Result__c er : Trigger.New){
if(eveMap.containsKey(er.Evaluation__c)){
if(er.marks__c > eveMap.get(er.Evaluation__c).total_points__c){
er.addError('Error---');
}
}
}
}
}
Thanks,
Sucharita