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
SFAdmin5SFAdmin5 

Account lookup field not populating from Trigger

I've got a trigger that inserts a new sObject record on a custom object when another custom object is created.  It works but the only thing that does not work is populating the Account lookup field.  Here's the code.  The problem has got to be around the line                  "newAN.Account__c = insertedCSBI.Account__c;"

 

I know this is an Id field.  Not sure if I need to be dealing with apex map or set or both here.  All I know is that this trigger creates a new record successfully, but that new record is not associating the new record to an Account.

 

Any ideas?

 

trigger CustomServicesBillingInvoice on Custom_Services_Billing_Invoice__c (after insert, before update, after update) {

    ///Account Note Insert for new Billing Records
    
    if (Trigger.isInsert && Trigger.isAfter) {
           
            //Declare a new List of type Account_Note__c.  Name of List is newANlist 
            List<Account_Note__c> newANlist = new List<Account_Note__c>();     
            
                 //Loop over all of the Custom_Services_Billing_Invoice__c values in Trigger.new
                 for(Custom_Services_Billing_Invoice__c insertedCSBI:Trigger.new){
        
                 //Declare a new Account_Note__c and name it newAN 
                 Account_Note__c newAN = new Account_Note__c();
    
                 //Assign the Custom_Services_Billing_Invoice__c record's values to the fields on the newAN record
                 newAN.Account__c = insertedCSBI.Account__c;
                 newAN.Note__c = insertedCSBI.Billing_Notes__c;
                 newAN.Note_Function__c  = 'Finance';
                 newAN.Note_Status__c = 'Resolved';

          
                 //Add newAN to newANlist for any insertedCSBI records that have some text in the Billing_Notes__c field. 
                 //Exclude CSBI records that are null for this field
                 if(insertedCSBI.Billing_Notes__c !=null){
                 newANlist.add(newAN);
                 }
                 }
    
                 //Insert newANlist 
                 Database.insert(newANlist);

    }

}

 

Andy BoettcherAndy Boettcher

I would put some System.Debug statements in there to see what the values are coming across as through the Trigger.  Weird things can sometimes happen.

SFAdmin5SFAdmin5

Thanks.  That is a good idea.  Unfortunately I couldn't figure that out.

 

However I did get this trigger working perfectly on my own.  It passes 100% test coverage.

 

Thought I'd post the final working code so others can benefit

 

///Account Note Insert for new Billing Records

    
    if ((Trigger.isInsert || Trigger.isUpdate) && Trigger.isAfter) {                           
                   
        //Declare a new List of type Account_Note__c.  Name of List is newANlist 
        List<Account_Note__c> newANlist = new List<Account_Note__c>();     
            
            //Loop over all of the Custom_Services_Billing_Invoice__c values in Trigger.new
            for(Custom_Services_Billing_Invoice__c insertedCSBI:Trigger.new){
        
                //Declare a new Account_Note__c and name it newAN 
                Account_Note__c newAN = new Account_Note__c();
    
                //Assign the Custom_Services_Billing_Invoice__c record's values to the fields on the newAN record
                newAN.Account__c = insertedCSBI.Accountlookup__c;
                newAN.Opportunity__c = insertedCSBI.Opportunity__c;
                newAN.Custom_Services_Billing_Request__c = insertedCSBI.id;
                newAN.Note__c = insertedCSBI.Billing_Notes__c;
                newAN.Note_Function__c  = 'Finance';
                newAN.Note_Status__c = 'Resolved';
                newAN.CreatedBy = insertedCSBI.CreatedBy;

                 //Add newAN to newANlist for any new insertedCSBI records that have some text in the Billing_Notes__c field
                 //OR
                 //Add newAN to the newANlist for any existing insertedCSBI records that have some text in the Billing_Notes__c field
                 //Do not fire the trigger again.  Trigger should only fire the first time the Billing_Notes__c field gets a value
                 if((Trigger.isInsert && insertedCSBI.Billing_Notes__c!=null ) || (Trigger.isUpdate && insertedCSBI.Billing_Notes__c!=null && Trigger.oldMap.get(insertedCSBI.Id).Billing_Notes__c==null  )){
                 newANlist.add(newAN);
                 }
              }
    
                 //Insert newANlist 
                 Database.insert(newANlist);

    }

 

SFAdmin5SFAdmin5

FYI solution is to create a cross object formula field called "Accountlookup__c" that is simply this

 

Opportunity__r.Account.Id

 

In my case this field is on the custom object called "Custom_Services_Billing_Invoice__c"