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
james1986james1986 

putting related fields from SOQL query into map

hi,

I have a apex class that runs every night to update aggregate fields on Accounts. It works fine, but I added a new peice of functionality that is not working.

 

I've added a field called "grandmother sponsor" (I am working with an NGO doing work in southern africa) to accounts. Everynight when my class runs, I want to pull up all accounts that have a child object called monthly_gift__c with a child object called Monthly_Gift_Allocations__c that is linked to a program account called grandmother support.

 

I know my query works. I know I have active grandmother sponsors. But when I run my test classes the bold/italic/underlined peice of code (below) never runs.

 

 

Would anyone mind having a look over the code below and tell me what I need to do get all accounts with a child object that has a grandchild object? Snippets of my code are below.

 

 

 

 

 

List<Monthly_Gift_Allocations__c> gsponsors = new List<Monthly_Gift_Allocations__c>();
for (Monthly_Gift_Allocations__c gsponsorquery: [SELECT Monthly_Gift__r.Account__r.id from Monthly_Gift_Allocations__c where (Monthly_Gift__r.end_date__c = NULL OR Monthly_Gift__r.end_date__c > :system.today()) and (amount__c >= 20 and stream__r.name = '2 - Grandmother Support')])       
        {
            id myid = (id)gsponsorquery.get('id');
            isactive_grannysponsor.put(myid, TRUE);
          
        }

 

......

 

      List<Account> accountstoupdate = new List<Account>();
        for (Account a: [SELECT Total_transactions__c, granny_sponsor__c, last_nonmonthly__c, First_Transaction__c, Last_Transaction__c, Number_of_transactions__c, isactive_monthly__c, ID FROM Account])
        {

      if (isactive_grannysponsor.get(a.ID) != NULL)
            {
                a.granny_sponsor__c = TRUE;
            }
            else if (isactive_grannysponsor.get(a.ID) ==NULL)
            {
                a.granny_sponsor__c = FALSE;
            }

}

Best Answer chosen by Admin (Salesforce Developers) 
jhurstjhurst

James,

 

The issue is with your logic.  From what I can tell, you are expecting the code:

 

 

id myid = (id)gsponsorquery.get('id');

 

 

To be the related Monthly_Gift__r.Account__r.id value. In reality, it is the ID of the Monthly_Gift_Allocations__c.  Because of this, later in the code you look for the Account.ID in the map, and it does not exist.

 

Instead, you need to set myid to be the Account ID:

 

 

id myid = (id)gsponsorquery.Monthly_Gift__r.Account__r.id;

This should bring you back the correct Account Id.  Also, you do not have to case the result to Id, the system will do that for you.  It does not hurt to do so, but it is not necessary.

 

Hope this helps./

Jay 

 

 

All Answers

jhurstjhurst

James,

 

The issue is with your logic.  From what I can tell, you are expecting the code:

 

 

id myid = (id)gsponsorquery.get('id');

 

 

To be the related Monthly_Gift__r.Account__r.id value. In reality, it is the ID of the Monthly_Gift_Allocations__c.  Because of this, later in the code you look for the Account.ID in the map, and it does not exist.

 

Instead, you need to set myid to be the Account ID:

 

 

id myid = (id)gsponsorquery.Monthly_Gift__r.Account__r.id;

This should bring you back the correct Account Id.  Also, you do not have to case the result to Id, the system will do that for you.  It does not hurt to do so, but it is not necessary.

 

Hope this helps./

Jay 

 

 

This was selected as the best answer
james1986james1986

Awesome, thanks Jay!