0

Certification__c(Parent object) --> Exam__c(Child object) Trigger triggers when Child is updated.

trigger certificationToFailedNew on Exam__c (after update) {
    //List out all the exams associated with the parent of the triggered object. exam record -> ParentID -> other child records which share the same parent
    List<Exam__c> exams = new List<Exam__c>();
    exams = [
        SELECT Id,Exam_id__c, Exam_state__c, Certification__r.Id, Certification__r.Exam_state__c
        FROM Exam__c
        WHERE Exam_state__c in ('Failed') AND Certification__r.Exam_state__c in ('Active') AND Certification__r.id == Certification__c.id IN :trigger.newMap.keySet()
            AND id != id IN :trigger.newMap.keySet()];

I'm trying to filter out child records(Exam__c), which share the same parent(Certification__c) of the triggered child record. Or the siblings of the triggered record (other records which share the same parent)

What needs to be changed in my code @ 'WHERE' to get the required records only.

Working UPDATED CODE

trigger certificationToFailedNew on Exam__c (after update) {

    set<id> certificationId = new set<id>();
    String x;
    String y;

    for(Exam__c e : Trigger.new){
        certificationId.add(e.Certification__c);
        x = e.Exam_id__c;
        y = e.Exam_state__c;
    }

    List<Exam__c> exams = new List<Exam__c>();
    exams = [
        SELECT Id, Exam_id__c, Exam_state__c, Certification__r.Id, Certification__r.Exam_state__c
        FROM Exam__c
        WHERE Exam_state__c in ('Failed') AND Certification__r.Exam_state__c in ('Active') 
            AND Id NOT IN :trigger.newMap.keySet() AND Certification__r.Id IN :certificationId 
            AND Exam_id__c = :x AND Exam_state__c = :y];
}
2

This is not valid SOQL syntax.

    WHERE Exam_state__c in ('Failed') AND Certification__r.Exam_state__c in ('Active') 
        AND Certification__r.id == Certification__c.id IN :trigger.newMap.keySet()
        AND id != id IN :trigger.newMap.keySet()];

Additionally, the values in Trigger.newMap().keySet() are Exam__c Ids. You cannot filter Certification__c Ids against them.

Instead, you need to loop over the trigger objects to accumulate a Set<Id> of the Ids of the parent records whose other children you wish to filter.

Then, both of your AND clauses are replaced by

Certification__c NOT IN :setOfParents

That will exclude all siblings of the current records (as well as the current records themselves), as it sounds like you want. If you wanted to get only siblings, you'd reverse the Boolean and add another clause:

    Certification__c IN :setOfParents AND id NOT IN :trigger.newMap.keySet()
  • AND id NOT IN :trigger.newMap.keySet() AND Certification__r.id IN :trigger.newMap.keySet()]; Will this work? this is not showing an error. Or do i have to use the set class? – Niveth Kumar May 23 at 12:28
  • @NivethKumar No. That still filters against the wrong type of Id, as discussed in my answer. – David Reed May 23 at 12:30
  • Thank you, also i found this upvoted 14 times. List<Child__c> children = [ SELECT Name FROM Child__c WHERE Parent__c IN :trigger.newMap.keySet() ]; @salesforce.stackexchange.com/questions/120577/… ----- is this the same thing? can you guide me a bit on how to write the set class for my need. – Niveth Kumar May 23 at 12:38
  • @NivethKumar That doesn't solve your problem. Your trigger is on the child, not the parent. – David Reed May 23 at 12:57
  • thank you for the help, fixed it – Niveth Kumar May 24 at 11:24
1

To get the parent record id first you need to iterate over the trigger and add the parant id in one set.

trigger certificationToFailedNew on Exam__c (after update) {
    set<id> certificationId = new set<id>();
    for(Exam__c e : Trigger.new){
        certificationId.add(e.Certification__c);
    }
    List<Exam__c> exams = [SELECT Id,Exam_id__c, Exam_state__c,Certification__r.Id, 
                           Certification__r.Exam_state__c FROM Exam__c 
                           WHERE Exam_state__c = 'Failed' AND Certification__r.Exam_state__c = 'Active'
                           AND Certification__r.id  IN :certificationId AND id NOT IN :trigger.newMap.keySet()];
}
  • set<id> certificationId = new set<id>(); for(Exam__c e : Trigger.new){ certificationId.add(e.Certification__r.Id); } ***** is this correct, your code shows me an error? – Niveth Kumar May 24 at 6:16
  • certificationId.add(e.Certification__c); e.Certification__c and e.Certification__r.Id Both are same – nubie May 24 at 6:37
  • what is the error message – nubie May 24 at 6:41
  • check the updated code, it is not executing what I want it to do when triggered. I do not get any errors, but i need to find a duplicate entry of the triggered record and then update the parent record. which is not working. – Niveth Kumar May 24 at 8:59
  • fixed it, thank you for the help – Niveth Kumar May 24 at 11:24

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.