+ Start a Discussion
cpo87cpo87 

System.ListException: Duplicate id in list

Hello All,

 

I'm receiving a 'Duplicate id in list' error.  I am trying to total the number of projects associated to an opportunity.  The join between these two is a lookup field on projects.

 

I am receiving this error...

 

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, trgOpp_FIProjectOnlyCount: execution of AfterInsert caused by: System.ListException: Duplicate id in list: 006S0000004dleLIAQ Trigger.trgOpp_FIProjectOnlyCount: line 21, column 5: []

 

Trigger

 

trigger trgOpp_FIProjectOnlyCount on SFDC_Project__c (after insert, after update) {
 //Find ID for 'File Integration Project - Document' and 'File Integration Project - Master' Record Type 
    Id RecType = [Select r.Id from RecordType r WHERE r.Name = 'File Integration Project - Document' AND r.SobjectType = 'SFDC_Project__c' Limit 1].Id;
	Id RecType2 = [Select r.Id from RecordType r WHERE r.Name = 'File Integration Project - Master' AND r.SobjectType = 'SFDC_Project__c' Limit 1].Id;
	
    List<Opportunity> opptys = new List<Opportunity>();
    integer pct;
    for(SFDC_Project__c p : trigger.new){
        if(p.RecordTypeId == RecType || p.RecordTypeId == RecType2){
            pct = [Select Count() FROM SFDC_Project__c p 
                                WHERE p.Opportunity__c = :p.Opportunity__c
                                AND p.Project_State__c != 'Completed'
                           ];

       Opportunity oid = [Select Count_of_FI_Projects__c from Opportunity WHERE id =:p.Opportunity__c ];
            oid.Count_of_FI_Projects__c = pct;
          
       opptys.add(oid);
       }
    }
    system.debug('\n' + opptys + '\n');
    Database.update(opptys);
}

 

 

UnitTest

 

@isTest
private class testOpp_FIProjectOnlyCount {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test
 
        Id RecType = [Select r.Id from RecordType r WHERE r.Name = 'File Integration Project - Document' AND r.SobjectType = 'SFDC_Project__c' Limit 1].Id;
		Id RecType2 = [Select r.Id from RecordType r WHERE r.Name = 'File Integration Project - Master' AND r.SobjectType = 'SFDC_Project__c' Limit 1].Id;     	
		List<SFDC_Project__c> projList = new List<SFDC_Project__c>();
		
        Account a = new Account();
        	a.Name = 'Test Vendor Account';
        	a.Type = 'Vendor';
        	a.Industry = 'Unknown';
        	a.ERP_Acct_App__c = 'Unknown';
        	Database.insert(a);
        	System.debug(a.ID);
			Account acct = [Select ID From Account a Limit 1];  
        	ID aid = acct.Id;
        Account b = new Account();
        	b.Name = 'Test Retailer Account';
        	b.Type = 'Buyer';
        	b.Industry = 'Unknown';
        	b.ERP_Acct_App__c = 'Unknown';
			Database.insert(b);
			Account acct2 = [Select ID From Account b Limit 1];
        	ID bid = acct2.Id;
        Campaign c = new Campaign();
        	c.Name = 'Test Campaign';
        	c.IsActive = True;
        	Database.insert(c);
        	Campaign cam = [Select ID From Campaign c Limit 1];
        	ID cid = cam.ID;
        Contact con = new Contact();
        	con.LastName = 'Test Last Name';
        	con.AccountId = aid;
        	Database.insert(con);
        	Contact cont = [Select ID From Contact con Limit 1];
        	ID conid = cont.ID;
        Opportunity mo = new Opportunity();
        	mo.CampaignId = cid;
        	mo.Name = 'Test Master Opportunity';
        	mo.LeadSource = 'Other';
        	mo.StageName = 'Suspect - 0%';
        	mo.CloseDate = system.today();
        	mo.AccountId = aid;
        	Database.insert(mo);
        	Opportunity o = [Select ID From Opportunity mo Limit 1];
        	ID moid = o.ID;       	
        SFDC_Project__c mproj = new SFDC_Project__c();
        	mproj.Name = 'Test Master Project';
        	mproj.Project_Type__c = 'File Integration';
        	mproj.SFDC_Project_Status__c = 'Not Started';
        	mproj.RecordTypeId = RecType2;
        	mproj.Account__c = aid;
        	mproj.Related_to_Account_c__c = bid;
        	mproj.Contact__c = conid;
        	mproj.Opportunity__c = moid;
        	Database.insert(mproj);
        	SFDC_Project__c proj = [Select ID From SFDC_Project__c mproj Limit 1];
        	ID mprojid = proj.ID;  	

        for(Integer i=0; i<21; i++){
        	SFDC_Project__c p = new SFDC_Project__c();
        		p.Name = 'Test Doc Project ' + i;
        		p.Parent_Implementation_Project__c = mprojid;
        		p.Project_Type__c = 'File Integration';
        		p.SFDC_Project_Status__c = 'Not Started';
        		p.RecordTypeId = RecType;
        		p.Account__c = aid;
        		p.Related_to_Account_c__c = bid;
        		p.Contact__c = conid;
        		p.Opportunity__c = moid;
        		projList.add(p);
        }
        Database.insert(projList);
        System.assert(mo.Count_of_FI_Projects__c == 22); 
    }
}

 

 

Any suggestions?

 

Thanks,

 

Christian

 

 

gtindugtindu

Use a Set<Opportunity> instead of List<Opportunity>

forecast_is_cloudyforecast_is_cloudy

Note that your current code is not bulkified (since it includes SOQL statements inside a for loop) and may blow up with governor limit exceptions if someone inserts/updates multiple SFDC_Project__c records (via say the Data Loader). I would strongly recommend that you refactor your code to make it bulk safe. As a side benefit, your duplicate ID issue will also be fixed.