Salesforce Trigger测试类

时间:2018-03-09 05:48:00

标签: triggers salesforce apex-code apex

下面是我的Apex触发器。我是一个初学者,并试图编写其测试类但不断收到错误" System.DmlException:插入失败。第0行的第一个例外;第一个错误:REQUIRED_FIELD_MISSING,错误:在您在产品相关列表中为此商机选择价格手册之前,您无法选择产品:[]"。

在商机上触发TrgrOptyHighestCustmorePrice(在插入之前,在更新之前) {

public Id oid;
public String bidType;
public String BUCode;

for(Opportunity o : trigger.new)
{
    oid = o.Id; 
    bidType = o.BidType__c;
    BUCode = o.Business_Line_BU__c;
}

List<OpportunityLineItem> oliList = new list<OpportunityLineItem>([SELECT id, Customer_Price__c, ReCat_Product_Line__c 
                                                               FROM OpportunityLineItem 
                                                               WHERE OpportunityId =: oid ORDER BY 
                                                               Customer_Price__c DESC LIMIT 1]);

for(OpportunityLineItem oli : oliList)
{
    if(bidType == 'Competitive' && oli.ReCat_Product_Line__c == 'DMS')
    {
        BUCode = 'BL.619';
    }
    if(bidType == 'Competitive' && (oli.ReCat_Product_Line__c == 'EMS' || oli.ReCat_Product_Line__c == 'GMS'))
    {
        BUCode = 'BL.620';
    }
    if(bidType == 'Competitive' && oli.ReCat_Product_Line__c == 'MMS')
    {
        BUCode = 'BL.622';
    }
    if(bidType == 'Sole Sourced' && oli.ReCat_Product_Line__c == 'DMS')
    {
        BUCode = 'BL.624';
    }
    if(bidType == 'Sole Sourced' && (oli.ReCat_Product_Line__c == 'EMS' || oli.ReCat_Product_Line__c == 'GMS'))
    {
        BUCode = 'BL.621';
    }
    if(bidType == 'Sole Sourced' && oli.ReCat_Product_Line__c == 'MMS')
    { 
        BUCode = 'BL.623';
    }        
}

for(Opportunity opt : trigger.new)
{
    opt.Business_Line_BU__c = BUCode;        
}    
}

测试类

@isTest(seeAllData=true)
public class Test_TrgrOptyHighestCustmorePrice {
    private static testmethod void TrgrOptyHighestCustmorePriceTest(){
        Test.startTest();
        //Insert a test product.
        Product2 p1 = new Product2(Name='Product Monthly 1111', isActive=true, CurrencyIsoCode='USD', ReCat_Product_Line__c = 'DMS');
        insert p1; 

        // Get standard price book ID.
        Id pricebookId = Test.getStandardPricebookId();

        // Insert a price book entry for the standard price book.
        PricebookEntry standardPrice = new PricebookEntry(
            Pricebook2Id = pricebookId, Product2Id = p1.Id,
            UnitPrice = 10000, IsActive = true);
        insert standardPrice;

        Pricebook2 customPB = new Pricebook2(Name='Custom Pricebook', isActive=true);
        insert customPB;

        PricebookEntry customPrice = new PricebookEntry(
            Pricebook2Id = customPB.Id, Product2Id = p1.Id,
            UnitPrice = 12000, IsActive = true);
        insert customPrice;

        // Insert Opportunity
        Opportunity opt = new Opportunity(Name='Test',StageName='Prospect', 
                                          CloseDate=date.today(),BidType__c = 'Competitive', 
                                          Business_Line_BU__c = 'BL.619', 
                                          PriceBook2 = customPB);
        insert opt;

        OpportunityLineItem optLI = new OpportunityLineItem(OpportunityId = opt.id, Product2Id = p1.Id);
        insert optLI;
        update opt;
        Test.stopTest();
    }

}

我无法理解如何测试我的简单触发器。

2 个答案:

答案 0 :(得分:0)

首先插入机会。 然后使用pricebookid更新商机。

G11

答案 1 :(得分:0)

因为你没有填写商机行项目的所有必填字段。有关必填字段,请参阅:https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_opportunitylineitem.htm

这个例子可行:

OpportunityLineItem optLI = new OpportunityLineItem(OpportunityId = opt.id, Product2Id = p1.Id, TotalPrice = 100, PricebookEntryId=customPrice.Id, Quantity =3);