0

My code is:

    trigger ClassCustomStatus on Class__c (before update) {
        Set<Id> classSet = new Set<Id>();
        for(Class__c c : Trigger.new){
            if(c.Custom_Status__c == 'Reset'){
                classSet.add(c.Id);
            }
        }
        list<student__C> s = [SELECT id FROM Student__c WHERE class__c IN:classSet];
        Delete s;
    }

Here I am getting one exception this: enter image description here

How can delete child records which are from student__c And How can I update a field of parent which is Class__c Please help

2
  • Is NumberOfStudents a Roll-Up Summary Field from Students to Class?
    – David Reed
    Apr 26, 2020 at 18:58
  • @DavidReed Yes this is roll-up summary field
    – Gopal
    Apr 26, 2020 at 18:59

1 Answer 1

0

Use after update to make sure that the record isn't already pending any changes. The rest of your code is fine.

8
  • Thanks it's working What should i do to update the mycount field to Zero in class__c
    – Gopal
    Apr 26, 2020 at 19:25
  • @gopal That should be in the before trigger. You can do something like if(Trigger.isBefore) setClassToZero(Trigger.new) else deleteChildRecords(Trigger.new) to keep everything in one trigger.
    – sfdcfox
    Apr 26, 2020 at 20:25
  • I have added another image please review
    – Gopal
    Apr 26, 2020 at 20:40
  • You don't manually set the value of a Roll-up Summary Field. The system will recalculate it for you. Please don't post code as images; use the {} tool in the editor to format code.
    – David Reed
    Apr 26, 2020 at 20:49
  • that's not a roll-up field that's number field
    – Gopal
    Apr 26, 2020 at 20:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .