+ Start a Discussion
SFAdmin5SFAdmin5 

Trigger to populate field on related record not working

I've got a trigger on a custom object that is not working.

 

Here's how it should work:

 

A user goes into a standard sales-record type Account record.  User creates from that account a record on a custom object called Customer Usage.  Once that record is created the cu record is associated to the account with a simple lookup field to the account record to which it belongs.  What's important to know here is that the account object has a lookup field called Referring_Partner__c.  This lookup field is a lookup to the account object itself, and is used to hold the value of a partner-record type account name.

 

What I want my trigger to do is, when a customer usage record is created or updated with a value in its First Positive Invoice Date field (it's a datetime field), I want that datetime to populate in field on the related account record Partner First Gross Add field. 

 

The problem is that I want the trigger to update the Partner First Gross Add field on the account that is related to the account from which the customer usage record was created, in other words, the trigger should update the partner account record that is related to the sales account from which the cu record was created.

 

Example:

 

Account X = Partner Account record

Account Y = Sales Account record

Customer Usage Z = Customer Usage record

 

User creates a customer usage record called Z with Account__c field value of Y and First Positive Invoice Date of 6/2/2011.

 

Upon insert of Z, record X should be updated with a value of 6/2/2011 in the Partner First Gross Add field.

 

The trigger below does the above, but, what it does is update record Y with 6/2/2011 in the Partner First Gross Add field.  What I want the trigger to do is update record X with 6/2/2011 in the Partner First Gross Add field.

 

How do I do this?  Please help.  I've spent probably 15 hours on this and can't figure it out.

 

trigger CustomerUsage on Customer_Usage__c (before insert, before update, after insert, after update, after delete) {

//-----
// 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;



}

 

 

 

liron169liron169

If you want to update the value in the lookup Account, you should retreive
those records and add them to your updated list.

In the last loop, you should collect list of all the the lookup id's

 

e.g.:


accountPartnerId.add(a.Referring_Partner__c);

and them do the select & update from Account according to this list

SFAdmin5SFAdmin5

Thanks.  Not sure I quite follow this although I think this is right.  I'm just not sure how to update the existing code with what you appear to be saying in your post is a new list called .

 

I'm working on your suggestion but any input you have I'd really appreciate.

 

BTW there was a small error in my trigger posted earlier.  Here's what I've got now that I'm working with

 

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,Account__c
    from Customer_Usage__c where Account__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.Id);
    a.Partner_First_Gross_Add__c = FPID ;
    AccountsToUpdate.add(a);
    

  }
 
  update AccountsToUpdate;
  

}

 

liron169liron169

I think finding the relavent records should be something like this:

 

//stage 1 - find the lookup records id's
List<String> tempLst=new List<String>();
Map<String, String> accountid_partner_map=new Map<String, String>();

for(Account a : [Select Id, Referring_Partner__c
        from Account
        where Id IN :AccountIds]){
    tempLst.add(a.Referring_Partner__c);
    accountid_partner_map.put(a.Referring_Partner__c, a.id);
}

//stage 2 - retreive the lookup records + update their dates.
List <Account> AccountsToUpdate = new List <Account>();

for(Account a : [Select Id,Referring_Partner__c,
        Partner_First_Gross_Add__c
        from Account where Id IN :tempLst]){
    Datetime FPID = AccountMap.get(accountid_partner_map.get(a.Referring_Partner__r));
    a.Partner_First_Gross_Add__c = FPID ;
    AccountsToUpdate.add(a);
}

 

SFAdmin5SFAdmin5

Bingo.  With a slight modification to your code...it worked.  Your code was exactly right I just had to add id to that referring partner field.

 

Thank you so much for your help.  I learned so much from this so thanks.

 

To help others who might need to learn how to populate a field on a related record when child record are populated here's my final code.  Note that I'll probably keep working on this to avoid putting a soql query in a for loop, and clean up the code a lot (add comments, general cleanup, etc.) but at least it works as is:

 

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

if ((Trigger.isInsert || Trigger.isUpdate) && 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);
    }
  }
 
  map<Id,DateTime> AccountMap = new map <Id,Datetime>();
 
  for(Customer_Usage__c q : [select Id,First_Positive_Invoice_Date__c,Account__c
    from Customer_Usage__c where Account__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')
      
      
      );
  }
 
//stage 1 - find the lookup records id's
List<String> tempLst=new List<String>();
Map<String, String> accountid_partner_map=new Map<String, String>();

for(Account a : [Select Id, Referring_Partner__c
        from Account
        where Id IN :AccountIds]){
    tempLst.add(a.Referring_Partner__c);
    accountid_partner_map.put(a.Referring_Partner__c, a.id);
}

//stage 2 - retreive the lookup records + update their dates.
List <Account> AccountsToUpdate = new List <Account>();
 
  for(Account a : [Select Id,Referring_Partner__c,Partner_First_Gross_Add__c from Account where Id IN :tempLst]){
    Datetime FPID = AccountMap.get(accountid_partner_map.get(a.Id));
    if(a.Partner_First_Gross_Add__c == null){
     
     a.Partner_First_Gross_Add__c = FPID ;
     
     }
    
     AccountsToUpdate.add(a);
 
  update AccountsToUpdate;
 
  }
  
}