Unit Test – CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY

H**ello,
**
My unit test return this error **TestUpdateAccountCA.test TestDataFactory system.DmlException: Update failed. First exception on row 0 with id 801WU00000JRtwkYAD; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, CalcultateAmount: execution of BeforeUpdate
caused Constructor not defined: [OrderNetAmountService].(List): [] Class.TestUpdateAccountCA.test: line 24,

I create apex class service AccountCA for to update a custom field ‘chiffre-d’affaire” on Account records based on the total revenue generated from associated Orders, class TestDataFactory for create elements, but I don’t understant why my asserts it’s not good.

Thank you for your help.

I would like a lead that could unlock my code.

@isTest
private class TestUpdateAccountCA {

    @isTest static void test() {
        // Create test data
        Account testAccount = TestDataFactory.createAccount('Account 1');
        Product2 testProduct = TestDataFactory.createProduct2('Produit 1');
        PricebookEntry testPriceBookEntry = TestDataFactory.createPricebookEntry(testProduct.Id);
        Order testOrder = TestDataFactory.createOrder(testAccount.Id);
        OrderItem testOrderItem1 = TestDataFactory.createOrderItem(testOrder.Id, testPriceBookEntry.Id, 10, 150);
        OrderItem testOrderItem2 = TestDataFactory.createOrderItem(testOrder.Id, testPriceBookEntry.Id, 20, 1000);

        Test.startTest();

        // Update the order status
        testOrder.Status = 'Ordered'; 
        update testOrder; // Line 24

        Test.stopTest();

        // Calculate expected total revenue
        Decimal expectedRevenue = (testOrderItem1.Quantity * testOrderItem1.UnitPrice) + (testOrderItem2.Quantity * testOrderItem2.UnitPrice);

        // ASSERT
        Account updatedAccount = [SELECT Id, Chiffre_d_affaire__c FROM Account WHERE Id = :testAccount.Id LIMIT 1];
        System.assertEquals(expectedRevenue, updatedAccount.Chiffre_d_affaire__c, 'The account revenue should be updated correctly.');
    }
}


public with sharing class TestDataFactory {

    public static Account createAccount(String name) {
        Account acc = new Account(Name = name);
        insert acc;
        return acc;
        
    }
    
    public static Account createAccountWithMultipleOrders (Integer numberOfOrders){

        Account accs = new Account(Name = 'Test Account 2');
        insert accs;

        List<Order> orders = new List<Order>();

           for (Integer i = 0; i < numberOfOrders; i++) {
               Order order = new Order(
                AccountId = accs.Id,
                Status = 'Draft',
                Pricebook2Id = Test.getStandardPricebookId(),
                EffectiveDate = Date.today(),
                Name = 'Test Order ' + i
               );
               orders.add(order);
           }
           
           // Insère toutes les commandes
           insert orders;
           return accs;

    }

    public static Product2 createProduct2(String name) {
        Product2 prod = new Product2(
            Name = name,
            Family = 'Chemise'
        );
        insert prod;
        return prod;
    }

    public static PricebookEntry createPricebookEntry(Id productId) {
        PricebookEntry pbooke = new PricebookEntry(
            Pricebook2Id = Test.getStandardPricebookId(),
            Product2Id = productId,
            UnitPrice = 1020,
            IsActive = true
        );
        insert pbooke;
        return pbooke;
    }

    public static Order createOrder(Id accountId) {
        Order testOrder = new Order(
            Name = 'Test Order',
            AccountId = accountId,
            Status = 'Draft',
            Pricebook2Id = Test.getStandardPricebookId(),
            EffectiveDate = Date.today()
        );
        insert testOrder;
        return testOrder;
    }

    public static OrderItem createOrderItem(Id orderId, Id pricebookEntryId, Integer quantity, Decimal unitPrice) {
        OrderItem orderItem = new OrderItem(
            OrderId = orderId,
            PricebookEntryId = pricebookEntryId,
            Quantity = quantity,
            UnitPrice = unitPrice
        );
        insert orderItem;
        return orderItem;
    }
}
public with sharing class AccountCAService {
    public static void processOrdersUpdateAccount(Map<Id, Order> oldOrders, List<Order> newOrders) {
      
        Set<Id> accountIds = new Set<Id>(); 
        Map<Id, Decimal> accountAmount = new Map<Id, Decimal>(); 

        for (Order ord : newOrders) {
            if (ord.AccountId != null) {
                 
                Decimal oldAmount = oldOrders != null && oldOrders.containsKey(ord.Id) ? oldOrders.get(ord.Id).TotalAmount : 0;
              
                Decimal difference = ord.TotalAmount - oldAmount;

                if (!accountAmount.containsKey(ord.AccountId)) {
                    accountAmount.put(ord.AccountId, 0);
                }
                accountAmount.put(ord.AccountId, accountAmount.get(ord.AccountId) + difference);
            }
        }

        List<Account> accountsToUpdate = new List<Account>();
         
        for (Id accountId : accountAmount.keySet()) {
            Account acc = [SELECT Id, Chiffre_d_affaire__c FROM Account WHERE Id = :accountId FOR UPDATE]; // Gérer les conflits d'update
              
            acc.Chiffre_d_affaire__c += accountAmount.get(accountId);
            accountsToUpdate.add(acc);
        }

        if (!accountsToUpdate.isEmpty()) {
            update accountsToUpdate;
        }
    }
}

    public static void calculateNetAmount(List<Order> orders) {
        
        if (orders == null || orders.isEmpty()) {
            return;
        }

        for (Order order : orders) {
            
            if (order.TotalAmount != null && order.ShipmentCost__c != 
 null) {
              
                order.NetAmount__c = order.TotalAmount - order.ShipmentCost__c;
            }
        }

        
    }
}

10

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật