+ Start a Discussion
SeAlVaSeAlVa 

Odd behavior in Test Classes

Hi there!, 

I'm having an exception (CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY) in my code when I change from this code
public void recalculateAccountSegmentation(List<Opportunity> lOppty){
    List <Account> accounts = new List <Account>();
    List <Account> acctoupdate = new List <Account>();
    
    for(Opportunity o: lOppty){
        accounts.add(o.Account);        
    }
    acctoupdate = [Select Id from Account where Id IN: accounts];
    update acctoupdate;
}

to this one
public void recalculateAccountSegmentation(List<Opportunity> lOppty){
    Set <Account> accountsSet = new Set <Account>();
    for(Opportunity o: lOppty){
        accountsSet.add(new Account(ID=o.AccountID));
    }
    update new List<Account>(accountsSet);
}

What might be the explanation? (that is the ONLY thing I am changing, and tests are running OK without this change)

Thanks in advance
Best Answer chosen by SeAlVa
SeAlVaSeAlVa
No luck, but found the issue.

public void recalculateAccountSegmentation(List<Opportunity> lOppty){
    Set <Account> accountsSet = new Set <Account>();
    for(Opportunity o: lOppty){
        accountsSet.add(new Account(ID=o.AccountID));
    }
    accountsSet.remove(new Account(ID=null));
    update new List<Account>(accountsSet);
}

Thanks for the quick reply, anyway.

All Answers

Ramu_SFDCRamu_SFDC
Rather than doing an update directly follow this procedure

List<Account> Acctlist = new List<Account>(accountsSet);
udpdate acctlist'

Let me know the results.
SeAlVaSeAlVa
No luck, but found the issue.

public void recalculateAccountSegmentation(List<Opportunity> lOppty){
    Set <Account> accountsSet = new Set <Account>();
    for(Opportunity o: lOppty){
        accountsSet.add(new Account(ID=o.AccountID));
    }
    accountsSet.remove(new Account(ID=null));
    update new List<Account>(accountsSet);
}

Thanks for the quick reply, anyway.
This was selected as the best answer