0

I have an object Tourist_Details__c which has 2 child objects Contacts and Incidents__c.

I already have a list of Incidents__c called results and now I need to get a list of all Tourist_Details__c where the related incident is in results. This is so I can perform some functions on the related Contact.

Here is my code:

public void anonymise() {
    List<Incident__c> results = 
        [SELECT id, Incident_name__c, Date_of_incident__c, Name, Garda_Name__c 
         FROM Incident__c WHERE Date_of_Incident__c <= :firstDate.Date_Of_Incident__c AND 
        Date_of_Incident__c >= :lastDate.Date_Of_Incident__c];

    for (Incident__c inc : results) {
        inc.Incident_name__c = inc.Name;
        inc.Garda_Name__c = '';
    }
    update results;

    List<Tourist_Details__c> touristDetails = 
        [SELECT Id, (SELECT Id FROM IncidentContacts__r)
         FROM Tourist_Details__c
         WHERE Incident__c in :results];               
}

    for (RELATED_CONTACT : touristDetails) {
    //do something
    }
    update touristDetails;

My problem is that I am getting the following error:

SELECT Id, (SELECT Id FROM IncidentContacts__r)
                       ^
ERROR at Row:1:Column:28
Didn't understand relationship 'IncidentContacts__r' in FROM part of query 
call. If you are attempting to use a custom relationship, be sure to append 
the '__r' after the custom relationship name. Please reference your WSDL or 
the describe call for the appropriate names.

For my inner select statement in touristDetails I have tried using the object name, API name and also the given Child Relationship Name (shown in code) on the Contact field in Tourist_Details__c but I always seems to get the same error.

3
  • 1
    The Child Relationship Name is the correct choice. The error suggests that this simply isn't the child relationship name. It's shown on the lookup field from the child to the parent.
    – David Reed
    Jan 9, 2019 at 12:38
  • Thanks David, you were right. It seems as if my Tourist_Details__cobject is actually the child so I was incorrect to do an inner SOQL statement
    – Luke Bray
    Jan 9, 2019 at 12:57
  • Great! I'll convert my comment to an answer.
    – David Reed
    Jan 9, 2019 at 13:10

1 Answer 1

0

For my inner select statement in touristDetails I have tried using the object name, API name and also the given Child Relationship Name (shown in code) on the Contact field in Tourist_Details__c but I always seems to get the same error.

You were on the right track: the Child Relationship Name is always the correct choice when performing a parent-to-child subquery in SOQL. However, the presence of the error indicates that IncidentContacts__r just isn't the child relationship name, which is shown on the lookup field from the child to the parent.

Based on your comment, you've discovered that your data is arranged a little differently, so that should resolve the issue.

1
  • 1
    @Luke Bray Another thing to consider - If the field is part of a managed package, the real relationship name has the namespace prepended to the beginning. You can see on Workbench the full API name including the prepended namespace.
    – Salvation
    Jan 9, 2019 at 13:34

You must log in to answer this question.

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