+ Start a Discussion
SFAdmin5SFAdmin5 

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.

 

AccountMap.put((ID)q.get('Account__c')

 

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

 

AccountMap.put((ID)q.get('Account__r.Referring_Partner__c')

 

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?

 

 

 

KapilCKapilC

Hi,

 

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) {
	accountId.add(cu.Account__c);
  }
}
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*/;
	   accountList.add(ac);
    }

	update accountList;

 

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

 

Thanks,

Kapil

SFAdmin5SFAdmin5

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 : trigger.new){
      AccountIds.add(p.Account__c);
    }
  }
 
  if(trigger.isDelete){
    for(Customer_Usage__c p : trigger.old){
      AccountIds.add(p.Account__c);
    }
  }
 
  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]){
      AccountMap.put(
      
      (ID)q.get('Account__c'),
      (Datetime)q.get('First_Positive_Invoice_Date__c')
      
      
      );
  }
 
  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 ;
    AccountsToUpdate.add(a);
    

  }
 
  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,....

 

(ID)q.get('Account__r.Referring_Partner__c'),
(Datetime)q.get('First_Positive_Invoice_Date__c')

 

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