3

I am trying to create an Apex Trigger that performs a Rollup from a custom object to Contacts.

Here is the error that I am receiving:

Didn't understand relationship 'Assignments__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.

The child relationship name is Assignments.

Below is the code:

    trigger RollupActiveAssigmentLevelsToContact on pse__Assignment__c (after insert, after update, after delete) {

    set<Id> set_id = new set<Id>();

    List<Contact> con_list = new List<Contact>();

    if(trigger.isInsert || trigger.isUpdate){
        for(pse__Assignment__c myAssignment : trigger.New) {
            set_id.add(myAssignment.pse__Resource__c);
        }
    }
    else if(trigger.isDelete){
        for(pse__Assignment__c myAssignment : trigger.Old){
            set_id.add(myAssignment.pse__Resource__c);
        }
    }

    if(trigger.isAfter && (trigger.isInsert || trigger.isUpdate || trigger.isDelete)){
        con_list = [SELECT id,Total_Levels__c, (SELECT id,name FROM Assignments__r WHERE Active_Assignment__c = TRUE) FROM Contact WHERE id IN :set_id];

        for(Contact con : con_list){
            if(con.Assignments__r.size()>0)
                con.Total_Levels__c = con.Assignments__r.size();
            else
                con.Total_Levels__c = 0;
        }
        if(!con_list.isEmpty())
        update con_list;
    }
}

Below is a snap of the Child Relationship name:

Child Relationship Name

7
  • Don't forget after undelete. I'd look at dlrs and save yourself the trouble here.
    – Adrian Larson
    Commented Jun 30, 2016 at 15:21
  • Good call on the undelete. I will check this out as well. Thanks
    – cmmoutes13
    Commented Jun 30, 2016 at 15:22
  • is the relation ship name correct in the query?
    – Anurag
    Commented Jun 30, 2016 at 15:23
  • 1
    Yes it is. I have tried it with and without the __r and I have tried it with and without the plural s. I have double checked the relationship name numerous times. That is what doesn't make sense to me. I was thinking it might have something to do with the fact that the custom object and relationship are a part of a managed package.
    – cmmoutes13
    Commented Jun 30, 2016 at 15:26
  • Can you show a picture of the field configuration after clicking on the field name on the contact object. You need to be using what is listed in the "Child Relationship" name. In all cases where this error occurs the user was using the wrong name or it did not exist. I do not recall any other outcome to this error or any known issues around this error
    – Eric
    Commented Jun 30, 2016 at 15:50

3 Answers 3

3

One snippet I often use to double check my ChildRelationship names in Execute Anonymous goes as follows:

for (ChildRelationship relationship : SObjectType.Contact.getChildRelationships())
    if (relationship.getChildSObject() == pse__Assignment__c.sObjectType)
        system.debug(relationship.getRelationshipName());

Make sure you copy the relationship name verbatim.

4
  • Good call on this. The image above says it is Assignments, but after running this I figured out it is pse_Assignments__r. So I got passed that issue, but I am running into another one. Should I submit a new question?
    – cmmoutes13
    Commented Jun 30, 2016 at 17:31
  • Yeah if this solved your issue and you then found another one, accept this answer and post a new question.
    – Adrian Larson
    Commented Jun 30, 2016 at 17:34
  • Alright will do.
    – cmmoutes13
    Commented Jun 30, 2016 at 17:36
  • @cmmoutes13 - Just saw these comments so my answer I guess is not need but will leave it. That is why the screenshots are helpful for us here. Adrain's answer is great for self help though! Glad you got it worked out
    – Eric
    Commented Jun 30, 2016 at 21:36
0

This query below isn't written correctly. The subquery should be on Assignments, not Assignments__r

con_list = [SELECT id,Total_Levels__c, (SELECT id,name FROM Assignments__r WHERE Active_Assignment__c = TRUE) FROM Contact WHERE id IN :set_id];

That error will propagate throughout the remainder of your code. When you tried it without the __r it's possible that your WHERE clause within the subquery isn't something the the query engine can process and you may need to remove it.

5
  • How do I check to see if it is something that cannot be processed? I feel like a simple checkbox being true should be able to be processed.
    – cmmoutes13
    Commented Jun 30, 2016 at 17:23
  • Try it without the WHERE clause and see if your query works. Very often, WHERE clauses inside of subqueries are problematic if they don't relate to the primary query object. See the SOQL reference & also the Apex Dev Guide for more.
    – crmprogdev
    Commented Jun 30, 2016 at 17:29
  • @Adrian was right below, the relationship was incorrect which I didn't see until I ran his execute statement.
    – cmmoutes13
    Commented Jun 30, 2016 at 17:32
  • @crmprodev - You sure that the subquery should not be on contacts? The field snapshot clearly shows it is a child of Contact
    – Eric
    Commented Jun 30, 2016 at 21:48
  • I don't understand your question. The subquery should be written on the plural of the child object while the main query is written on the parent object. I didn't say anything regarding the subquery and it's parent object Contact other than the inner WHERE clause often needs to be related between the two objects.
    – crmprogdev
    Commented Jul 1, 2016 at 2:26
0

Since this is part of a managed package you will need to add the namespace:

pse__Assignments__r

So the query becomes:

[SELECT id,Total_Levels__c, (SELECT id,name FROM pse__Assignments__r WHERE Active_Assignment__c = TRUE) FROM Contact WHERE id IN :set_id];

You must log in to answer this question.

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