function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Laetitia Damen 9Laetitia 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);
}
Best Answer chosen by Laetitia Damen 9
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Please find the below test class which covers 100% for both the triggers.
 
@istest
public class orderTriggerTest {

    @istest
    public static void testmet1()
    {
        
    Account a = new Account();
    a.Name = 'Test Account';
    a.Chiffre_d_affaire__c=56;
    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;
    

    Id pricebookId = Test.getStandardPricebookId();
    
    // Insert PricebookEntry

    PricebookEntry standardPrice = new PricebookEntry();
    standardPrice.Pricebook2Id = pricebookId;
    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 = 'Ordered';
    o.EffectiveDate = system.today();
    o.EndDate = system.today() + 4;
    o.AccountId = a.id;
    o.ShipmentCost__c=876;
    o.Pricebook2Id =  pricebookId ; 
    insert o;
        
        
        
         Order o1 = new Order();
    o1.Name = 'Test Order ';
    o1.Status = 'Draft';
    o1.EffectiveDate = system.today();
    o1.EndDate = system.today() + 4;
    o1.AccountId = a.id;
    o1.ShipmentCost__c=876;
    o1.Pricebook2Id =  pricebookId ; 
    insert o1;
        
       o1.ShipmentCost__c =76;
        update o1;
    }

}

If this solution helps, Please mark it as best answer.

Thanks

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

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%.
 
@istest
public class orderTriggerTest {

    @istest
    public static void testmet1()
    {
        
    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;
    

    Id pricebookId = Test.getStandardPricebookId();
    
    // Insert PricebookEntry

    PricebookEntry standardPrice = new PricebookEntry();
    standardPrice.Pricebook2Id = pricebookId;
    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.ShipmentCost__c=876;
    o.Pricebook2Id =  pricebookId ; 
    insert o;
        
        o.ShipmentCost__c =76;
        update o;
    }

}

If this solution helps, Please mark it as best answer.

Thanks,
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

What are FA_QR_Account and FA_SRV_Account objects. What is relation between Order and account and these objects.

Thanks,
 
Laetitia Damen 9Laetitia Damen 9
Hi, 

 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;
}
}
 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Please find the below test class which covers 100% for both the triggers.
 
@istest
public class orderTriggerTest {

    @istest
    public static void testmet1()
    {
        
    Account a = new Account();
    a.Name = 'Test Account';
    a.Chiffre_d_affaire__c=56;
    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;
    

    Id pricebookId = Test.getStandardPricebookId();
    
    // Insert PricebookEntry

    PricebookEntry standardPrice = new PricebookEntry();
    standardPrice.Pricebook2Id = pricebookId;
    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 = 'Ordered';
    o.EffectiveDate = system.today();
    o.EndDate = system.today() + 4;
    o.AccountId = a.id;
    o.ShipmentCost__c=876;
    o.Pricebook2Id =  pricebookId ; 
    insert o;
        
        
        
         Order o1 = new Order();
    o1.Name = 'Test Order ';
    o1.Status = 'Draft';
    o1.EffectiveDate = system.today();
    o1.EndDate = system.today() + 4;
    o1.AccountId = a.id;
    o1.ShipmentCost__c=876;
    o1.Pricebook2Id =  pricebookId ; 
    insert o1;
        
       o1.ShipmentCost__c =76;
        update o1;
    }

}

If this solution helps, Please mark it as best answer.

Thanks
This was selected as the best answer