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
AlSawtoothAlSawtooth 

Update custom field based on date value in related activity on a different object?

If the close date on my Opp is > than the date of a specific activity on the contact record (where subject = 'Report'), I want to check a custom box on the opportunity. I'm getting a compile error at the second map: Invalid foreign key relationship: Account.ActivityHistory__r at line 17 column 26
 
List<Account> activityaccount = [Select Name, (Select Subject, Id, WhoId, WhatId, ActivityDate from ActivityHistories where Subject = 'Research Report') from Account];
List<Contact> cons = [select Name, Id, AccountId from Contact where AccountId in :activityaccount];
List<Opportunity> opps = [select Amount, CloseDate, cv__Contact__c from Opportunity where cv__Contact__c in :cons];

Map<Id, Date> oppdatemap = new Map<Id, Date>();
for(Opportunity o: [select Id, CloseDate from Opportunity where Id in :opps]){
   oppdatemap.put(o.Id, o.CloseDate);
   }
   
Map<Id, Date> actdatemap = new Map<Id, Date>();
for(Account a: [select (select Id, ActivityDate from ActivityHistories where Id in :activityaccount) from Account]){
    actdatemap.put(a.Id, a.ActivityDate);
    }
    
Map<Date, Date> oppactdatemap = new Map<Date, Date>();
    oppactdatemap.put(oppdatemap.keyset(), actdatemap.keyset());
    
for(Opportunity o :trigger.newmap.keyset()){
    if(datemap.get(o.CloseDate) <= o.CloseDate){
            o.After_RR_Field__c = true;
        }
    }
What am I missing? (Is there a better way to do this?) Thank you!
 
ShashankShashank (Salesforce Developers) 
It could be this line:


actdatemap.put(a.Id, a.ActivityDate);

There is no such field on accounts.