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
Proposal ButtonProposal Button 

troble while deploying a trigger

Hi All

 

I had wrote atrigger and facing difficult in deploying. The think is it is trowing an error in an different apex test class. please have a look at my trigger(Update account) and the error is showing in an different apex test class(Test_Opportunitytoorder) which i mentioned in Red

 

trigger UpdateAccount on Opportunity (after update)
{
List<id> accIds=new List<id>();
for (Opportunity opp : trigger.new)
{
   accIds.add(opp.accountid);
}
Map<Id, Account> accountsById=new Map<Id, Account>();
List<Account> accs= [select id,Ready_to_Wincare__c from Account where id in :accIds];
accountsById.putAll(accs);
List<Account> accsToUpdate=new List<Account>();
for (Opportunity opp : trigger.new)
{
   if (opp.StageName ==  'Closed Won')
   {  
       Account acc=accountsById.get(opp.AccountId);
       acc.Ready_to_Wincare__c = True;
       accsToUpdate.add(acc);
          }
}
update accsToUpdate;
}

 

Errors

Test_OpportunityToOrder.testOpportunityToOrder System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UpdateAccount: execution of AfterUpdate
caused by: System.ListException: Duplicate id in list: 0014000000XtFyyAAF
Trigger.UpdateAccount: line 22, column 1: []

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
forecast_is_cloudyforecast_is_cloudy

I think that this should fix the issue..

 

 

trigger UpdateAccount on Opportunity (after update)
{
List<id> accIds=new List<id>();
for (Opportunity opp : trigger.new)
{
   accIds.add(opp.accountid);
}

Map<Id, Account> accountsById= new Map<Id, Account>([select id,Ready_to_Wincare__c from Account where id in :accIds]);

List<Account> accsToUpdate=new List<Account>();
Set<ID> accsIdsToUpdate=new Set<ID>();

for (Opportunity opp : trigger.new)
{
   if (opp.StageName ==  'Closed Won' && 
       !accsIdsToUpdate.contains(opp.AccountId))
   {  
       Account acc=accountsById.get(opp.AccountId);
       acc.Ready_to_Wincare__c = True;
       accsToUpdate.add(acc);
       accsIdsToUpdate.add(opp.AccountId);
}
update accsToUpdate;
}

 

 

All Answers

aalbertaalbert

Make sure you don't have duplicate Accounts being updated in the same Update DML statement. 

To troubleshoot, System.debug() the accountIds being updated before you update to see if you have duplicates. 

forecast_is_cloudyforecast_is_cloudy

I think that this should fix the issue..

 

 

trigger UpdateAccount on Opportunity (after update)
{
List<id> accIds=new List<id>();
for (Opportunity opp : trigger.new)
{
   accIds.add(opp.accountid);
}

Map<Id, Account> accountsById= new Map<Id, Account>([select id,Ready_to_Wincare__c from Account where id in :accIds]);

List<Account> accsToUpdate=new List<Account>();
Set<ID> accsIdsToUpdate=new Set<ID>();

for (Opportunity opp : trigger.new)
{
   if (opp.StageName ==  'Closed Won' && 
       !accsIdsToUpdate.contains(opp.AccountId))
   {  
       Account acc=accountsById.get(opp.AccountId);
       acc.Ready_to_Wincare__c = True;
       accsToUpdate.add(acc);
       accsIdsToUpdate.add(opp.AccountId);
}
update accsToUpdate;
}

 

 

This was selected as the best answer
Proposal ButtonProposal Button

Thankyou ver muck..the code has fixed my issue. Appreciate your help

 

Thanks

Sree