function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Vinu VargheseVinu Varghese 

Error in select entry in a relational object

I have the following select statement, but it giving me error. When I query seperately it works. I tried under query editor also. Same error.

List<Deposit_Detail__c> depositDetailList = [Select Deposit__r.Name,  Id,Amount__c, Member_Name__c from Deposit_Detail__c Where Id IN :trigger.new];

please help me.

Thanks

 
ANUTEJANUTEJ (Salesforce Developers) 
Hi Vinu,

As you are using trigger.new you need to use it inside a code snippet that has access to these records in the trigger.new can you once check if it helps if you add the list of id's of the trigger.new records to a set and then using the below snippet?
 
set<id> sid= new set<id>();

for(Deposit_Detail__c dd:trigger.new)
{
sid.add(dd.id);
}

List<Deposit_Detail__c> depositDetailList = [Select Deposit__r.Name,  Id,Amount__c, Member_Name__c from Deposit_Detail__c Where Id IN :sid];



Also, please do note that the records in trigger.new would have record id only when it is after insert or in update scenarios.

I hope you are not performing the above snippet in before insert, as the records would not have recordid's.

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
ANUTEJANUTEJ (Salesforce Developers) 
In case if the above case is not the scenario can you mention the error you are getting and the code if possible to check further.
Vinu VargheseVinu Varghese
The error is as follows:
Select Deposit__r.Name, Id,Amount__c ^ ERROR at Row:1:Column:8 Didn't understand relationship 'Deposit__r' in field path. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name.

Note that its nothing to do with trigger.new. I removed the trigger.new from the select entry. still has the folowing error. The same way I did it in other triigers/classes and its works. The following code works as an example:

List<Receipts_Payments__c> memberList = [Select Member__r.Id, Member__r.Name, Member__r.Email__c, 
            Id, Member__c, RecordTypeId from Receipts_Payments__c where Id IN :trigger.new
            ];


But  this one doesn't.
List<Deposit_Detail__c> depositDetailList = [Select Deposit__r.Name,  Id,Amount__c, Member_Name__c from Deposit_Detail__c Where Id IN :trigger.new]; 

I even tried to use child to parent relationship query, that doesn't too. 

I don't know whether the error is related to object itself.


 
Malika Pathak 9Malika Pathak 9
Goto-> Position(i.e Child Object) -> click on view fields --> check the Look up field name ( that we used to define the relationship between child (Deposit detail) and parent(Deposit))

This "Lookup field name"(inside the child object i.e Position here) should be exactly same as the "Parent Object API Name"
trigger YA11trigger on Deposit_Detail__c (After insert) {
if(trigger.isAfter && trigger.isInsert){
List<Deposit_Detail__c> depositDetailList = [Select Deposit__r.Name,Id,Amount__c, Member_Name__c from Deposit_Detail__c Where Id IN :trigger.new];
}


}

 
Vinu VargheseVinu Varghese
Parent object API name : Deposit__c
Child Object API Name: Deposit_Detail__c
Child Relationship Name : Deposit_Details