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
Ryno LourensRyno Lourens 

Apex Class not saving - 'duplicate value found: <unknown> duplicates value on record with id: <unknown>'

I am trying to write an Apex test Class for a Trigger that I want to deploy. However, when I receive the 'duplicate value found' error:

@isTest
private class ProphixFieldsTest {
    static testMethod void testProphixFields() {
        // Create test data
        Opportunity opp = new Opportunity(Name='Test Opp', StageName='Prospecting', CloseDate=System.today().addDays(30), ForecastCategoryName='Pipeline');
        insert opp;
        Product2 prod = new Product2(Name='Test Product', ProductCode='123', Budget_Code__c='327 - RPD');
        insert prod;
        OpportunityLineItem oli = new OpportunityLineItem(OpportunityId=opp.Id, Product2Id=prod.Id, UnitPrice=100, Start_Date__c=System.today(), End_Date__c=System.today().addDays(30), Fiscal_Months__c=12);
        insert oli;

        // Test insert trigger
        opp = [SELECT Products_opp1__c, Product_Codes__c, Budget_Code__c, Amount, Current_Fiscal_Revenue__c, QuoteBeginDate__c, QuoteEndDate__c FROM Opportunity WHERE Id=:opp.Id];
        System.assertEquals(prod.Name, opp.Products_opp1__c);
        System.assertEquals(oli.ProductCode, opp.Product_Codes__c);
        System.assertEquals(oli.Budget_Code__c, opp.Budget_Code__c);
        System.assertEquals(oli.UnitPrice, opp.Amount);
        System.assertEquals(oli.Fiscal_Revenue__c, opp.Current_Fiscal_Revenue__c);
        System.assertEquals(oli.Start_Date__c, opp.QuoteBeginDate__c);
        System.assertEquals(oli.End_Date__c, opp.QuoteEndDate__c);

        // Test update trigger
        prod.Name = 'Test Product Updated';
        update prod;
        opp = [SELECT Products_opp1__c FROM Opportunity WHERE Id=:opp.Id];
        System.assertEquals(prod.Name, opp.Products_opp1__c);

        // Test delete trigger
        delete oli;
        opp = [SELECT Products_opp1__c FROM Opportunity WHERE Id=:opp.Id];
        System.assertEquals(null, opp.Products_opp1__c);
    }
}

Naveen KNNaveen KN
Are you getting this error when saving the class (as per the title) or while running this test class?
Ryno LourensRyno Lourens

When I run the class I receive the following error: 

System.DmlException: Insert failed. First exception on row 0; first error: STANDARD_PRICE_NOT_DEFINED, Before creating a custom price, create a standard price.: []

SubratSubrat (Salesforce Developers) 
Hello Ryno ,

The error message "STANDARD_PRICE_NOT_DEFINED, Before creating a custom price, create a standard price" is related to a Product2 record that you are trying to insert.

This error typically occurs when you try to insert a product without a standard price defined. In Salesforce, each product must have at least one standard price defined before you can create custom prices. To fix this error, you will need to create a standard price for the product before inserting it.

To create a standard price for the product, you can refer this discussion -> https://salesforce.stackexchange.com/questions/232265/create-standard-price-book-programmatically

Hope it helps !
Thank you.