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
kandy11kandy11 

Invalid foreign key relationship

hello

 

Iam getting an invalid foreign key relationship error

 

for(Voucher__c queryvouchers : [select (select Amount_Raised_for_Event__c,CreatedDate,Stage__c, Date_Accepted__c, Amount_Raised_for_Voucher__c, Location_of_event__c,Name_of_the_Event__c,
Date_of_the_Event__c,Temporary_Voucher__r.Account__r.Name from Treasure_Chest_Applications__r ) Total_Amount_Raised_for_Events__c,Total_Amount_Raised_for_Vouchers__c,Number_of_Events_Benefiting__c,Term__c,Name,CreatedDate from Voucher__c where id=: Voucherid ])

 

{
if((date.today().month() - queryvouchers.Treasure_Chest_Applications__r.Date_of_the_Event__c.month() == 1) && (date.today().year() == queryvouchers.Treasure_Chest_Applications__r.Date_of_the_Event__c.year())) {
RelatedVoucher.add(queryvouchers);

CheyneCheyne

Is Treasure Chest Application a parent or child to Voucher? In other words, if you are looking at a voucher record, is there a field for Treasure Chest Application or is there a list of them?

 

Since you are using an inner SOQL query, this query is attempting to pull multiple Treasure Chest Applications for each Voucher, but your if statement is treating it like there is only one Treasure Chest Application for each Voucher. You need to treat queryvouchers.Treasure_Chest_Applications__r like a list.

 

If in fact Treasure Chest Application is a parent of Voucher, then you should construct your query differently:

 

[SELECT Treasure_Chest_Application__r.Date_of_the_Event__c, Total_Amount_Raised_for_Events__c, .... FROM Voucher__c WHERE Id = :Voucherid] 

kandy11kandy11
voucher is the parent object and treasure chest is child.

i want to get the multiple treasure chest records for the particular voucher.
CheyneCheyne

Since Treasure_Chest_Applications__r is a list, you cannot get a property of queryvoucher.Treasure_Chest_Applications__r. Instead, you need to access each element of that list, like so:

 

for (Voucher__c queryvoucher : [your query]) {
    for (Treasure_Chest_Application__c app : queryvoucher.Treasure_Chest_Applications__r) {
      //do stuff with app
    }
}