• MAITREYEE DINGARE 3
  • NEWBIE
  • 10 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 2
    Replies
Hi, 
I have an APEX Class that is updating a list and later I am presenting that list via LWC on the Lightning Record Page. 
I would like to write a test Class for the APEX Class. 
APEX Class:
public class PriceBookEntriesFromAccount {
    
    @AuraEnabled
    public static List<BB_Produkt__c> getPriceBookEntries(String artId) {
        List<BB_Produkt__c> bb = 
            [SELECT Name, Id, Berichsartikel__c, Berichsartikel_Copy__c, Produkt__r.Name, Verkostung__c, Kaufinteresse__c, Unternehmen__r.Name, CreatedDate 
             FROM BB_Produkt__c 
             WHERE Unternehmen__r.Id = :[SELECT Unternehmen__r.Name FROM Besuchsbericht__c WHERE Id =:artId].Unternehmen__r.Id
            ORDER BY CreatedDate DESC];
        //Add isAccessible() check
        return bb;
    }
    
    @AuraEnabled
	public static void updateRelatedList(List<BB_Produkt__c> Bblist)
    {
        if(Bblist!= null && Bblist.size()>0)
        {
            update Bblist;
            Bblist.sort();
        }
    }
}
So far this is what I have written, but I am getting 0% code coverage:
@isTest(SeeAllData=true)
public class testPriceBookEntriesFromAccount {
    
    public static testMethod void testBB_Produkt()
	{

		Account createAccount =new Account(Name='TestA');
		insert createAccount;
        
        Product2 createProduct2 =new Product2(Name='TestP');
		insert createProduct2;

		BB_Produkt__c createBB_Produkt =new BB_Produkt__c(Produkt__c=createProduct2.ID, Unternehmen__c=createAccount.Id);
		insert createBB_Produkt;

		createBB_Produkt.Feedback_Verkostung_1_5__c = 2;
		update createBB_Produkt;
	}
}


 
Hello All,
I have written a Trigger on OrderItems (before Insert) and now writing a test class for the same. 
I am getting an error as:
System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Pricebook Not Set on Order: []

My Trigger:
trigger UnitPrice on OrderItem (before insert) {

    for (OrderItem orderItem: Trigger.new) {
    if(orderItem.UnitPrice == NULL){
            orderItem.UnitPrice= 0; // Dummy Value
            }
    }

}

Test Class:
@isTest(SeeAllData=true)
public class testUnitPrice {
    
    public static testMethod void testorderproduct(){

    // Insert Account

    Account a = new Account();
    a.Name = 'Test Account';
    insert a;

    // Insert Product
    Product2 p = new Product2();
    p.Name = ' Test Product ';
    p.Description='Test Product Entry 1';
    p.productCode = 'ABC';
    p.isActive = true;
    insert p;
    

    Pricebook2  standardPb = [select id, name, isActive from Pricebook2 where IsStandard = true limit 1];
    
    // Insert PricebookEntry

    PricebookEntry standardPrice = new PricebookEntry();
    standardPrice.Pricebook2Id = standardPb.Id;
    standardPrice.Product2Id = p.Id;
    standardPrice.UnitPrice = 1;
    standardPrice.IsActive = true;
    standardPrice.UseStandardPrice = false;
    insert standardPrice ;
    
    // Insert Order
    
    Order o = new Order();
    o.Name = 'Test Order ';
    o.Status = 'Draft';
    o.EffectiveDate = system.today();
    o.EndDate = system.today() + 4;
    o.AccountId = a.id;
    o.Pricebook2Id =  standardPb.Id ;
    insert o;
    
    // Insert Order Item

    OrderItem i = new OrderItem();
    i.OrderId = o.id;
    i.Quantity = 5;
    i.UnitPrice = 0;
    i.Product2id = p.id;
    i.PricebookEntryId=standardPrice.Id;
    insert i;

	}
}

I am very new to programming. Any help would be appreciated.

Thank you!
Maitreyee Dingare​​​​​​​​​​​​​​
Hi, 
I have an APEX Class that is updating a list and later I am presenting that list via LWC on the Lightning Record Page. 
I would like to write a test Class for the APEX Class. 
APEX Class:
public class PriceBookEntriesFromAccount {
    
    @AuraEnabled
    public static List<BB_Produkt__c> getPriceBookEntries(String artId) {
        List<BB_Produkt__c> bb = 
            [SELECT Name, Id, Berichsartikel__c, Berichsartikel_Copy__c, Produkt__r.Name, Verkostung__c, Kaufinteresse__c, Unternehmen__r.Name, CreatedDate 
             FROM BB_Produkt__c 
             WHERE Unternehmen__r.Id = :[SELECT Unternehmen__r.Name FROM Besuchsbericht__c WHERE Id =:artId].Unternehmen__r.Id
            ORDER BY CreatedDate DESC];
        //Add isAccessible() check
        return bb;
    }
    
    @AuraEnabled
	public static void updateRelatedList(List<BB_Produkt__c> Bblist)
    {
        if(Bblist!= null && Bblist.size()>0)
        {
            update Bblist;
            Bblist.sort();
        }
    }
}
So far this is what I have written, but I am getting 0% code coverage:
@isTest(SeeAllData=true)
public class testPriceBookEntriesFromAccount {
    
    public static testMethod void testBB_Produkt()
	{

		Account createAccount =new Account(Name='TestA');
		insert createAccount;
        
        Product2 createProduct2 =new Product2(Name='TestP');
		insert createProduct2;

		BB_Produkt__c createBB_Produkt =new BB_Produkt__c(Produkt__c=createProduct2.ID, Unternehmen__c=createAccount.Id);
		insert createBB_Produkt;

		createBB_Produkt.Feedback_Verkostung_1_5__c = 2;
		update createBB_Produkt;
	}
}


 
Hello All,
I have written a Trigger on OrderItems (before Insert) and now writing a test class for the same. 
I am getting an error as:
System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Pricebook Not Set on Order: []

My Trigger:
trigger UnitPrice on OrderItem (before insert) {

    for (OrderItem orderItem: Trigger.new) {
    if(orderItem.UnitPrice == NULL){
            orderItem.UnitPrice= 0; // Dummy Value
            }
    }

}

Test Class:
@isTest(SeeAllData=true)
public class testUnitPrice {
    
    public static testMethod void testorderproduct(){

    // Insert Account

    Account a = new Account();
    a.Name = 'Test Account';
    insert a;

    // Insert Product
    Product2 p = new Product2();
    p.Name = ' Test Product ';
    p.Description='Test Product Entry 1';
    p.productCode = 'ABC';
    p.isActive = true;
    insert p;
    

    Pricebook2  standardPb = [select id, name, isActive from Pricebook2 where IsStandard = true limit 1];
    
    // Insert PricebookEntry

    PricebookEntry standardPrice = new PricebookEntry();
    standardPrice.Pricebook2Id = standardPb.Id;
    standardPrice.Product2Id = p.Id;
    standardPrice.UnitPrice = 1;
    standardPrice.IsActive = true;
    standardPrice.UseStandardPrice = false;
    insert standardPrice ;
    
    // Insert Order
    
    Order o = new Order();
    o.Name = 'Test Order ';
    o.Status = 'Draft';
    o.EffectiveDate = system.today();
    o.EndDate = system.today() + 4;
    o.AccountId = a.id;
    o.Pricebook2Id =  standardPb.Id ;
    insert o;
    
    // Insert Order Item

    OrderItem i = new OrderItem();
    i.OrderId = o.id;
    i.Quantity = 5;
    i.UnitPrice = 0;
    i.Product2id = p.id;
    i.PricebookEntryId=standardPrice.Id;
    insert i;

	}
}

I am very new to programming. Any help would be appreciated.

Thank you!
Maitreyee Dingare​​​​​​​​​​​​​​