0

i am trying to update the parent field with child field but when I try to assign the child's field value to the parent field I keep getting the error "A non-foreign key field cannot be referenced in a path expression: Review_Informations__r "

public  class Reviews {
  
  public static void updateReviewRangeOnContact (List <Review_Info__c> reviewList){

     Set <Id> contactIds = new Set <Id> ();

        if(reviewList != null && reviewList.size()>0){
            for (Review_Info__c revi : reviewList){
                if (revi.Contact__c != null){
                    contactIds.add(revi.Contact__c);
                }
            }
        }

      List <Contact> ContactList = new List <Contact> ();
      ContactList = [SELECT id, Review_rate__c,(SELECT Id,Review_Rating__c FROM Review_Informations__r) 
                     FROM Contact 
                     WHERE ID IN : contactIds];

        for (contact con : ContactList){
            con.Review_rate__c = con.Review_Informations__r.Review_Rating__c;
        }
        if (ContactList != null && ContactList.size()>0 )
        update ContactList;

  }


}

1 Answer 1

1

You're trying to use a List as a non-list value, which is not allowed. You're also using way too many "guards"; your code can be simplified. Here's a revised version:

public  class Reviews {
  
    public static void updateReviewRangeOnContact (List <Review_Info__c> reviewList){

        Set <Id> contactIds = new Set <Id> ();

        for (Review_Info__c revi : reviewList){
            contactIds.add(revi.Contact__c);
        }
        contactIds.remove(null);
        List <Contact> ContactList = [SELECT id, Review_rate__c,(SELECT Id,Review_Rating__c FROM Review_Informations__r) 
                        FROM Contact 
                        WHERE ID IN : contactIds];
        for (contact con : ContactList){
            if(con.Review_Informations__r.size() > 0) {
                con.Review_rate__c = con.Review_Informations__r[0].Review_Rating__c;
            }
        }
        update ContactList;
    }
}

You must log in to answer this question.

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