+ Start a Discussion
Hi There!Hi There! 

Help with Apex code in trigger

Hey guys some background into my prob.  I need a lookup field, Account__c, to auto-populate with an account name on update of an event payment object.

 

The relavent schema looks like this:

 

Account

      Id

 

Opportunity

        AccountId

 

             opportunity child Event Payment

                                            Account__c

 So heres what I got so far:

 

trigger accountRename on Event_Payment__c (after update) {   

 Set<Id> accIDs = new Set<Id>();   

     for(Event_Payment__c a: Trigger.new){        accIDs.add(a.Id);    }            

 List<Account> accountList = [SELECT Id FROM Account WHERE Id in :accIDs];        

      for(Event_Payment__c a : Trigger.new){                     

                       for(Account i : accountList){         a.Account__c = i.Id;                        

       }                              update accountList;         }    }

 

Any ideas?


 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
CaptainObviousCaptainObvious

As others pointed out, there may be some inconsistencies in your trigger that do not allow it to work as you intend.

 

See if you can follow the logic here:

 

trigger accountRename on Event_Payment__c (after insert, after update) {   
   
    Set<Id> EventIDs = new Set<Id>();
    Map<Id,Event_Payment__c> PaymentsToProcess = new Map<Id,Event_Payment__c>();
    Map<Id,Event_Payment__c> PaymentsToUpdate = new Map<Id,Event_Payment__c>();

    //This will work whether Creating or Updating the Event Payment,
    //as long as the Event Payment is associated with an opportunity
	
    //All we're doing here is creating a set of ids...
    if(Trigger.isInsert || Trigger.isUpdate){
        for(Event_Payment__c eventPayments : trigger.new){
            if(eventPayments.opportunity__c != null){
                if(!EventIDs.contains(eventPayments.id)){
                    EventIDs.add(eventPayments.id);
                }
            }
        }
    }
	
    if(EventIDs.size() > 0){

        //Retrieve the Event Payment records from our set of ids
        //and collect the relevant fields into a map
        //Note the use of the Opportunity__r relationship to
        //get the Account id
        for (Event_Payment__c ePayment : 
            [SELECT Id, Account__c, Opportunity__r.AccountId
            FROM Event_Payment__c 
            WHERE Id in :EventIDs]){
                PaymentsToProcess.put(ePayment.id,ePayment);
        }

        //Here we look at the Event Payment Account field and compare
        //it with the opportunity accountid. If they are not the same,
        //update the Account field and put the Event Payment into a 
        //map, to be updated later in bulk.
        for(Event_Payment__c ePayment: PaymentsToProcess.values()){
            if (ePayment.Account__c != ePayment.Opportunity__r.AccountId) {
                ePayment.Account__c = ePayment.Opportunity__r.AccountId;
                PaymentsToUpdate.put(ePayment.id,ePayment);
            }
        }
		
        //Finally, run the update
        update PaymentsToUpdate.Values();
    }
}

 

Hope that helps 

All Answers

bob_buzzardbob_buzzard

This line:

 

 

for(Event_Payment__c a: Trigger.new){        accIDs.add(a.Id);    }       

 looks problematic to me - the id that you are adding to the accIds list is actually that of the Event_Payment__c object that you are processing.

 

 

What account id are you trying to copy to each Event_Payment__c object?

 

 

Hi There!Hi There!

I am looking for the account with the same id as in the opportunity AccountId, if that makes sense.  Thanks for the help !

bob_buzzardbob_buzzard

Your trigger doesn't mention opportunities - should it?

Hi There!Hi There!

yeah I am working on this now as a solution:  for (Event_Payment__c a : Trigger.new) { a.Account__c =  a.Special_Event__r.Account.Id;}

 

the a.Special_Event__r. gets me to opportunities but the " a.Special_Event__r.Account.Id " code isnt working. Like if I hard code the id it works but not as the current code and I see no reason why it wouldnt... is there a parse issue or something that changes the id to another format maybe

CaptainObviousCaptainObvious

As others pointed out, there may be some inconsistencies in your trigger that do not allow it to work as you intend.

 

See if you can follow the logic here:

 

trigger accountRename on Event_Payment__c (after insert, after update) {   
   
    Set<Id> EventIDs = new Set<Id>();
    Map<Id,Event_Payment__c> PaymentsToProcess = new Map<Id,Event_Payment__c>();
    Map<Id,Event_Payment__c> PaymentsToUpdate = new Map<Id,Event_Payment__c>();

    //This will work whether Creating or Updating the Event Payment,
    //as long as the Event Payment is associated with an opportunity
	
    //All we're doing here is creating a set of ids...
    if(Trigger.isInsert || Trigger.isUpdate){
        for(Event_Payment__c eventPayments : trigger.new){
            if(eventPayments.opportunity__c != null){
                if(!EventIDs.contains(eventPayments.id)){
                    EventIDs.add(eventPayments.id);
                }
            }
        }
    }
	
    if(EventIDs.size() > 0){

        //Retrieve the Event Payment records from our set of ids
        //and collect the relevant fields into a map
        //Note the use of the Opportunity__r relationship to
        //get the Account id
        for (Event_Payment__c ePayment : 
            [SELECT Id, Account__c, Opportunity__r.AccountId
            FROM Event_Payment__c 
            WHERE Id in :EventIDs]){
                PaymentsToProcess.put(ePayment.id,ePayment);
        }

        //Here we look at the Event Payment Account field and compare
        //it with the opportunity accountid. If they are not the same,
        //update the Account field and put the Event Payment into a 
        //map, to be updated later in bulk.
        for(Event_Payment__c ePayment: PaymentsToProcess.values()){
            if (ePayment.Account__c != ePayment.Opportunity__r.AccountId) {
                ePayment.Account__c = ePayment.Opportunity__r.AccountId;
                PaymentsToUpdate.put(ePayment.id,ePayment);
            }
        }
		
        //Finally, run the update
        update PaymentsToUpdate.Values();
    }
}

 

Hope that helps 

This was selected as the best answer
Hi There!Hi There!

wow!  I had to change opportunity to 'Special_Event', and then it worked!  I can't believe the amount of code necessary just to update that one field.... LoL

thanks so much