+ Start a Discussion
Avinash Dhanke 32Avinash Dhanke 32 

System.ListException: Duplicate id in list: 0016F000030h1SbQAI: Trigger.updateAccountIfOppCustomer: line 32, column 1

trigger updateAccountIfOppCustomer on Opportunity (after insert,after update) {
    List<Account>accToupdate=new List<Account>();
    List<Opportunity>opps = new List<Opportunity>();
    set<id>associatedAccid = new set<id>();
    for(Opportunity opp :trigger.new){
        associatedAccid.add(opp.AccountId);
        System.debug('associatedAccid'+opp.AccountId);
    }
    //Map<id,Account>Accmap=new Map<id,Account>
    opps =[Select id,Accountid,StageName,Account.Type From Opportunity Where Accountid =:associatedAccid];
    System.debug('Opportunity' +opps);
    for(Opportunity opp:opps){
        if(opp.StageName=='Closed Won-One Time'||opp.StageName=='Closed Won-Recurring'||opp.StageName=='Customer Reseller'){
            opp.Account.Type='Customer-Direct';
            accToupdate.add(opp.Account);
            System.debug('accToupdate'+opp.Account);
        }
        else{
            opp.Account.Type='Prospect';
           accToupdate.add(opp.Account);
        }
    }
    update accToupdate;
}
Raj VakatiRaj Vakati
Is its complete code ?? You code dnt have line 32 ???

trigger.updateAccountIfOppCustomer: line 32, column 1

try this code
 
trigger updateAccountIfOppCustomer on Opportunity (after insert,after update) {
    List<Account> accToupdate=new List<Account>();
    List<Opportunity> opps = new List<Opportunity>();
    set<id> associatedAccid = new set<id>();
    for(Opportunity opp :trigger.new){
        associatedAccid.add(opp.AccountId);
        System.debug('associatedAccid'+opp.AccountId);
    }
    //Map<id,Account>Accmap=new Map<id,Account>
    opps =[Select id,Accountid,StageName,Account.Type From Opportunity Where Accountid =:associatedAccid];
	Set<Id> checkforDUps = new Set<Id>() ; 
	
    System.debug('Opportunity' +opps);
    for(Opportunity opp:opps){
		if(!checkforDUps.contains(opp.Account)){
        if(opp.StageName=='Closed Won-One Time'||opp.StageName=='Closed Won-Recurring'||opp.StageName=='Customer Reseller'){
            opp.Account.Type='Customer-Direct';
            accToupdate.add(opp.Account);
            System.debug('accToupdate'+opp.Account);
        }
        else{
            opp.Account.Type='Prospect';
           accToupdate.add(opp.Account);
        }
		}else{
			checkforDUps.put(opp.AccountId);
		}
    }
    update accToupdate;
}

 
Avinash Dhanke 32Avinash Dhanke 32
@ravi vakati when i replace my code with your suggested code iline no 15 and 26 give in an this error
'Method does not exist or incorrect signature: void contains(Account) from the type Set<Id>'
Raj VakatiRaj Vakati
Try this code

 
trigger updateAccountIfOppCustomer on Opportunity (after insert,after update) {
    List<Account> accToupdate=new List<Account>();
    List<Opportunity> opps = new List<Opportunity>();
    set<id> associatedAccid = new set<id>();
    for(Opportunity opp :trigger.new){
        associatedAccid.add(opp.AccountId);
        System.debug('associatedAccid'+opp.AccountId);
    }
    //Map<id,Account>Accmap=new Map<id,Account>
    opps =[Select id,Accountid,StageName,Account.Type From Opportunity Where Accountid =:associatedAccid];
    Set<Id> checkforDUps = new Set<Id>() ; 
    
    System.debug('Opportunity' +opps);
    for(Opportunity opp:opps){
        if(!checkforDUps.contains(opp.AccountId)){
            if(opp.StageName=='Closed Won-One Time'||opp.StageName=='Closed Won-Recurring'||opp.StageName=='Customer Reseller'){
                opp.Account.Type='Customer-Direct';
                accToupdate.add(opp.Account);
                System.debug('accToupdate'+opp.Account);
            }
            else{
                opp.Account.Type='Prospect';
                accToupdate.add(opp.Account);
            }
        }else{
            checkforDUps.add(opp.AccountId);
        }
    }
    update accToupdate;
}

 
Avinash Dhanke 32Avinash Dhanke 32
@raj vakati when i try updated code and check still same error giving to mi 'System.ListException: Duplicate id in list: 0016F000030h1NpQAI'
Raj VakatiRaj Vakati
Try this code
trigger updateAccountIfOppCustomer on Opportunity (after insert,after update) {
    List<Account> accToupdate=new List<Account>();
    List<Opportunity> opps = new List<Opportunity>();
    set<id> associatedAccid = new set<id>();
    for(Opportunity opp :trigger.new){
        associatedAccid.add(opp.AccountId);
    }
    //Map<id,Account>Accmap=new Map<id,Account>
    opps =[Select id,Accountid,StageName,Account.Type From Opportunity Where Accountid IN :associatedAccid];
    Set<Id> checkforDUps = new Set<Id>() ; 
    Set<Id> custStype = new Set<Id>() ; 
    Set<Id> prosepType = new Set<Id>() ; 
    
    System.debug('Opportunity' +opps);
    for(Opportunity opp:opps){
        Account acc = opp.Account ;
        if(opp.StageName=='Closed Won-One Time'||opp.StageName=='Closed Won-Recurring'||opp.StageName=='Customer Reseller'){
            custStype.add(opp.AccountId);
        }
        else{
            prosepType.add(opp.AccountId);
        }
        
    }
    List<Account> accs = [Select Id , Type from Account where Id in :prosepType ] ; 
    for(Account a :accs){
        a.Type='Prospect';
    }
    update accs ; 
    
    List<Account> accs1 = [Select Id , Type from Account where Id in :custStype ] ; 
    for(Account a :accs1){
        a.Type='Customer-Direct';
    }
    update accs1 ; 
    
    
    
    //  update accToupdate;
}

 
Avinash Dhanke 32Avinash Dhanke 32
@Raj Vakati  when we create the new opportunity under account and when the account Type is Prospect and Set the stagename of opportunity is Customer Reseller the update proper value That is Customer-Direct But When iterchaange and update then this is not work, and My trigger Scenario is follow.

Create a triger  that runs whenever an opportuntity is Created  or updated
The Trigger needs to check all the other Opportunities related to the account
of the opportunity being updated.itshould check to see if any opportunities have  a
StageName equals to 'Closedwon-One Time' or 'Closed won-Recurring' and if so,it should
update the account type to 'Customer'.if  none of the Opportunities are closed won,
the Account Tye is 'Prospect'
Amit Chaudhary 8Amit Chaudhary 8
try to update your code like below
trigger updateAccountIfOppCustomer on Opportunity (after insert,after update) {

    List<Account>accToupdate=new List<Account>();
    List<Opportunity>opps = new List<Opportunity>();

    set<id>associatedAccid = new set<id>();
    for(Opportunity opp :trigger.new){
        associatedAccid.add(opp.AccountId);
        System.debug('associatedAccid'+opp.AccountId);
    }
    //Map<id,Account>Accmap=new Map<id,Account>
	
    opps =[Select id,Accountid,StageName,Account.Type From Opportunity Where Accountid =:associatedAccid];
	
    System.debug('Opportunity' +opps);
	
	Set<Id> setAccId = new Set<Id>();
    for(Opportunity opp : opps){
		if( setAccId.contains(opp.Accountid) == false )
		{
			if(opp.StageName=='Closed Won-One Time'||opp.StageName=='Closed Won-Recurring'||opp.StageName=='Customer Reseller'){
				opp.Account.Type='Customer-Direct';
				accToupdate.add(opp.Account);
				System.debug('accToupdate'+opp.Account);
			}
			else{
				opp.Account.Type='Prospect';
				accToupdate.add(opp.Account);
			}
			setAccId.add(opp.Accountid);
		}
    }
    update accToupdate;
}

But i will suggest you to implements changes like below
1) Set Account.Type='Prospect' as default and update Account.Type='Customer-Direct'  when opp closed. like below
2) Update your code like below
trigger updateAccountIfOppCustomer on Opportunity (after insert,after update) {

    Set<id> associatedAccid = new Set<id>();
    for(Opportunity opp :trigger.new){
		if(opp.StageName=='Closed Won-One Time'||opp.StageName=='Closed Won-Recurring'||opp.StageName=='Customer Reseller'){
			associatedAccid.add(opp.AccountId);
		}	
    }
	
    List<Account> lstAcc = [Select id,Type From Account Where Id in :associatedAccid ];
    for(Account acc : lstAcc){
		acc.Type='Customer-Direct';
    }
    update lstAcc;
	
}

Let us know if this will help you