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