You need to sign in to do that
Don't have an account?
Wayne_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;
}
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:
All Answers
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
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:
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:
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