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
ajay ambatiajay ambati 

updating multiple records from child to parent in anonymous window hlp me plssss

iam executing this code in anonymous block iam getting error like my requirment is only in anonumos window
Line: 35, Column: 1
System.ListException: Duplicate id in list: 00128000003LLRkAAO
OK
pls hlp me
:*the following below  is my code:*
list<account> lst1 = new list<account>();
 list<schema.contact> lst = [select id,name,account.name,account.Fax from contact where account.Fax != null];
for(schema.contact c:lst)
{
   system.debug('DISPLAY'+c.account.name); 
       system.debug('@@@@@@'+c.account.Fax);
    //Account ac = [select id,name,fax from account where id=:c.account.id];
  c.account.Fax='040-567-234';
    
   lst1.add(c.account);
}  
update lst1;
 
Best Answer chosen by ajay ambati
Amit Chaudhary 8Amit Chaudhary 8
Please try below code.
list<account> lst1 = new list<account>();
list<schema.contact> lst = [select id,name,accountid,account.name,account.Fax from contact where account.Fax != null];
Set<Id> setAccId = new Set<Id>(); 

for(schema.contact c : lst )
{
	if( setAccId.contains(c.accountid) == false)
	{
		lst1.add(new Account(Id = c.accountId, Fax = '040-567-234'));
		setAccId.add(c.accountid);
	}	
}  
if(lst1.size() > 0 )
{
	update lst1;
}

 

All Answers

Srinivas SSrinivas S
list<account> lst1 = new list<account>();
 list<schema.contact> lst = [select id,name,account.name,account.Fax from contact where account.Fax != null];
for(schema.contact c:lst)
{
   system.debug('DISPLAY'+c.account.name); 
       system.debug('@@@@@@'+c.account.Fax);
    //Account ac = [select id,name,fax from account where id=:c.account.id];    
   lst1.add(new Account(Id = c.accountId, Fax = '040-567-234'));
}  
update lst1;
You can update the account as mentioned above.

------------
Thanks,
Srinivas
- Please mark as solution if your problem is resolved.
ajay ambatiajay ambati
same error bro
Line: 36, Column: 1
System.ListException: Duplicate id in list: 00128000003LLRkAAO
Amit Chaudhary 8Amit Chaudhary 8
Please update your code like below
list<account> lst1 = new list<account>();
list<schema.contact> lst = [select id,name,accountid,account.name,account.Fax from contact where account.Fax != null];
Set<Id> setAccId = new Set<Id>(); // use set to avoid duplicate record.

for(schema.contact c:lst)
{
	if( setAccId.contains(accountid) == false)
	{
		lst1.add(new Account(Id = c.accountId, Fax = '040-567-234'));
		lst1.add(c.account);
		setAccId.add(accountid);
	}	
}  
if(lst1.size() > 0 )
{
	update lst1;
}
Let us know if this will help you
 
ajay ambatiajay ambati
again it getting the follwing error
Line: 51, Column: 3
Incompatible element type Schema.SObjectField for collection of Id
ajay ambatiajay ambati
below error getiing pls jst once check in your access and send me code

Line: 51, Column: 16
Variable does not exist: accountid
Amit Chaudhary 8Amit Chaudhary 8
Please try below code.
list<account> lst1 = new list<account>();
list<schema.contact> lst = [select id,name,accountid,account.name,account.Fax from contact where account.Fax != null];
Set<Id> setAccId = new Set<Id>(); 

for(schema.contact c : lst )
{
	if( setAccId.contains(c.accountid) == false)
	{
		lst1.add(new Account(Id = c.accountId, Fax = '040-567-234'));
		setAccId.add(c.accountid);
	}	
}  
if(lst1.size() > 0 )
{
	update lst1;
}

 
This was selected as the best answer
ajay ambatiajay ambati
tqu chaudary brooo do u know telugu?