function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
shikher jainshikher jain 

I created two custom classes class and student. I want to create a trigger for updating some values and delete related students

trigger CustomStatusChanged on Class__c (before update) {

    for(Class__c cls : trigger.new){
        if(cls.Custom_Status__c == 'Reset'){
            delete [Select Name from Student__c where Class_object__c in :trigger.new];
        }
    }
}

I have written this code but got self reference error
Best Answer chosen by shikher jain
ANUTEJANUTEJ (Salesforce Developers) 
What I meant was initially you could get all the class ids that match the condition: cls.Custom_Status__c == 'Reset'  then get all the list of records of the student where the class ids are in the set then in a single DML operation you can simply delete the list of student records.

Generally, soql queries inside a loop is not considered as a best practice that is why I was suggesting the above method.

As your implementation could change this was one way I thought this can be implemented.

 You can find something similar in the link :- [ https://salesforce.stackexchange.com/questions/45566/deleting-child-records-on-a-parent ]

I hope this helps.

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Shikher,

Can you try putting the necessary records in a list and then delete do a DML operation after the loop, let me know if this works and you are doing a soql for every value is an iteration in the trigger.new can you try minimizing it by adding the classes id or such to a set then get the student records that match it and then delete them after the loop.

Do let me know if this helps and in case if it does can you mark this as the best answer so that it can be used by others in the future.

Regards,
Anutej
shikher jainshikher jain
Hi Anutej , I am unable to understand your concern , could you please elaborate it with a slice of code.
ANUTEJANUTEJ (Salesforce Developers) 
What I meant was initially you could get all the class ids that match the condition: cls.Custom_Status__c == 'Reset'  then get all the list of records of the student where the class ids are in the set then in a single DML operation you can simply delete the list of student records.

Generally, soql queries inside a loop is not considered as a best practice that is why I was suggesting the above method.

As your implementation could change this was one way I thought this can be implemented.

 You can find something similar in the link :- [ https://salesforce.stackexchange.com/questions/45566/deleting-child-records-on-a-parent ]

I hope this helps.
This was selected as the best answer
John PetrellaJohn Petrella
ANUTEJ, Thanks to you for this complete answer, I have now got to do this and have encountered the same problem but with your help I have got myself sorted very well!
Stephen Montgomery 6Stephen Montgomery 6
Hi, now I am a student at technical college, and for a long time I have been doing programming, if you need help you can apply, besides this, I want to find out if only I have to do for home essays, this irritates me a lot, but I found this source https://eduzaurus.com/free-essay-samples/martin-luther-king-i-have-a-dream/ from where I can get free samples that I use for my writings, by the way, it impressed me that much information I can find here, the last one I read was Martin Luther King I have a dream. It has developed my skills in leading people and understanding myself better.
john micronjohn micron
not a bad combination, my dream is to become a programmer and write codes, but for now I'm studying to be a writer. My cousin with whom I live forced me to do this. It's good that there is mba assignment help (https://www.brillassignment.co.uk/mba-assignment-help/) it helps me do my homework. I hope I will collect money and finish at least elementary programming courses
Xodeg SaskijXodeg Saskij
Labcorp Patient Portal Login (https://labcorp-patient-portal.info/) – Labcorp Patient Portal (Patient.labcorp.com) is an online service provided by Laboratory Corporation of America Holdings to improve the overall management of clinical and non-clinical information.
Caroll BleinCaroll Blein
When I write my memo for me using https://writer-elite.com/memo-paper/, I have plenty of time to learn about programming because I know that my work would be done well and quickly.
A self-reference error occurs when you attempt to delete a record and the record being deleted is also referenced in the trigger context. In your trigger, you delete the related Student__c records where Class_object__c is in trigger.new. However, trigger.new is a list of Class__c records, and you cannot use it to filter related Student__c records.

To fix this, you need to create a separate list to store Class_object__c values and use that list to filter related Student__c records.