You need to sign in to do that
Don't have an account?
Martin 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);
}
}
}
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);
}
}
}
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,
All Answers
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.
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,
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 -- Regards
Gopal Rathore