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
Varun99Varun99 

Trigger after update

Hi,

   Am updating account object through data loader facing an error please help me

 

form433fupdate: execution of AfterUpdate

caused by: System.ListException: Duplicate id in list: a1B30000001oZDwEAM

Trigger.form433fupdate: line 34, column 1

 

 

trigger form433fupdate on Account (after update)
{
set<id> aset=new set<id>();
for(account a:trigger.new)
{
aset.add(a.id);
}
list<form433f__c> flist=new list<form433f__c>();
for(account a1:trigger.new)
{
for(form433f__c f:[select id,Scell__c,Shome__c,Swork__c,Ssecurity_No__c,E_Sbirthdate__c,Spouse_FullName__c,cell__c,Security_No__c,E_Birth__c,Home__c,work__c,Account__c,name,Personmailingcity__c,Personmailingpostalcode__c,Personmailingstate__c,Personmailingstreet__c from form433f__c where account__c in:aset])
{
f.name=a1.name;
f.county_of_Residence__c=a1.Personmailingcountry;
f.Personmailingcity__c=a1.Personmailingcity;
f.Personmailingpostalcode__c=a1.Personmailingpostalcode;
f.Personmailingstate__c=a1.Personmailingstate;
f.Personmailingstreet__c=a1.Personmailingstreet;
f.cell__c=a1.PersonMobilePhone;
f.Home__c =a1.PersonHomePhone;
f.work__c =a1.PersonOtherPhone;
f.Security_No__c =a1.SSN1__c;
f.E_Birth__c =a1.PersonBirthdate;
f.Ssecurity_No__c =a1.Spouse_SSN1__c;
f.E_Sbirthdate__c =a1.Spouse_Birthdate__c;
f.Spouse_FullName__c=a1.Spouse_FullName__c;
f.Scell__c = a1.Spouse_Cell_Phone__c;
f.Shome__c = a1.Spouse_Home_Phone__c;
f.Swork__c = a1.Spouse_Work_Phone__c;
flist.add(f);
}

}
update flist;
}

Best Answer chosen by Admin (Salesforce Developers) 
sivaextsivaext

hi 

try this

 

trigger form433fupdate on Account (after update)
{
set<id> aset=new set<id>();
for(account a:trigger.new)
{
aset.add(a.id);
}
list<form433f__c> flist=new list<form433f__c>();
for(account a1:trigger.new)
{
for(form433f__c f:[select id,Scell__c,Shome__c,Swork__c,Ssecurity_No__c,E_Sbirthdate__c,Spouse_FullName__c,cell__c,Security_No__c,E_Birth__c,Home__c,work__c,Account__c,name,Personmailingcity__c,Personmailingpostalcode__c,Personmailingstate__c,Personmailingstreet__c from form433f__c where account__c in:aset])
{

 if(a1.id==f.account__c)

{
f.name=a1.name;
f.county_of_Residence__c=a1.Personmailingcountry;
f.Personmailingcity__c=a1.Personmailingcity;
f.Personmailingpostalcode__c=a1.Personmailingpostalcode;
f.Personmailingstate__c=a1.Personmailingstate;
f.Personmailingstreet__c=a1.Personmailingstreet;
f.cell__c=a1.PersonMobilePhone;
f.Home__c =a1.PersonHomePhone;
f.work__c =a1.PersonOtherPhone;
f.Security_No__c =a1.SSN1__c;
f.E_Birth__c =a1.PersonBirthdate;
f.Ssecurity_No__c =a1.Spouse_SSN1__c;
f.E_Sbirthdate__c =a1.Spouse_Birthdate__c;
f.Spouse_FullName__c=a1.Spouse_FullName__c;
f.Scell__c = a1.Spouse_Cell_Phone__c;
f.Shome__c = a1.Spouse_Home_Phone__c;
f.Swork__c = a1.Spouse_Work_Phone__c;
flist.add(f);

}
}

}
update flist;
}

All Answers

Gunners_23Gunners_23

I guess you're trying to copy all the account details to another object and trying to update that. For that you've created a set which

 

contains all the unique IDs of accounts, But you're also trying to iterate at the same time.

 

Below for loop to iterate the account is the one which is creating the problem. Please remove the below for loop and try

 

for(account a1:trigger.new)
{

    

}

 

In the below SOQL query you're iterating the accounts. So iterating on Account is redundant

 

for(form433f__c f:[select id,Scell__c,Shome__c,Swork__c,Ssecurity_No__c,E_Sb

irthdate__c,Spouse_FullName__c,cell__c,Security_No__c,E_Birth__c,Home__c,work__c,Account__c,name,Personmailingcity__c,Personmailingpostalcode__c,Personmailingstate__c,Personmailingstreet__c from form433f__c where account__c in:aset])
{
f.name=a1.name;
f.county_of_Residence__c=a1.Personmailingcountry;
f.Personmailingcity__c=a1.Personmailingcity;
f.Personmailingpostalcode__c=a1.Personmailingpostalcode;
f.Personmailingstate__c=a1.Personmailingstate;
f.Personmailingstreet__c=a1.Personmailingstreet;
f.cell__c=a1.PersonMobilePhone;
f.Home__c =a1.PersonHomePhone;
f.work__c =a1.PersonOtherPhone;
f.Security_No__c =a1.SSN1__c;
f.E_Birth__c =a1.PersonBirthdate;
f.Ssecurity_No__c =a1.Spouse_SSN1__c;
f.E_Sbirthdate__c =a1.Spouse_Birthdate__c;
f.Spouse_FullName__c=a1.Spouse_FullName__c;
f.Scell__c = a1.Spouse_Cell_Phone__c;
f.Shome__c = a1.Spouse_Home_Phone__c;
f.Swork__c = a1.Spouse_Work_Phone__c;
flist.add(f);
}

 

Let me know if it didn't work

Varun99Varun99

Hi Can i remove the for loop am geeting an error

 

Error: Compile Error: Variable does not exist: a1.name at line 17 column 8

 

 

please help me

sivaextsivaext

hi 

try this

 

trigger form433fupdate on Account (after update)
{
set<id> aset=new set<id>();
for(account a:trigger.new)
{
aset.add(a.id);
}
list<form433f__c> flist=new list<form433f__c>();
for(account a1:trigger.new)
{
for(form433f__c f:[select id,Scell__c,Shome__c,Swork__c,Ssecurity_No__c,E_Sbirthdate__c,Spouse_FullName__c,cell__c,Security_No__c,E_Birth__c,Home__c,work__c,Account__c,name,Personmailingcity__c,Personmailingpostalcode__c,Personmailingstate__c,Personmailingstreet__c from form433f__c where account__c in:aset])
{

 if(a1.id==f.account__c)

{
f.name=a1.name;
f.county_of_Residence__c=a1.Personmailingcountry;
f.Personmailingcity__c=a1.Personmailingcity;
f.Personmailingpostalcode__c=a1.Personmailingpostalcode;
f.Personmailingstate__c=a1.Personmailingstate;
f.Personmailingstreet__c=a1.Personmailingstreet;
f.cell__c=a1.PersonMobilePhone;
f.Home__c =a1.PersonHomePhone;
f.work__c =a1.PersonOtherPhone;
f.Security_No__c =a1.SSN1__c;
f.E_Birth__c =a1.PersonBirthdate;
f.Ssecurity_No__c =a1.Spouse_SSN1__c;
f.E_Sbirthdate__c =a1.Spouse_Birthdate__c;
f.Spouse_FullName__c=a1.Spouse_FullName__c;
f.Scell__c = a1.Spouse_Cell_Phone__c;
f.Shome__c = a1.Spouse_Home_Phone__c;
f.Swork__c = a1.Spouse_Work_Phone__c;
flist.add(f);

}
}

}
update flist;
}

This was selected as the best answer
Gunners_23Gunners_23

Please use the below code

 

trigger form433fupdate on Account (after update)
{

set<id> aset=new set<id>();

list<form433f__c> flist=new list<form433f__c>();

Map<Id,Account> accountMap = new Map<Id,Account>();
for(account a:trigger.new)
{
aset.add(a.id);

accountMap.put(a.id,a);
}

for(form433f__c f:[select id,Scell__c,Shome__c,Swork__c,Ssecurity_No__c,E_Sb

irthdate__c,Spouse_FullName__c,cell__c,Security_No__c,E_Birth__c,Home__c,work__c,Account__c,name,Personmailingcity__c,Personmailingpostalcode__c,Personmailingstate__c,Personmailingstreet__c from form433f__c where account__c in:aset])
{

Account a1 = accountMap.get(f.account__c );
f.name=a1.name;
f.county_of_Residence__c=a1.Personmailingcountry;
f.Personmailingcity__c=a1.Personmailingcity;
f.Personmailingpostalcode__c=a1.Personmailingpostalcode;
f.Personmailingstate__c=a1.Personmailingstate;
f.Personmailingstreet__c=a1.Personmailingstreet;
f.cell__c=a1.PersonMobilePhone;
f.Home__c =a1.PersonHomePhone;
f.work__c =a1.PersonOtherPhone;
f.Security_No__c =a1.SSN1__c;
f.E_Birth__c =a1.PersonBirthdate;
f.Ssecurity_No__c =a1.Spouse_SSN1__c;
f.E_Sbirthdate__c =a1.Spouse_Birthdate__c;
f.Spouse_FullName__c=a1.Spouse_FullName__c;
f.Scell__c = a1.Spouse_Cell_Phone__c;
f.Shome__c = a1.Spouse_Home_Phone__c;
f.Swork__c = a1.Spouse_Work_Phone__c;
flist.add(f);
}

update flist;
}

Varun99Varun99

Hey,

        Thank you its working but i have a small doubt in system debug it showing in list same account name

many times more than child records and when update using data loader more than 100 child records in a particular record

is it throw an error?

 

Thank you for your fast replay.

sivaextsivaext

hi 

 

it not thrown any error more that 100 child records. because you queries are all outside for loop. 

 

trigger takes batch of 200 records. you are collecting 200 record ids in "set". you are writing a single query for getting all child records. 

Varun99Varun99

sorry for late responce yes it is also perfectly working.

 

Thank very much.