Skip to main content
Ryno Lourens (Nlogic) 님이 #Apex에 질문했습니다

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);

    }

}
답변 3개
  1. 2023년 3월 22일 오후 5:13
    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.
0/9000