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
Parantap SrivastavParantap Srivastav 

Hi! I need to write a trigger on contact to update the account name field in contact to be the last name of contact concatenated with the account name of the contact. Now when I write my trigger, trigger.new is only going through one contact.

Here is my code: 
trigger TrigUpAcc on Contact (after insert,after update) 
{
    System.debug('Trigger.New has: '+Trigger.New); //to see the trigger.New records 
    List<Id> accIdL = new List<id>();
    integer i = 0;
    for(Contact c : Trigger.new)
    {
        if (c.AccountId != null)
        {
            System.debug('The loop is running '+ i +'times'); // to count the records in Trigger.New 
            accIdL.add(c.AccountId);
            i++;
            System.debug('The contact is: '+c); // To get the contact
        }
    }
    
    System.debug('size of the id list is: '+accIdL.size()); //just another check of the record list
    
    List<Contact> lstConUpdate = new List<Contact>();
    List<Account> accL = [Select Id, Name,(Select LastName From Contacts)  From Account WHERE Id in :accIdL];
    System.debug('Size of accL is :  ' +accL.size()); 
    for(Account acc : accL)
    {
        string accName = ''; 
        for(Contact c : acc.Contacts)
        {
            accName = acc.name + c.LastName; 
            acc.Name = accName;
            lstConUpdate.add(c);
            
        }
        
    }
    if(lstConUpdate.size() > 0)
    {
        Update lstConUpdate;   
    }
    
}
The code runs but does not make the required changes. making the debugging shows that all records in trigger,new are not being accessed. Kinldy help me out, I am stuck here.




 
Raj VakatiRaj Vakati
Try this code
 
trigger TrigUpAcc on Contact (after insert,after update) 
{
    Map<Id,Name> accIdL = new Map<id,Name>();
    for(Contact c : Trigger.new)
    {
        if (c.AccountId != null)
        {
            accIdL.put(c.AccountId ,c.LastName);
        }
    }
    
    
    List<Contact> lstConUpdate = new List<Contact>();
    List<Account> accL = [Select Id, Name  From Account WHERE Id in :accIdL.keySet()];
    for(Account acc : accL)
    {
            acc.Name = acc.Name+accIdL.get(acc.Id);
        
    }
    if(accL.size() > 0)
    {
        Update accL;   
    }
	}

 
Parantap SrivastavParantap Srivastav
Thank you Rajmohan for the reply! I tried your code and it gave me an error saying 'Method does not exist or incorrect signature: void put(Id, String) from the type Map'. I havent used map yet, so I dont know what went wrong. ----- /* accIdL.put(c.AccountId,c.LastName); */ --------------is the line that got the error. Also Could you figure out what went wrong with my code? I would really appreciate that. Because I dont know why my trigger.new is only getting 1 record from Contact object. Thank you
Raj VakatiRaj Vakati
My bad .. try this code
 
trigger TrigUpAcc on Contact (after insert,after update) 
{
    Map<Id,String> accIdL = new Map<id,String>();
    for(Contact c : Trigger.new)
    {
        if (c.AccountId != null)
        {
            accIdL.put(c.AccountId ,c.LastName);
        }
    }
    
    
    List<Contact> lstConUpdate = new List<Contact>();
    List<Account> accL = [Select Id, Name  From Account WHERE Id in :accIdL.keySet()];
    for(Account acc : accL)
    {
            acc.Name = acc.Name+accIdL.get(acc.Id);
        
    }
    if(accL.size() > 0)
    {
        Update accL;   
    }
}

 
Parantap SrivastavParantap Srivastav
Thank you for this, Raj.  I also did that exactly the same way. Just removed the contact loop as well and changed the map parameter to string instead of name. 
Raj VakatiRaj Vakati
Great! close this thread