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

apex map question

I've got a map in a trigger on a custom object.  Custom object is called Customer_Usage__c, and Account__c is a lookup field on that object to the standard Account object.




so that is putting in the ID of the account record to which that customer usage record belongs


Can I access a related record on the  Account object here?  In other words I want something like




where Referring_Partner__c is a a lookup field on the account object itself.  So I'd be putting in the ID of the value in the referring partner on the account to which the customer usage record belongs.


Trigger saves but throws an error in testing saying the field does not exist.  Is this syntax error or am I doing something dumb?







You can't reference the field in trigger like "Account__r.Referring_Partner__c" even this is a lookup field. Reason behind it is only id is populating in lookup field not entire account object so you have to query this object if you want to update any field of account object. i am giving a code snippet for that.


set<Id> accountId = new set<Id>();

for(Customer_Usage__c cu : Trigger.New) {
  if(cu.Account__c <> null) {
list<Account> accountList = new list<Account>();
for (Account ac : [SELECT id, Referring_Partner__c FROM account 
                       WHERE id IN :accountId]) {
       ac.Referring_Partner__c = /*Put Your stuff Here*/;

	update accountList;


[If you got answer from my post please mark it as solution.]





Yeah nothing seems to be working but thanks.  Maybe you can look at my code and tell me what is wrong.


This trigger works but not the way I want it to.  The trigger below updates a field on the account record to which the customer usage record belongs.  What I want it to do is exactly what it does now, but, I want it to update the field on an account that is related to the account called Account.Referring_Partner__c


  // ------------------------------------------------------------------------------
    // Account Updates Processing
    // ------------------------------------------------------------------------------

if ((Trigger.isInsert || Trigger.isUpdate || Trigger.isDelete) && Trigger.isAfter) {    
set<Id> AccountIds = new set<Id>();
  if(trigger.isInsert || trigger.isUpdate){
    for(Customer_Usage__c p :{
    for(Customer_Usage__c p : trigger.old){
  map<Id,DateTime> AccountMap = new map <Id,Datetime>();
  for(Customer_Usage__c q : [select Id,First_Positive_Invoice_Date__c
    from Customer_Usage__c where Account__r.Referring_Partner__c IN :AccountIds  order by First_Positive_Invoice_Date__c asc limit 1]){
  List <Account> AccountsToUpdate = new List <Account>();
  for(Account a : [Select Id,Referring_Partner__c,Partner_First_Gross_Add__c from Account where Id IN :AccountIds]){
    Datetime FPID = AccountMap.get(a.Referring_Partner__r.Id);
    a.Partner_First_Gross_Add__c = FPID ;

  update AccountsToUpdate;

 The field Referring_Partner__c is just a lookup field on the account object to the account object itself.


So at the very end there, I want it to be something like


a.Referring_Partner__r.Partner_First_Gross_Add__c, instead of a.Partner_First_Gross_Add__c


I'm thinking I need to add another set or list to this trigger to do that but can't seem to get it to work.  The code compiles everytime I adjust it, but when I test I run into errors.


Any ideas guys?

Jia HuJia Hu

for(Customer_Usage__c q : [select Id,First_Positive_Invoice_Date__c
from Customer_Usage__c where Account__r.Referring_Partner__c IN :AccountIds ,...

is wrong,


you should get the value of 'Referring_Partner__c' from the 1st query, and update it in the second query.


for(Customer_Usage__c q : [select Id, Account__r.Referring_Partner__c, First_Positive_Invoice_Date__c
from Customer_Usage__c where Account__c IN :AccountIds,....




then find the Account of 'Account__r.Referring_Partner__c' and update the field,...