You need to sign in to do that
Don't have an account?
Laetitia Damen 9
Trigger Test !
Hi there !
I'm currently in the test phase of my project. I suppose to write a test on my triggers if someone has the good syntax for that (I've tried but it didn't work): here my triggers
1/
trigger FA_TRH_CalculeAmount on Order (before update) {
for(integer i=0; i< trigger.new.size(); i++){
Order newOrder= trigger.new[i];
newOrder.NetAmount__c = newOrder.TotalAmount - newOrder.ShipmentCost__c;
}
}
2/
trigger FA_TRH_UpdateAccountCA on Order (before insert) {
set<Id> setAccountIds = new set<Id>();
List<Order> ordersList = trigger.new;
FA_QR_Account accountQuery = new FA_QR_Account();
FA_SRV_Account accountService = new FA_SRV_Account();
for(integer i=0; i< trigger.new.size(); i++){
Order newOrder= trigger.new[i];
setAccountIds.add(newOrder.AccountId);
}
Map<Id,Account> mapAccountId = accountQuery.getAccountsByIds(setAccountIds);
List<Account> accToUpdate = new List<Account>();
for (Order order : ordersList){
if(order.Status == 'Ordered' ||order.Status == 'Activated'){
Account acc = mapAccountId.get(order.AccountId);
if(acc != null){
acc.Chiffre_d_affaire__c = acc.Chiffre_d_affaire__c + order.TotalAmount;
}
accToUpdate.add(acc);
}
}
accountService.updateAccount(accToUpdate);
}
I'm currently in the test phase of my project. I suppose to write a test on my triggers if someone has the good syntax for that (I've tried but it didn't work): here my triggers
1/
trigger FA_TRH_CalculeAmount on Order (before update) {
for(integer i=0; i< trigger.new.size(); i++){
Order newOrder= trigger.new[i];
newOrder.NetAmount__c = newOrder.TotalAmount - newOrder.ShipmentCost__c;
}
}
2/
trigger FA_TRH_UpdateAccountCA on Order (before insert) {
set<Id> setAccountIds = new set<Id>();
List<Order> ordersList = trigger.new;
FA_QR_Account accountQuery = new FA_QR_Account();
FA_SRV_Account accountService = new FA_SRV_Account();
for(integer i=0; i< trigger.new.size(); i++){
Order newOrder= trigger.new[i];
setAccountIds.add(newOrder.AccountId);
}
Map<Id,Account> mapAccountId = accountQuery.getAccountsByIds(setAccountIds);
List<Account> accToUpdate = new List<Account>();
for (Order order : ordersList){
if(order.Status == 'Ordered' ||order.Status == 'Activated'){
Account acc = mapAccountId.get(order.AccountId);
if(acc != null){
acc.Chiffre_d_affaire__c = acc.Chiffre_d_affaire__c + order.TotalAmount;
}
accToUpdate.add(acc);
}
}
accountService.updateAccount(accToUpdate);
}
Please find the below test class which covers 100% for both the triggers.
If this solution helps, Please mark it as best answer.
Thanks
All Answers
Please find the below test class for first class. please create an other order with all the related fields so it covers 100%. First class is covering 100%.
If this solution helps, Please mark it as best answer.
Thanks,
What are FA_QR_Account and FA_SRV_Account objects. What is relation between Order and account and these objects.
Thanks,
FA_QR_Account is :
public Map<Id,Account> getAccountsByIds(set<Id> setAccountIds) {
Map<Id,Account> mapAccountId = new Map<Id,Account> ([
SELECT Id, Chiffre_d_affaire__c
FROM Account
WHERE Id in:setAccountIds
]);
return mapAccountId;
}
}
and FA_SRV_Account:
public class FA_SRV_Account {
public void updateAccount(List<Account> accounts){
update accounts;
}
}
Please find the below test class which covers 100% for both the triggers.
If this solution helps, Please mark it as best answer.
Thanks