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
Martin AbanesMartin Abanes 

Update Contact Account Name when Blank to Dummy Account

Here is my Apex trigger:

trigger AccountAssignment on Contact (after insert, after update) {
    
        List<Contact> conToInsert = [select id, accountid from contact  where id in: trigger.newmap.keyset()];


        Account defaultAccount = [SELECT ID FROM account Where ID  = '001i0000010asN9' limit 1]; 
       
        //record id of Dummy Account is 001i0000010asN9
     
          for (Contact c : conToInsert) {            
                      
                if( c.accountid == NULL)
              {        
             
                c.accountid = defaultAccount.id;    
                 
               conToInsert.add(c);           
            
               }           
        }    
        }
Best Answer chosen by Martin Abanes
Arunkumar RArunkumar R
Hi Martin,

Please note the below few points,

1. Trigger.new and trigger.old are read only
2. An object can change its own field values only in before trigger: trigger.new
3. In all cases other than mentioned in point 2; fields values cannot be changed in trigger.new and would cause run time exception "record is read only"

Here is your code,

trigger AccountAssignment on Contact (before insert, before update) 
{
    Account defaultAccount = [SELECT ID,Name FROM account Where ID  = '001i000000sGC7h' limit 1]; 

    for (Contact c : Trigger.New) 
    { 
        if(c.accountid == NULL)
        {        
            c.AccountId = defaultAccount.Id;    
        }           
    }
}


All Answers

Martin AbanesMartin Abanes
Trigger was succesfully Saved however when I try to create new contact without Account Name it throws an error "Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger AccountAssignment caused an unexpected exception, contact your administrator: AccountAssignment: execution of AfterInsert caused by: System.FinalException: Cannot modify a collection while it is being iterated.: Trigger.AccountAssignment: line 10, column 1".

Am I missing something? Any help on this code will be greatly appreciated. 
Arunkumar RArunkumar R
Hi Martin,

Please note the below few points,

1. Trigger.new and trigger.old are read only
2. An object can change its own field values only in before trigger: trigger.new
3. In all cases other than mentioned in point 2; fields values cannot be changed in trigger.new and would cause run time exception "record is read only"

Here is your code,

trigger AccountAssignment on Contact (before insert, before update) 
{
    Account defaultAccount = [SELECT ID,Name FROM account Where ID  = '001i000000sGC7h' limit 1]; 

    for (Contact c : Trigger.New) 
    { 
        if(c.accountid == NULL)
        {        
            c.AccountId = defaultAccount.Id;    
        }           
    }
}


This was selected as the best answer
Gopal RathoreGopal Rathore
Hi Martin Abanes ,
We Cannot modify a collection while it is being iterated. We Have To Create New Similler List and add data on it. 
Because Trigger is For after insert, after update We Have To Use Upseart  The List.

i.e,

Trigger --
trigger AccountAssignment on Contact (after insert, after update) {
    
        List<Contact> conToInsert = [select id, accountid from contact  where id in: trigger.newmap.keyset()];
		List<Contact> conToInsert1 = new List<Contact>();

        Account defaultAccount = [SELECT ID FROM account Where ID  = '001i0000010asN9']; 
       
        //record id of Dummy Account is 001i0000010asN9
     
          for (Contact c : conToInsert) {            
                      
                if( c.accountid == NULL)
              {        
             
                c.accountid = defaultAccount.id;    
                 
               conToInsert1.add(c);           
            
               }           
        }  
    upsert conToInsert1;
        }
Regards
Gopal Rathore
Martin AbanesMartin Abanes
Thanks for the aid Arumkumar & Gopal, both of your code works. Till then guys, happy coding!.