+ Start a Discussion



System.QueryException: Didn't understand relationship 'Department__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.: Trigger.UpdateAllocate: line 3, column 43




        String queryString = 'SELECT Id, Name, Title__c, Last_Name__c, Middle_Name__c, First_Name__c, Rank__c,    Gender__c, Email__c, Mobile_Number__c, (SELECT Id, Name, Description__c from Department__r LIMIT 1) from Lecturer__c';

            SObject[] queryParentObject = Database.query(queryString);
            for (SObject parentRecord : queryParentObject){
                Object ParentFieldValue1 = parentRecord.get('Name');
                Object ParentFieldValue2 = parentRecord.get('Last_Name__c');
                Object ParentFieldValue3 = parentRecord.get('Last_Name__c');
                Object ParentFieldValue4 = parentRecord.get('Middle_Name__c');
                Object ParentFieldValue5 = parentRecord.get('First_Name__c');
                Object ParentFieldValue6 = parentRecord.get('Rank__c');
                Object ParentFieldValue7 = parentRecord.get('Gender__c');
                Object ParentFieldValue8 = parentRecord.get('Email__c');
                Object ParentFieldValue9 = parentRecord.get('Mobile_Number__c');
                // Prevent a null relationship from being accessed 
                Object ChildFieldValue1;
                Object ChildFieldValue2;
                SObject[] childRecordsFromParent = parentRecord.getSObjects('Department__c');
                if (childRecordsFromParent != null) {
                    for (SObject childRecord : childRecordsFromParent){
                        ChildFieldValue1 = childRecord.get('Name');
                        ChildFieldValue2 = childRecord.get('Description__c');
                        //System.debug('Account Name: ' + ParentFieldValue + '. Contact Name: '+ ChildFieldValue1 + ' ' + ChildFieldValue2);
                Object mName = ParentFieldValue4;          
                if(mName  == null){
                   mName ='';
                allocateList.add(new Allocate__c(
                Name = String.valueOf(ParentFieldValue1),
                Full_Name__c = ParentFieldValue2+' '+ParentFieldValue3+' '+mName+' '+ParentFieldValue5,
                Rank__c = String.valueOf(ParentFieldValue6),
                Gender__c = String.valueOf(ParentFieldValue7),
                Email__c = String.valueOf(ParentFieldValue8),
                Mobile_Number__c = String.valueOf(ParentFieldValue9),
                Department__c = String.valueOf(ChildFieldValue1),
                School__c = String.valueOf(ChildFieldValue2)));
            insert allocateList;




Can Sombody Help?

Shashikant SharmaShashikant Sharma

Go to Department Oject and see whats the name of Relationship with Lecturer object. Your relationship name is incorrect. 


Let me knwo if any issues in it.


 Department is the Detail or Child Object, while Lecturer is the Master Object. So the Relationship Name is in Lecturer.

And Department__c is the Custom Field name in the Lecturer Object which is a foriegn key that links the Department. Hence the Relationship name should be Department__r. Thats where I got the relationship name from.


I hope, you are trying to retrieve department data along with the lecturer data. In that case, your query should like this...

String queryString = 'SELECT Id, Name, Title__c, Last_Name__c, Middle_Name__c, First_Name__c, Rank__c,    Gender__c, Email__c, Mobile_Number__c, Department__r.DeptName__c, Department__r.DeptHead..... from Lecturer__c';