1

I have a scenario. I have two fields; students and courses (parent). I want to stop the users from deleting course records when course contains at least 1 student. If a course does not have any students, we can delete the records (no restriction). I had written my code like this.

trigger recorddel on Course__c (before delete) { 
     for(course__c c : trigger.old){

        list<course__c> cs = [SELECT Id, Name,(select name from students__r) FROM course__c where id  IN (SELECT Course__c FROM Student__c) ]; 

        if (cs.size() > 0) {
            c.adderror('This course contains students cannot be deleted');

        }
     }     
}
1
  • Anyone please suggest me where did i went wrong??
    – sripuram
    Dec 29 '14 at 16:26
3

You've made a couple of mistakes. Firstly, you're querying inside your for loop which is liable to be problematic if you have more than 100 Course__c records that are being deleted. You should do you query outside of the loop to be sure to avoid limits exceptions.

Also, you're really interested in the Course__c records that you are deleting rather than all Course__c's that have Student__c's so your query filter needs to change.

Then, to check the number of Students for a Course, you need to look at the Students__r related list rather than the size of the Course__c list.

Putting all that together, I think you'd end up with a Trigger something like this:

trigger recorddel on Course__c (before delete) { 
     list<course__c> cs = [SELECT Id, Name,(select name from students__r) FROM course__c where id  IN :Trigger.old ]; 

     for(course__c c : cs){    
        if (c.Students__r.size() > 0) {
            Trigger.oldMap.get(c.id).adderror('This course contains students cannot be deleted');
        }
     }     
}
3
  • I am getting this error . My error message is not showing.System.FinalException: SObject row does not allow errors.
    – sripuram
    Dec 29 '14 at 17:48
  • 2
    I think Phil meant -- Trigger.oldMap.get(c.id).addError('...'); instead of c.addError(...) as the addError has to apply to the trigger list; not the list from the SOQL
    – cropredy
    Dec 29 '14 at 19:38
  • :) yep, that's exactly what I meant - editing... Dec 30 '14 at 10:50
0

There is an example of what you are trying to accomplish in the Apex workbook found here. The error you mentioned in your comment above is because you queried for the record you are attempting to add the error to which is outside of the trigger context. Adapting the trigger example on page 49 of the workbook for your object above:

trigger RestrictInvoiceDeletion on Invoice_Statement__c (before delete) {
     // With each of the course records targeted by the trigger
     // and that have students, add an error to prevent them
     // from being deleted.
     for (course__c course :
          [SELECT Id FROM course__c WHERE Id IN (SELECT course__c FROM students__c) AND Id IN :Trigger.old]){
          Trigger.oldMap.get(course.Id).addError(
          'Cannot delete course with students');
     }
}
0

One can achieve the same functionality much simpler by adding a Rollup Summary Field StudentsCount__c (in Case Students__r is a Master-Detail Relationship) and simply putting on a validation rule that checks for StudentsCount__c > 0.

I guess OP does not need this advice anymore, but anyone who comes here through a search engine might find this helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.