You need to sign in to do that
Don't have an account?
Parantap 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.
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 Vakati
Try this code
Parantap 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 Vakati
My bad .. try this code
Parantap 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 Vakati
Great! close this thread