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
Wayne_ClarkWayne_Clark 

How to create a Test Class for Apex Trigger (After Insert)

Hello,

 

I've been running in circles trying to figure out how to create a test class for my trigger.  The trigger works great in my sandbox, but in order for me to put it into production, I need to test it.  I've installed the Force.com IDE, but I'm not sure how to go about creating the instance within the test class. 

 

Below is my code, can anyone help me create a test class for this or point me in the right direction?

 

Trigger CloseSalesOrder on Invoice__c(after insert)

{

Set<Id> ids = trigger.newmap.keySet();

List<Invoice__c> inv=[select Sales_Order_Number__r.Status__c from  Invoice__c where id IN :ids];

List< Sales_Order__c> updateSalesorder=new List< Sales_Order__c>();

For(Invoice__c invs:inv)

{

 Invs.Sales_Order_Number__r.Status__c='Closed';

updateSalesorder.add(Invs.Sales_Order_Number__r);

}

Update updateSalesorder;

}
Best Answer chosen by Admin (Salesforce Developers) 
Wayne_ClarkWayne_Clark

Srini,

 

Thanks alot for your help.  Because of the required fields, I had to go 7 objects deep starting from inserting a profile. I finally got it to work and learned alot about test classes.  Below is my final code:

 

@isTest 
private class TestCloseSaleOrderTrigger {

static testMethod void myTest() {
Profile p = [select id from profile where name='Standard User'];
 
        User user = new User(alias = 'test123', email='test123@noemail.com',
            emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
            localesidkey='en_US', profileid = p.Id, country='United States',
            timezonesidkey='America/Los_Angeles', username='test123@noemail.com');
        insert user;

Account account = new Account();// specify all the required fields
account.name = 'testing';
insert account;
Contact contact = new Contact();// specify all the required fields
contact.lastname = 'test';
insert contact;
Orders__c kccorder = new Orders__c();// specify all the required fields
kccorder.account__c = account.ID;
kccorder.customer_order_number__c = '123456';
kccorder.order_type__c = 'ftl';
kccorder.Sales_Advisor__c = user.ID;
kccorder.Account_Contact__c = contact.ID;
kccorder.Value_del__c = Decimal.valueOf('500');
insert kccorder;
Customer_Charge__c customercharge = new Customer_Charge__c ();// specify all the required fields
customercharge.Customer_Charge__c = Decimal.valueOf('500');
customercharge.KCC_Order__c = kccorder.ID;
insert customercharge;
Sales_Order__c salesorder = new Sales_Order__c();// specify all the required fields
salesorder.Customer_Charge_Name__c = customercharge.ID;
insert salesorder;
Invoice__c invoice = new Invoice__c(Sales_Order_Number__c = salesorder.id);//specify all other required fields
invoice.sales_order_number__c = salesorder.ID;
insert invoice;
}
}

All Answers

hisrinuhisrinu

In order to cover this code you just need to insert Sales Order then an Invoice record where the lookup value should link to Sales Order.

 

If you do the above inserts automatically your code gets covered

Wayne_ClarkWayne_Clark

hisrinu,

 

How would I link the lookup value (sales_order_number__c) from the invoice to the Sales Order?  I have the following code so far, but it doesnt cover everything:

 

@isTest 
private class CreateInvoice {

static testMethod void myTest() {
Sales_Order__c salesorder = new Sales_Order__c();
Invoice__c invoice = new Invoice__c();


insert invoice;
insert salesorder;


}
}
hisrinuhisrinu

@isTest 
private class CreateInvoice {

static testMethod void myTest() {
Sales_Order__c salesorder = new Sales_Order__c();// specify all the required fields
insert salesorder
Invoice__c invoice = new Invoice__c(Sales_Order_Number__c = salesorder.id);//specify all other required fields
insert invoice;
}
}

This code should cover your entire trigger
Wayne_ClarkWayne_Clark

Srini,

 

Thanks alot for your help.  Because of the required fields, I had to go 7 objects deep starting from inserting a profile. I finally got it to work and learned alot about test classes.  Below is my final code:

 

@isTest 
private class TestCloseSaleOrderTrigger {

static testMethod void myTest() {
Profile p = [select id from profile where name='Standard User'];
 
        User user = new User(alias = 'test123', email='test123@noemail.com',
            emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
            localesidkey='en_US', profileid = p.Id, country='United States',
            timezonesidkey='America/Los_Angeles', username='test123@noemail.com');
        insert user;

Account account = new Account();// specify all the required fields
account.name = 'testing';
insert account;
Contact contact = new Contact();// specify all the required fields
contact.lastname = 'test';
insert contact;
Orders__c kccorder = new Orders__c();// specify all the required fields
kccorder.account__c = account.ID;
kccorder.customer_order_number__c = '123456';
kccorder.order_type__c = 'ftl';
kccorder.Sales_Advisor__c = user.ID;
kccorder.Account_Contact__c = contact.ID;
kccorder.Value_del__c = Decimal.valueOf('500');
insert kccorder;
Customer_Charge__c customercharge = new Customer_Charge__c ();// specify all the required fields
customercharge.Customer_Charge__c = Decimal.valueOf('500');
customercharge.KCC_Order__c = kccorder.ID;
insert customercharge;
Sales_Order__c salesorder = new Sales_Order__c();// specify all the required fields
salesorder.Customer_Charge_Name__c = customercharge.ID;
insert salesorder;
Invoice__c invoice = new Invoice__c(Sales_Order_Number__c = salesorder.id);//specify all other required fields
invoice.sales_order_number__c = salesorder.ID;
insert invoice;
}
}
This was selected as the best answer
Alina KondrashevaAlina Kondrasheva
Hi, i'm write apex Trigger, but I can't figure out how to create a test for it.
here is my code.
trigger UpdateNetTotalOnOffer on Quotation_Request__c (after update) {
  Set<Id> offerId = new Set<Id>();
  Map<Id,Quotation_Request__c> nMap = new Map<Id,Quotation_Request__c>();
  nMap =Trigger.newMap;  
  for(Offer_to_Quotation_Request__c ofQR:[
      Select Offer__c,Quotation_Request__c  
      from Offer_to_Quotation_Request__c
      where Quotation_Request__c in :nMap.keySet()
    ]){
    offerId.add(ofQR.Offer__c);
  }
//  System.debug(offerId);
List<Offer_to_Quotation_Request__c> qrToRollup = [
  select Offer__c,Quotation_Request__c,Quotation_Request__r.Cost__c
  from Offer_to_Quotation_Request__c
 
];
//System.debug(qrToRollup[0].Quotation_Request__r.Cost__c);
// Map<Id, Offer__c> offersMap = new Map<Id, Offer__c>();
// for (Offer_to_Quotation_Request__c a : qrToRollup) {
//   if (!offersMap.containsKey(a.Offer__c)){
//     offersMap.put(a.Offer__c, new Offer__c(Id = a.Offer__c));
//   }
//   Offer__c offer = offersMap.get(a.Offer__c);
//   offer.Net_Total__c += a.Quotation_Request__r.Cost__c ;
// }
// update offersMap.values();
List<Offer__c> offerToUpdate = new List<Offer__c>();
for(AggregateResult qrToRollup :[select offer__c, Sum(Quotation_Request__r.Net_Total__c) offerTotal from Offer_to_Quotation_Request__c where Offer__c in: offerId GROUP BY Offer__c ]){
  offerToUpdate.add(new Offer__c(Net_Total__c = (Decimal) qrToRollup.get('offerTotal'), Id = (Id) qrToRollup.get('Offer__c')));
}
update offerToUpdate;
}
please help write a test