• Ali Shb
  • NEWBIE
  • 10 Points
  • Member since 2022
  • SAlesforce Developer

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 0
    Replies
trigger OppTrigger on Opportunity (after insert)
{
    List<opportunity> addRecods = new List<Opportunity>();
    for(Opportunity opp: Trigger.new)
    {
        addRecods.add(opp.AccountId);
    }

}
why Am I facing this issue?
(AddOppInfoTrigger: execution of AfterInsert caused by: System.ListException: Duplicate id in list: a045h00000zJB2rAAG Trigger.AddOppInfoTrigger: line 80, column 1)

trigger AddOppInfoTrigger on Opportunity (After insert)
{
         
         List<Opportunity_Info__c> infoUpdate = new List<Opportunity_Info__c>();
         List<Opportunity_Info__c> infoInsert = new List<Opportunity_Info__c>();
    
         List<Account> addAccountRecods = new List<Account>();
         for(Opportunity getOpp: Trigger.new)
         {
             Account getRelatedRecords = [select id, name, (SELECT Id, Name, Stage_List__c, Amount FROM Opportunities) From Account Where id = :getOpp.AccountId];
             addAccountRecods.add(getRelatedRecords);
         }
         for(Account acc: addAccountRecods) 
         {
             List<opportunity> getOpp = acc.opportunities;
             for(opportunity checkOpp: getOpp)
             {
                 if(checkOpp.Stage_List__c == 'new' || (checkOpp.Stage_List__c == 'In Progress' || checkOpp.Stage_List__c == 'Negotiation'))
                 {
                     List<Opportunity_Info__c> getQuerry = [select id, Name,  Account__c, Amount__c, Counter__c from Opportunity_Info__c Where (Name = :checkOpp.Stage_List__c AND Account__c = :acc.Id) limit 1];
                     
                     for(Opportunity_Info__c oppInfo: getQuerry)
                          {
                              
                             if(getQuerry.size() <= 0)
                              {   
                                    Opportunity_Info__c newInfoInsert = new Opportunity_Info__c();
                                  newInfoInsert.Name = checkOpp.Stage_List__c;
                                  newInfoInsert.Counter__c =  newInfoInsert.Counter__c + 1;
                                  newInfoInsert.Amount__c =   newInfoInsert.Amount__c + checkOpp.Amount;
                                  newInfoInsert.Account__c = acc.Id;
                                  
                                  infoInsert.add(newInfoInsert);
                              }
                             else
                             {
                                 oppInfo.Name = checkOpp.Stage_List__c;
                                      oppInfo.Counter__c  = oppInfo.Counter__c + 1;
                                      oppInfo.Amount__c = oppInfo.Amount__c + checkOpp.Amount;
                                      
                                      infoUpdate.add(oppInfo);
                            }
                               
                          }                      
                 }
                 else
                 {
                     List<Opportunity_Info__c> getQuerry = [select id, Name,  Account__c, Amount__c, Counter__c from Opportunity_Info__c Where (Name = :checkOpp.Stage_List__c AND Account__c = :acc.Id) limit 1];
                     
                     for(Opportunity_Info__c oppInfo: getQuerry)
                          {
                              
                             if(getQuerry.size() <= 0)
                              {   
                                    Opportunity_Info__c newInfoInsert = new Opportunity_Info__c();
                                  newInfoInsert.Name = checkOpp.Stage_List__c;
                                  newInfoInsert.Counter__c =  newInfoInsert.Counter__c + 1;
                                  newInfoInsert.Amount__c =   newInfoInsert.Amount__c + checkOpp.Amount;
                                  newInfoInsert.Account__c = acc.Id;
                                  
                                  infoInsert.add(newInfoInsert);
                              }
                             else
                             {
                                 oppInfo.Name = checkOpp.Stage_List__c;
                                      oppInfo.Counter__c  = oppInfo.Counter__c + 1;
                                      oppInfo.Amount__c = oppInfo.Amount__c + checkOpp.Amount;
                                      
                                      infoUpdate.add(oppInfo);
                            }
                               
                          } 
                 }
                     
             }
             
         }
                    if(infoUpdate.size() > 0)
                    {
                        update infoUpdate;                       
                    }
                    
                    if(infoInsert.size() > 0)
                    {
                        insert infoInsert;                        
                    }
}