You need to sign in to do that
Don't have an account?
Test/Publish Trigger
I wrote a fairly simple Apex trigger in my company's sandbox that I want to roll out, but I don't understand how testing and code coverage work. Is there a way I can specifically just test my trigger and get salesforce to let me publish it?
Thanks
Thanks
Your test method will need to create a record or records of the object type from your trigger, and then perform the DML action the trigger is looking for. After you do the DML in your test, then you should requery the record you created and validate that the trigger did what it was supposed to do.
Please check below blog for more information on Test classes:-
http://amitsalesforce.blogspot.in/2015/06/best-practice-for-test-classes-sample.html
Test Class for Trigger
Test Class for Standard Controller
Please let us know if this will help you
trigger editSchedule on OpportunityLineItem (after insert) {
if(Trigger.isInsert){
Map<Id, Opportunity> opportunities = new Map<Id, Opportunity>();
Date myDate = Date.newInstance(1990, 2, 17);
for(OpportunityLineItem oli: Trigger.new) {
opportunities.put(oli.OpportunityId, null);
}
opportunities.putAll([SELECT CloseDate FROM Opportunity WHERE Id in :opportunities.keySet()]);
for(OpportunityLineItem oli: Trigger.new) {
Opportunity thisLineItemOpp = opportunities.get(oli.OpportunityId);
myDate = thisLineItemOpp.CloseDate;
}
List<OpportunityLineItemSchedule> listOLIS = new List<OpportunityLineItemSchedule>();
for (OpportunityLineItem oli: Trigger.new){
if(oli.PricebookEntryId == '01uU000000H81Iv' || oli.PricebookEntryId == '01uU000000H81Iy'){//These are the Pricebook Entry IDs of Onboarding and Training, our two services.
//They must be handled differently than the products, because their list price
//is one sum, as opposed to a monthly amount. In order to get this trigger to handle
//new services, insert their IDs into each if statement for handling services.
listOLIS.add(new OpportunityLineItemSchedule(OpportunityLineItemId = oli.Id, Revenue = 943, ScheduleDate = myDate, Type = 'Revenue'));
}
if(oli.Payment_Terms__c == 'Monthly'){
if(oli.PricebookEntryId == '01uU000000H81Iv' || oli.PricebookEntryId == '01uU000000H81Iy'){
for(Integer duration = (Integer)oli.Duration__c; duration > 0; duration--){
listOLIS.add(new OpportunityLineItemSchedule(OpportunityLineItemId = oli.Id, Revenue = (oli.Discounted_Price__c*oli.Quantity)/oli.Duration__c, ScheduleDate = myDate, Type = 'Revenue'));
myDate = myDate.addMonths(1);
}//end of loop
}//end of inner IF
else{
for(Integer duration = (Integer)oli.Duration__c; duration > 0; duration--){
listOLIS.add(new OpportunityLineItemSchedule(OpportunityLineItemId = oli.Id, Revenue = oli.Discounted_Price__c*oli.Quantity, ScheduleDate = myDate, Type = 'Revenue'));
myDate = myDate.addMonths(1);
}//end of loop
}//end of else
}//end of Monthly IF
else if(oli.Payment_Terms__c == 'Quarterly'){
if(oli.PricebookEntryId == '01uU000000H81Iv' || oli.PricebookEntryId == '01uU000000H81Iy'){
for(Integer duration = (Integer)oli.Duration__c/3; duration > 0; duration--){
listOLIS.add(new OpportunityLineItemSchedule(OpportunityLineItemId = oli.Id, Revenue = (oli.Discounted_Price__c*oli.Quantity*3)/oli.Duration__c, ScheduleDate = myDate, Type = 'Revenue'));
myDate = myDate.addMonths(3);
}//end of loop
}//end of inner IF
else{
for(Integer duration = (Integer)oli.Duration__c/3; duration > 0; duration--){
listOLIS.add(new OpportunityLineItemSchedule(OpportunityLineItemId = oli.Id, Revenue = oli.Discounted_Price__c*oli.Quantity*3, ScheduleDate = myDate, Type = 'Revenue'));
myDate = myDate.addMonths(3);
}//end of loop
}//end of else
}//end of Quarterly IF
else if(oli.Payment_Terms__c == 'Annually'){
if(oli.PricebookEntryId == '01uU000000H81Iv' || oli.PricebookEntryId == '01uU000000H81Iy'){
for(Integer duration = (Integer)oli.Duration__c/12; duration > 0; duration--){
listOLIS.add(new OpportunityLineItemSchedule(OpportunityLineItemId = oli.Id, Revenue = (oli.Discounted_Price__c*oli.Quantity*12)/oli.Duration__c, ScheduleDate = myDate, Type = 'Revenue'));
myDate = myDate.addYears(1);
}//end of loop
}//end of inner IF
else{
for(Integer duration = (Integer)oli.Duration__c/12; duration > 0; duration--){
listOLIS.add(new OpportunityLineItemSchedule(OpportunityLineItemId = oli.Id, Revenue = oli.Discounted_Price__c*oli.Quantity*12, ScheduleDate = myDate, Type = 'Revenue'));
myDate = myDate.addYears(1);
}//end of loop
}//end of else
}//end of Annually IF
else if(oli.Payment_Terms__c == 'Up Front'){
if(oli.PricebookEntryId == '01uU000000H81Iv' || oli.PricebookEntryId == '01uU000000H81Iy'){
listOLIS.add(new OpportunityLineItemSchedule(OpportunityLineItemId = oli.Id, Revenue = oli.Discounted_Price__c*oli.Quantity, ScheduleDate = myDate, Type = 'Revenue'));
}//end of inner IF
else{
listOLIS.add(new OpportunityLineItemSchedule(OpportunityLineItemId = oli.Id, Revenue = oli.Discounted_Price__c*oli.Quantity*oli.Duration__c, ScheduleDate = myDate, Type = 'Revenue'));
}//end of else
}//end of Up Front IF
insert listOLIS;
}
}//end of isInsert
}//end of trigger
Thanks so much for your help.