• syric
  • NEWBIE
  • 40 Points
  • Member since 2012

  • Chatter
    Feed
  • 1
    Best Answers
  • 2
    Likes Received
  • 0
    Likes Given
  • 7
    Questions
  • 14
    Replies
I was messing with unlocking opportunities.  The business requires Opportunities submitted for Approval in the "Signed and Submited for Approval" stage to be unlocked.  Currently in the UI when you do this it locks the record and places an unlock button that allows you to unlock the record only if you have an admin profile/modify all permission.  I used the sample code from the method guide to make this trigger:
 
trigger triggerUnlock on Opportunity (after update) {
    
Opportunity[] oList = [SELECT Id from Opportunity Where Id IN : Trigger.new and StageName = 'Signed and Submitted for Approval']; 

Approval.UnlockResult[] urList = Approval.unlock(oList, false);

for(Approval.UnlockResult ur : urList) {
    if (ur.isSuccess()) {
        // Operation was successful, so get the ID of the record that was processed
        System.debug('Successfully unlocked opportunity with ID: ' + ur.getId());
    }
    else {
        // Operation failed, so get all errors                
        for(Database.Error err : ur.getErrors()) {
            System.debug('The following error has occurred.');                    
            System.debug(err.getStatusCode() + ': ' + err.getMessage());
            System.debug('Opportunity fields that affected this error: ' + err.getFields());
        }
    }
}


}
Using debug logs I can tell that it is unlocking the opportunity record when it is submitted for approval.  But in the UI it still has the unlock button and the record is locked.  So is the "Approval.unlock()" method used specifically to unlock the record only for apex uses?  Does record lock itself again after all code has ran?  Is there something I can do to keep the record unlocked so a user can still edit it?  Thanks
 
  • August 25, 2016
  • Like
  • 1
I have a trigger I wrote that works great when I test it in the UI, but my test is failing.  I get the following error:
Error Message	System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, triggerTest: execution of BeforeInsert

caused by: System.NullPointerException: Argument cannot be null.

Trigger.triggerTest: line 294, column 1: []
Stack Trace	Class.testtest.myUnitTest: line 61, column 1
If I'm understanding this correctly it says its failing when it tries to insert the opportunitylineitem because when the trigger tries to do "a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);" and it thinks No_Ordered__c or ListPrice is NULL.  I get this error whether or not I use a the test product or a real product already in the system that I know works fine when I test in the UI.  Please tell me what I am doing wrong.  Any help is appreciated.  Here is the code for the trigger and the test.  I trimmed down the test to test only one product.

TRIGGER
trigger triggerTest on OpportunityLineItem (before insert, before update) {

		decimal disc;
		decimal sub;
		decimal servamt;
		decimal billcalc;
		Integer quartercalc;
		Integer enddate;

		for(OpportunityLineItem a : Trigger.New){
			a.Quantity = (a.No_Ordered__c * a.Service_Duration__c);
			sub = (a.Quantity * a.UnitPrice);
			if(a.Discount != NULL){
				disc = (a.Discount * .01);
				a.TotalPrice = (sub - (sub * disc));
			}else{
				a.TotalPrice = sub;	
			}
			servamt = (a.Quantity / a.No_Ordered__c);
			a.Service_Amount__c = (a.TotalPrice / servamt);
			if(a.PB__c == 'Weekly'){
				if(a.Billing_Period__c == 'Weekly'){
					billcalc = 1;
					a.Billing_Amount__c = (a.Service_Amount__c * billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice * billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c * billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 7);
					a.End_Date__c = a.ServiceDate.addDays(enddate);
				}else if(a.Billing_Period__c == 'Quarterly'){
					billcalc = 13;
					a.Billing_Amount__c = (a.Service_Amount__c * billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice * billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c * billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 7);
					a.End_Date__c = a.ServiceDate.addDays(enddate);				
				}else if(a.Billing_Period__c == 'Semi-Annual'){
					billcalc = 26;
					a.Billing_Amount__c = (a.Service_Amount__c * billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice * billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c * billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 7);
					a.End_Date__c = a.ServiceDate.addDays(enddate);				
				}else if(a.Billing_Period__c == 'Annual'){
					billcalc = 52;
					a.Billing_Amount__c = (a.Service_Amount__c * billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice * billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c * billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);				
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 7);
					a.End_Date__c = a.ServiceDate.addDays(enddate);							
				}

			}else if(a.PB__c == 'Monthly' || a.PB__c == 'Monthly D'){
				if(a.Billing_Period__c == 'Monthly'){
					billcalc = 1;
					a.Billing_Amount__c = (a.Service_Amount__c * billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice * billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c * billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 1);
					a.End_Date__c = a.ServiceDate.addMonths(enddate);
				}else if(a.Billing_Period__c == 'Every Other Month'){
					billcalc = 2;
					a.Billing_Amount__c = (a.Service_Amount__c * billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice * billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c * billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 1);
					a.End_Date__c = a.ServiceDate.addMonths(enddate);
				}else if(a.Billing_Period__c == 'Quarterly'){
					billcalc = 3;
					a.Billing_Amount__c = (a.Service_Amount__c * billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice * billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c * billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 1);
					a.End_Date__c = a.ServiceDate.addMonths(enddate);
				}else if(a.Billing_Period__c == 'Semi-Annual'){
					billcalc = 6;
					a.Billing_Amount__c = (a.Service_Amount__c * billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice * billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c * billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 1);
					a.End_Date__c = a.ServiceDate.addMonths(enddate);
				}else if(a.Billing_Period__c == 'Annual'){
					billcalc = 12;
					a.Billing_Amount__c = (a.Service_Amount__c * billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice * billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c * billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 1);
					a.End_Date__c = a.ServiceDate.addMonths(enddate);
				}

			}else if(a.PB__c == 'Quarterly'){
				if(a.Billing_Period__c == 'Quarterly'){
					billcalc = 1;
					a.Billing_Amount__c = (a.Service_Amount__c * billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice * billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c * billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 3);
					a.End_Date__c = a.ServiceDate.addMonths(enddate);
				}else if(a.Billing_Period__c == 'Semi-Annual'){
					billcalc = 2;
					a.Billing_Amount__c = (a.Service_Amount__c * billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice * billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c * billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 3);
					a.End_Date__c = a.ServiceDate.addMonths(enddate);
				}else if(a.Billing_Period__c == 'Annual'){
					billcalc = 4;
					a.Billing_Amount__c = (a.Service_Amount__c * billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice * billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c * billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 3);
					a.End_Date__c = a.ServiceDate.addMonths(enddate);
				}else if(a.Billing_Period__c == 'Monthly'){
					billcalc = 3;
					a.Billing_Amount__c = (a.Service_Amount__c / billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice / billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c / billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 3);
					a.End_Date__c = a.ServiceDate.addMonths(enddate);
				}

			}else if(a.PB__c == 'Annual'){
				if(a.Billing_Period__c == 'Annual'){
					billcalc = 1;
					a.Billing_Amount__c = (a.Service_Amount__c * billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice * billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c * billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 1);
					a.End_Date__c = a.ServiceDate.addYears(enddate);
				}else if(a.Billing_Period__c == 'Monthly'){
					billcalc = 12;
					a.Billing_Amount__c = (a.Service_Amount__c / billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice / billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c / billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 1);
					a.End_Date__c = a.ServiceDate.addYears(enddate);
				}else if(a.Billing_Period__c == 'Every Other Month'){
					billcalc = 6;
					a.Billing_Amount__c = (a.Service_Amount__c / billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice / billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c / billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 1);
					a.End_Date__c = a.ServiceDate.addYears(enddate);
				}else if(a.Billing_Period__c == 'Quarterly'){
					billcalc = 4;
					a.Billing_Amount__c = (a.Service_Amount__c / billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice / billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c / billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 1);
					a.End_Date__c = a.ServiceDate.addYears(enddate);
				}else if(a.Billing_Period__c == 'Semi-Annual'){
					billcalc = 2;
					a.Billing_Amount__c = (a.Service_Amount__c / billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice / billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c / billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 1);
					a.End_Date__c = a.ServiceDate.addYears(enddate);
				}

			}else if(a.PB__c == 'One Time' || a.PB__c == 'One Time D'){
				if(a.Billing_Period__c == 'Monthly'){
					billcalc = 1;
					a.Billing_Amount__c = (a.Service_Amount__c * billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice * billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c * billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 1);
					a.End_Date__c = a.ServiceDate.addDays(enddate);
				}

			}

		}


	}

TEST CLASS
 
/**
 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 *
 * Unit tests are class methods that verify whether a particular piece
 * of code is working properly. Unit test methods take no arguments,
 * commit no data to the database, and are flagged with the testMethod
 * keyword in the method definition.
 *
 * All test methods in an organization are executed whenever Apex code is deployed
 * to a production organization to confirm correctness, ensure code
 * coverage, and prevent regressions. All Apex classes are
 * required to have at least 75% code coverage in order to be deployed
 * to a production organization. In addition, all triggers must have some code coverage.
 * 
 * The @isTest class annotation indicates this class only contains test
 * methods. Classes defined with the @isTest annotation do not count against
 * the organization size limit for all Apex scripts.
 *
 * See the Apex Language Reference for more information about Testing and Code Coverage.
 */
@isTest(SeeAllData=true)
private class testtest {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test

        Date now = date.today();
        integer day = now.day();
        
        // CREATE ACCOUNT
        Account newAccount = new Account(Name = 'Test Account', Phone='817-999-9999' );
        insert newAccount;

         // CREATE CONTACT
        Contact newContact = new Contact(LastName = 'Test Contact', AccountId = newAccount.Id );
        insert newContact; 

        // CREATE OPPORTUNITY
        Opportunity newOpp = new Opportunity(Name = 'Test Opportunity', CloseDate = System.today(), AccountId = newAccount.Id, Contact_Name__c = newContact.Id, StageName = 'Initial Interview' );
        insert newOpp;
        
        // GET STANDARD PRICEBOOK
		Pricebook2  standardPb = [select id, name, isActive from Pricebook2 where IsStandard = true limit 1];

		// CREATE CUSTOM PRICEBOOK
		Pricebook2 pbk1 = new Pricebook2 (Name='Test Pricebook Entry 1',Description='Test Pricebook Entry 1', isActive=true);
		insert pbk1;

		// CREATE PRODUCT
		Product2 prd1 = new Product2 (Name='Test Product Entry 1',Description='Test Product Entry 1',productCode = 'ABC', isActive = true, Pricing_Basis__c = 'Annual' );
		insert prd1;

		// CREATE PRICEBOOKENTRY
		PricebookEntry pbe1 = new PricebookEntry (Product2ID=prd1.id,Pricebook2ID=standardPb.id,UnitPrice=50, isActive=true);
		insert pbe1;


        // CREATE OPPORTUNITYPRODUCTS
        OpportunityLineItem newProdAnnual = new OpportunityLineItem (Billing_Period__c = 'Annual', OpportunityId = newOpp.Id, Service_Duration__c = 1, ServiceDate = now, End_Date__c = now, PricebookEntryId = pbe1.Id, No_Ordered__c = 1, UnitPrice = 50, Quantity = 1);       
        insert newProdAnnual;

        //QUERY
        OpportunityLineItem a =[SELECT ID, Quantity, TotalPrice, Discount, UnitPrice, Billing_Amount__c, List_Price_Svc__c, List_Price_Bill__c, List_Price_Extd__c, Billing_Savings__c, Per_Item_Bill__c, Discount_Amount_Bill__c, Service_Savings__c, Per_Item_Svc__c, Discount_Amount_Svc__c, End_Date__c, ServiceDate from OpportunityLineItem WHERE ID =: newProdAnnual.Id ];


        //VALIDATE DATA WAS UPDATED CORRECTLY

		//Annual
		System.assertEquals(a.Quantity, 1);
		System.assertEquals(a.TotalPrice, 50.00);
		System.assertEquals(a.Discount, 0);
		System.assertEquals(a.Billing_Amount__c, 50.00);
		System.assertEquals(a.List_Price_Svc__c, 50.00);
		System.assertEquals(a.List_Price_Bill__c, 50.00);
		System.assertEquals(a.List_Price_Extd__c, 50.00);
		System.assertEquals(a.Billing_Savings__c, NULL);
		System.assertEquals(a.Per_Item_Bill__c, 50.00);		
		System.assertEquals(a.Discount_Amount_Bill__c, 0.00);
		System.assertEquals(a.Service_Savings__c, NULL);
		System.assertEquals(a.Per_Item_Svc__c, 50.00);
		System.assertEquals(a.Discount_Amount_Svc__c, 0.00);
		System.assertEquals(a.End_Date__c, a.ServiceDate.addYears(1));

    }
}



 
  • March 03, 2015
  • Like
  • 1
Hello- I'm new to visualforce and this is my first project.  Through the online guides and examples I have been able to accomplish almost everything.  I'm trying to increase my code coverage.  It is currently at 66%.

This is my controller:
public with sharing class CommentsController {

	private final Case cas;
		   
    //
    public String MasterRecordId        {get; set;}
    public List<Case_Comments__c> ChildRecords  {get; set;}
    public Case MasterRecord                 {get; set;}

    //SET UP PAGE
    public CommentsController(ApexPages.StandardController stdController) {
    		this.cas = (Case)stdController.getRecord();
		MasterRecordId = ApexPages.currentPage().getParameters().get('id');

		 //CHECK FOR ID
        if(!String.isBlank(MasterRecordId)){

            //GET PARENT
            MasterRecord =
                [
                    SELECT 
                        Id,
                        CaseNumber
                    FROM
                        Case
                    WHERE
                        Id = :MasterRecordId
                ];

            //GET CHILD RECORDS
            ChildRecords =
                [
                    SELECT 
                        Name,
						Id,
                        CreatedDate,
                        CreatedBy.Id,
                        CreatedBy.Name,
                        Time_Spent__c,
                        Comment__c,
                        ParentId__c
                    FROM
                        Case_Comments__c
                    WHERE 
                        ParentId__c = :MasterRecordId order by CreatedDate desc
                ];
        }
    }

}

This is my test class:

@isTest
	public class testCommentsController {
    	static testMethod void testFirstTest() {

        // CREATE ACCOUNT
		Account newAccount = new Account(Name = 'Test Account', Phone='817-999-9999');
		insert newAccount;
      
        // CREATE CONTACT
		Contact newContact = new Contact(LastName = 'Test Contact', AccountId = newAccount.Id);
		insert newContact;  
		       		        
        // CREATE CASE
        Case newCase = new Case(Subject = 'Case Subject', Description = 'Case Description', ContactId = newContact.Id, AccountId = newAccount.Id);
        insert newCase;  
        
        // CREATE CASE COMMENT
        Case_Comments__c newComment = new Case_Comments__c(ParentId__c = newCase.Id, Comment__c = 'Test Comment Body', Time_Spent__c = .5);
        insert newComment;

        ApexPages.StandardController sc = new ApexPages.StandardController(newCase);
        CommentsController cc = new CommentsController (sc);

        PageReference pageRef = Page.Comments;
        pageRef.getParameters().put('id', String.valueOf(newCase.Id));
        Test.setCurrentPage(pageRef);
			
        }
}

I can tell from the test coverage highlights that the case "MasterRecord" and the list "ChildRecords" from the controller are not covered.  I assume that I would need to assert that MasterRecord = the case, and that ChildRecords ParentId__c = MasterRecord Id.  I do not know how to do that.  I do not know how to reference them in my test class or if that is right.  Any help would be appreciated.

  • August 14, 2014
  • Like
  • 0

I'm new and learning Apex.  I'm having an issue with a trigger I wrote.  It hits the script statements govenor and causes the error "System.LimitException: Too many script statements: 200001".  It works with single updates. The error was caused when I tested it by updating 2,000 opportunity records and started after 937 were succesfully updated.

 

There are three revenue fields on Opportunity (Monthly, Prorated, Membership).  When an opportunity is updated, the trigger searchs through its products and determines which field the revenue belongs to using the product description.

 

trigger Test2 on Opportunity (before insert, before update) {

// CREATE PRODUCT LIST FOR THE OPPORTUNITIES
List<OpportunityLineItem> prodList = [select ID, OpportunityId, Description, TotalPrice from OpportunityLineItem where OpportunityId in: Trigger.new];

// DEFINE STRINGS USED TO DETERMINE TYPE OF PRODUCT
String Prorated = 'Prorated';
String Membership = 'Membership';

// LOOP THROUGH OPPORTUNITIES, RESET 3 REVENUE FIELDS TO 0
	for(Opportunity a : Trigger.new){
		a.Prorated_Total__c = 0;
		a.Membership_Total__c = 0;
		a.Monthly_Total__c = 0;

// LOOP THROUGH PRODUCT LIST TO GET REVENUE TYPE AND TOTAL FOR CURRENT OPPORTUNITY
		for(OpportunityLineItem b : prodList){

//  MAKE SURE PRODUCTS BELONG TO CURRENT OPPORTUNITY
			if(a.Id == b.OpportunityId){
// DETERMINES PRODUCT TYPE FROM DESCRIPTION AND ADDS PRICE TO THE CORRESPONDING FIELD ON THE CURRENT OPPORTUNITY
		    	     if(b.Description.contains(Prorated)){
		     	     a.Prorated_Total__c += b.TotalPrice;         
		   	     }else if(b.Description.contains (Membership)){        
			     a.Membership_Total__c += b.TotalPrice; 
		    	     }else{
		             a.Monthly_Total__c += b.TotalPrice;   
	    		     }
			}   
		}
	}
}

  After looking up the error, I'm sure the issue is with the nested loop. Everything else I tried failed and I'm struggling.  Any help is appreciated, thanks.

  • January 07, 2013
  • Like
  • 0

I have a validation rule:

AND(OR(BillingCountry = "CAN", BillingCountry = "USA"),
VLOOKUP( 
$ObjectType.Zip_Code__c.Fields.City__c , 
$ObjectType.Zip_Code__c.Fields.Name , 
BillingPostalCode) <> BillingCity)

I want it work when BillingCountry is anything besides CAN or USA.  Is there a way to do this without doing BillingCountry = "" for every other country in the world?

 

I tried this and it did not work correctly:

AND(OR(NOT(BillingCountry = "CAN"), NOT(BillingCountry = "USA")),
VLOOKUP( 
$ObjectType.Zip_Code__c.Fields.City__c , 
$ObjectType.Zip_Code__c.Fields.Name , 
BillingPostalCode) <> BillingCity)

 Any help would be appreciated. Thanks.

  • December 11, 2012
  • Like
  • 0

I need help with a trigger I wrote.  It works fine with one record updates but it hits the govenor limits when a batch is procesesd.  I understand that it is because I have the SOQL query in a for loop which is causing the query to fire multiple times when a batch is processed.  The issue is I am not sure how to write it where the Query is outside the loop and the trigger will still work and how to "Bulkify" the portion where I am updating BillingCity and BillingZip to handle the mutiple records in the query.  I'm a beginner/learning and any help would be appreciated.

 

trigger triggerAccountCityState on Account (before insert, before update) {

// IF THE ACCOUNT RECORD BEING INSERTED/UPDATED HAS A BILLINGPOSTALCODE AND DOES NOT HAVE A BILLINGCOUNTRY OF "FOR", EXECUTE TRIGGER
    for(Account acct:Trigger.new){
        if(acct.BillingPostalCode != null && acct.BillingCountry != 'FOR'){     
            string zipcode = acct.BillingPostalCode;
            string zipcity;
            string zipstate;
            
// QUERY THE CUSTOM sOBJECT "ZIP CODE" TO GET THE CITY AND STATE FOR THE ZIP CODE BEING INSERTED/UPDATED
            List <Zip_Code__c> zipList = [select Name, City__c, State__c from Zip_Code__c where Name = :zipcode];

// REPLACE THE EXISTING CITY AND STATE WITH THE DATA RETURNED FROM THE QUERY
                for(Zip_Code__c z : zipList){
                zipcity = z.City__c;
                zipstate = z.State__c;
                }
            acct.BillingCity = zipcity;
            acct.BillingState = zipstate;

// IF NOTHING WAS RETURNED/UDPATED GIVE THE INVALID ZIP CODE ERROR
            for (Integer i = 0; i < Trigger.new.size(); i++) {
                if (Trigger.new[i].BillingCity == null){
                    Trigger.new[i].addError('The Zip Code you have entered is not valid.');
                }
            }
           }

    }

}
  • October 04, 2012
  • Like
  • 0

Hello, I am new to apex.  I'm trying to make a trigger that updates the unit price on an opportunity's products marked with a specific discount when it is set to a certain stage.  For testing purposes I have made a trigger that tries to update the marked prices anytime the opportunity is updated. 

 

The problem I am running into is that I can't get the updated prices to save.  I put debug statements in so I could see what was happening and everything looks ok to me.  I have posted my trigger code and the debug log results below.  Any help would be appreciated.

 

CODE:

 

trigger DEVProrateTrigger on Opportunity (before insert, before update) {

//GET TODAYS DATE AND SUBTRACT IT FROM 30 TO GET THE INTEGER TO DIVIDE THE PRICE BY, "PRICEDIV"
Date now = date.today();
integer day = now.day();
integer month = 30;
integer pricediv = month - day;

//GET A LIST OF THE PRODUCTS FOR THE OPPORTUNITY BEING UPDATED "PRODLIST"
List<OpportunityLineItem> prodList = [select ID, OpportunityId, Price_Adjustment_Reason_Code__c, UnitPrice from OpportunityLineItem where OpportunityId in: Trigger.new];

//DEBUG STATMENT #1 TO SHOW ME THE PULLED LIST OF PRODUCTS, "PRODLIST"
System.Debug(prodList);

//LOOP THROUGH PULLED "PRODLIST" TO FIND ANY WITH A PRICE ADJUSTMENT OF PRORATE AND DIVDE THEIR UNIT PRICE BY THE INTEGER "PRICEDEV"
for(OpportunityLineItem a : prodList){
    if(a.Price_Adjustment_Reason_Code__c == 'PI = Prorated Item'){
        a.UnitPrice = (a.UnitPrice / pricediv);
    }
}

//DEBUG STATEMENT #2 TO SHOW ME "PRODLIST" WITH THE UPDATED VALUES
System.Debug(prodList);

//UPDATE/SAVE THE UPDATED "PRODLIST" TO THE DATABASE
Database.Saveresult[] results =Database.update(prodList);

//DEBUG STATEMENT #3 TO SHOW ME THAT "PRODLIST" WAS UPDATED/SAVED CORRECTLY
System.Debug(results);
}

 

 

DEBUG LOG STATEMENTS

 

#1 - 13:45:16.176 (176394000)|USER_DEBUG|[10]|DEBUG|(OpportunityLineItem:{Price_Adjustment_Reason_Code__c=PI = Prorated Item, OpportunityId=006W0000002VguoIAC, Id=00kW0000002zs2iIAA, UnitPrice=50.00})  -This statement shows the correct data was pulled.

 

#2 - 13:45:16.177 (177312000)|USER_DEBUG|[17]|DEBUG|(OpportunityLineItem:{Price_Adjustment_Reason_Code__c=PI = Prorated Item, OpportunityId=006W0000002VguoIAC, Id=00kW0000002zs2iIAA, UnitPrice=2.38095238095238095238095238095238})  -This statement shows that the unitprice in the list was updated from the loop.

 

#3 - 13:45:16.347 (347895000)|USER_DEBUG|[20]|DEBUG|(Database.SaveResult[getErrors=();getId=00kW0000002zs2iIAA;isSuccess=true;]) -This shows that the updated list with the updated price was saved to the database correctly.

  • June 13, 2012
  • Like
  • 0
I was messing with unlocking opportunities.  The business requires Opportunities submitted for Approval in the "Signed and Submited for Approval" stage to be unlocked.  Currently in the UI when you do this it locks the record and places an unlock button that allows you to unlock the record only if you have an admin profile/modify all permission.  I used the sample code from the method guide to make this trigger:
 
trigger triggerUnlock on Opportunity (after update) {
    
Opportunity[] oList = [SELECT Id from Opportunity Where Id IN : Trigger.new and StageName = 'Signed and Submitted for Approval']; 

Approval.UnlockResult[] urList = Approval.unlock(oList, false);

for(Approval.UnlockResult ur : urList) {
    if (ur.isSuccess()) {
        // Operation was successful, so get the ID of the record that was processed
        System.debug('Successfully unlocked opportunity with ID: ' + ur.getId());
    }
    else {
        // Operation failed, so get all errors                
        for(Database.Error err : ur.getErrors()) {
            System.debug('The following error has occurred.');                    
            System.debug(err.getStatusCode() + ': ' + err.getMessage());
            System.debug('Opportunity fields that affected this error: ' + err.getFields());
        }
    }
}


}
Using debug logs I can tell that it is unlocking the opportunity record when it is submitted for approval.  But in the UI it still has the unlock button and the record is locked.  So is the "Approval.unlock()" method used specifically to unlock the record only for apex uses?  Does record lock itself again after all code has ran?  Is there something I can do to keep the record unlocked so a user can still edit it?  Thanks
 
  • August 25, 2016
  • Like
  • 1
I have a trigger I wrote that works great when I test it in the UI, but my test is failing.  I get the following error:
Error Message	System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, triggerTest: execution of BeforeInsert

caused by: System.NullPointerException: Argument cannot be null.

Trigger.triggerTest: line 294, column 1: []
Stack Trace	Class.testtest.myUnitTest: line 61, column 1
If I'm understanding this correctly it says its failing when it tries to insert the opportunitylineitem because when the trigger tries to do "a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);" and it thinks No_Ordered__c or ListPrice is NULL.  I get this error whether or not I use a the test product or a real product already in the system that I know works fine when I test in the UI.  Please tell me what I am doing wrong.  Any help is appreciated.  Here is the code for the trigger and the test.  I trimmed down the test to test only one product.

TRIGGER
trigger triggerTest on OpportunityLineItem (before insert, before update) {

		decimal disc;
		decimal sub;
		decimal servamt;
		decimal billcalc;
		Integer quartercalc;
		Integer enddate;

		for(OpportunityLineItem a : Trigger.New){
			a.Quantity = (a.No_Ordered__c * a.Service_Duration__c);
			sub = (a.Quantity * a.UnitPrice);
			if(a.Discount != NULL){
				disc = (a.Discount * .01);
				a.TotalPrice = (sub - (sub * disc));
			}else{
				a.TotalPrice = sub;	
			}
			servamt = (a.Quantity / a.No_Ordered__c);
			a.Service_Amount__c = (a.TotalPrice / servamt);
			if(a.PB__c == 'Weekly'){
				if(a.Billing_Period__c == 'Weekly'){
					billcalc = 1;
					a.Billing_Amount__c = (a.Service_Amount__c * billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice * billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c * billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 7);
					a.End_Date__c = a.ServiceDate.addDays(enddate);
				}else if(a.Billing_Period__c == 'Quarterly'){
					billcalc = 13;
					a.Billing_Amount__c = (a.Service_Amount__c * billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice * billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c * billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 7);
					a.End_Date__c = a.ServiceDate.addDays(enddate);				
				}else if(a.Billing_Period__c == 'Semi-Annual'){
					billcalc = 26;
					a.Billing_Amount__c = (a.Service_Amount__c * billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice * billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c * billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 7);
					a.End_Date__c = a.ServiceDate.addDays(enddate);				
				}else if(a.Billing_Period__c == 'Annual'){
					billcalc = 52;
					a.Billing_Amount__c = (a.Service_Amount__c * billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice * billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c * billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);				
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 7);
					a.End_Date__c = a.ServiceDate.addDays(enddate);							
				}

			}else if(a.PB__c == 'Monthly' || a.PB__c == 'Monthly D'){
				if(a.Billing_Period__c == 'Monthly'){
					billcalc = 1;
					a.Billing_Amount__c = (a.Service_Amount__c * billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice * billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c * billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 1);
					a.End_Date__c = a.ServiceDate.addMonths(enddate);
				}else if(a.Billing_Period__c == 'Every Other Month'){
					billcalc = 2;
					a.Billing_Amount__c = (a.Service_Amount__c * billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice * billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c * billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 1);
					a.End_Date__c = a.ServiceDate.addMonths(enddate);
				}else if(a.Billing_Period__c == 'Quarterly'){
					billcalc = 3;
					a.Billing_Amount__c = (a.Service_Amount__c * billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice * billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c * billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 1);
					a.End_Date__c = a.ServiceDate.addMonths(enddate);
				}else if(a.Billing_Period__c == 'Semi-Annual'){
					billcalc = 6;
					a.Billing_Amount__c = (a.Service_Amount__c * billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice * billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c * billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 1);
					a.End_Date__c = a.ServiceDate.addMonths(enddate);
				}else if(a.Billing_Period__c == 'Annual'){
					billcalc = 12;
					a.Billing_Amount__c = (a.Service_Amount__c * billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice * billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c * billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 1);
					a.End_Date__c = a.ServiceDate.addMonths(enddate);
				}

			}else if(a.PB__c == 'Quarterly'){
				if(a.Billing_Period__c == 'Quarterly'){
					billcalc = 1;
					a.Billing_Amount__c = (a.Service_Amount__c * billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice * billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c * billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 3);
					a.End_Date__c = a.ServiceDate.addMonths(enddate);
				}else if(a.Billing_Period__c == 'Semi-Annual'){
					billcalc = 2;
					a.Billing_Amount__c = (a.Service_Amount__c * billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice * billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c * billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 3);
					a.End_Date__c = a.ServiceDate.addMonths(enddate);
				}else if(a.Billing_Period__c == 'Annual'){
					billcalc = 4;
					a.Billing_Amount__c = (a.Service_Amount__c * billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice * billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c * billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 3);
					a.End_Date__c = a.ServiceDate.addMonths(enddate);
				}else if(a.Billing_Period__c == 'Monthly'){
					billcalc = 3;
					a.Billing_Amount__c = (a.Service_Amount__c / billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice / billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c / billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 3);
					a.End_Date__c = a.ServiceDate.addMonths(enddate);
				}

			}else if(a.PB__c == 'Annual'){
				if(a.Billing_Period__c == 'Annual'){
					billcalc = 1;
					a.Billing_Amount__c = (a.Service_Amount__c * billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice * billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c * billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 1);
					a.End_Date__c = a.ServiceDate.addYears(enddate);
				}else if(a.Billing_Period__c == 'Monthly'){
					billcalc = 12;
					a.Billing_Amount__c = (a.Service_Amount__c / billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice / billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c / billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 1);
					a.End_Date__c = a.ServiceDate.addYears(enddate);
				}else if(a.Billing_Period__c == 'Every Other Month'){
					billcalc = 6;
					a.Billing_Amount__c = (a.Service_Amount__c / billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice / billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c / billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 1);
					a.End_Date__c = a.ServiceDate.addYears(enddate);
				}else if(a.Billing_Period__c == 'Quarterly'){
					billcalc = 4;
					a.Billing_Amount__c = (a.Service_Amount__c / billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice / billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c / billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 1);
					a.End_Date__c = a.ServiceDate.addYears(enddate);
				}else if(a.Billing_Period__c == 'Semi-Annual'){
					billcalc = 2;
					a.Billing_Amount__c = (a.Service_Amount__c / billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice / billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c / billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 1);
					a.End_Date__c = a.ServiceDate.addYears(enddate);
				}

			}else if(a.PB__c == 'One Time' || a.PB__c == 'One Time D'){
				if(a.Billing_Period__c == 'Monthly'){
					billcalc = 1;
					a.Billing_Amount__c = (a.Service_Amount__c * billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice * billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c * billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 1);
					a.End_Date__c = a.ServiceDate.addDays(enddate);
				}

			}

		}


	}

TEST CLASS
 
/**
 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 *
 * Unit tests are class methods that verify whether a particular piece
 * of code is working properly. Unit test methods take no arguments,
 * commit no data to the database, and are flagged with the testMethod
 * keyword in the method definition.
 *
 * All test methods in an organization are executed whenever Apex code is deployed
 * to a production organization to confirm correctness, ensure code
 * coverage, and prevent regressions. All Apex classes are
 * required to have at least 75% code coverage in order to be deployed
 * to a production organization. In addition, all triggers must have some code coverage.
 * 
 * The @isTest class annotation indicates this class only contains test
 * methods. Classes defined with the @isTest annotation do not count against
 * the organization size limit for all Apex scripts.
 *
 * See the Apex Language Reference for more information about Testing and Code Coverage.
 */
@isTest(SeeAllData=true)
private class testtest {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test

        Date now = date.today();
        integer day = now.day();
        
        // CREATE ACCOUNT
        Account newAccount = new Account(Name = 'Test Account', Phone='817-999-9999' );
        insert newAccount;

         // CREATE CONTACT
        Contact newContact = new Contact(LastName = 'Test Contact', AccountId = newAccount.Id );
        insert newContact; 

        // CREATE OPPORTUNITY
        Opportunity newOpp = new Opportunity(Name = 'Test Opportunity', CloseDate = System.today(), AccountId = newAccount.Id, Contact_Name__c = newContact.Id, StageName = 'Initial Interview' );
        insert newOpp;
        
        // GET STANDARD PRICEBOOK
		Pricebook2  standardPb = [select id, name, isActive from Pricebook2 where IsStandard = true limit 1];

		// CREATE CUSTOM PRICEBOOK
		Pricebook2 pbk1 = new Pricebook2 (Name='Test Pricebook Entry 1',Description='Test Pricebook Entry 1', isActive=true);
		insert pbk1;

		// CREATE PRODUCT
		Product2 prd1 = new Product2 (Name='Test Product Entry 1',Description='Test Product Entry 1',productCode = 'ABC', isActive = true, Pricing_Basis__c = 'Annual' );
		insert prd1;

		// CREATE PRICEBOOKENTRY
		PricebookEntry pbe1 = new PricebookEntry (Product2ID=prd1.id,Pricebook2ID=standardPb.id,UnitPrice=50, isActive=true);
		insert pbe1;


        // CREATE OPPORTUNITYPRODUCTS
        OpportunityLineItem newProdAnnual = new OpportunityLineItem (Billing_Period__c = 'Annual', OpportunityId = newOpp.Id, Service_Duration__c = 1, ServiceDate = now, End_Date__c = now, PricebookEntryId = pbe1.Id, No_Ordered__c = 1, UnitPrice = 50, Quantity = 1);       
        insert newProdAnnual;

        //QUERY
        OpportunityLineItem a =[SELECT ID, Quantity, TotalPrice, Discount, UnitPrice, Billing_Amount__c, List_Price_Svc__c, List_Price_Bill__c, List_Price_Extd__c, Billing_Savings__c, Per_Item_Bill__c, Discount_Amount_Bill__c, Service_Savings__c, Per_Item_Svc__c, Discount_Amount_Svc__c, End_Date__c, ServiceDate from OpportunityLineItem WHERE ID =: newProdAnnual.Id ];


        //VALIDATE DATA WAS UPDATED CORRECTLY

		//Annual
		System.assertEquals(a.Quantity, 1);
		System.assertEquals(a.TotalPrice, 50.00);
		System.assertEquals(a.Discount, 0);
		System.assertEquals(a.Billing_Amount__c, 50.00);
		System.assertEquals(a.List_Price_Svc__c, 50.00);
		System.assertEquals(a.List_Price_Bill__c, 50.00);
		System.assertEquals(a.List_Price_Extd__c, 50.00);
		System.assertEquals(a.Billing_Savings__c, NULL);
		System.assertEquals(a.Per_Item_Bill__c, 50.00);		
		System.assertEquals(a.Discount_Amount_Bill__c, 0.00);
		System.assertEquals(a.Service_Savings__c, NULL);
		System.assertEquals(a.Per_Item_Svc__c, 50.00);
		System.assertEquals(a.Discount_Amount_Svc__c, 0.00);
		System.assertEquals(a.End_Date__c, a.ServiceDate.addYears(1));

    }
}



 
  • March 03, 2015
  • Like
  • 1
I have a trigger I wrote that works great when I test it in the UI, but my test is failing.  I get the following error:
Error Message	System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, triggerTest: execution of BeforeInsert

caused by: System.NullPointerException: Argument cannot be null.

Trigger.triggerTest: line 294, column 1: []
Stack Trace	Class.testtest.myUnitTest: line 61, column 1
If I'm understanding this correctly it says its failing when it tries to insert the opportunitylineitem because when the trigger tries to do "a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);" and it thinks No_Ordered__c or ListPrice is NULL.  I get this error whether or not I use a the test product or a real product already in the system that I know works fine when I test in the UI.  Please tell me what I am doing wrong.  Any help is appreciated.  Here is the code for the trigger and the test.  I trimmed down the test to test only one product.

TRIGGER
trigger triggerTest on OpportunityLineItem (before insert, before update) {

		decimal disc;
		decimal sub;
		decimal servamt;
		decimal billcalc;
		Integer quartercalc;
		Integer enddate;

		for(OpportunityLineItem a : Trigger.New){
			a.Quantity = (a.No_Ordered__c * a.Service_Duration__c);
			sub = (a.Quantity * a.UnitPrice);
			if(a.Discount != NULL){
				disc = (a.Discount * .01);
				a.TotalPrice = (sub - (sub * disc));
			}else{
				a.TotalPrice = sub;	
			}
			servamt = (a.Quantity / a.No_Ordered__c);
			a.Service_Amount__c = (a.TotalPrice / servamt);
			if(a.PB__c == 'Weekly'){
				if(a.Billing_Period__c == 'Weekly'){
					billcalc = 1;
					a.Billing_Amount__c = (a.Service_Amount__c * billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice * billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c * billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 7);
					a.End_Date__c = a.ServiceDate.addDays(enddate);
				}else if(a.Billing_Period__c == 'Quarterly'){
					billcalc = 13;
					a.Billing_Amount__c = (a.Service_Amount__c * billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice * billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c * billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 7);
					a.End_Date__c = a.ServiceDate.addDays(enddate);				
				}else if(a.Billing_Period__c == 'Semi-Annual'){
					billcalc = 26;
					a.Billing_Amount__c = (a.Service_Amount__c * billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice * billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c * billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 7);
					a.End_Date__c = a.ServiceDate.addDays(enddate);				
				}else if(a.Billing_Period__c == 'Annual'){
					billcalc = 52;
					a.Billing_Amount__c = (a.Service_Amount__c * billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice * billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c * billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);				
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 7);
					a.End_Date__c = a.ServiceDate.addDays(enddate);							
				}

			}else if(a.PB__c == 'Monthly' || a.PB__c == 'Monthly D'){
				if(a.Billing_Period__c == 'Monthly'){
					billcalc = 1;
					a.Billing_Amount__c = (a.Service_Amount__c * billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice * billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c * billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 1);
					a.End_Date__c = a.ServiceDate.addMonths(enddate);
				}else if(a.Billing_Period__c == 'Every Other Month'){
					billcalc = 2;
					a.Billing_Amount__c = (a.Service_Amount__c * billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice * billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c * billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 1);
					a.End_Date__c = a.ServiceDate.addMonths(enddate);
				}else if(a.Billing_Period__c == 'Quarterly'){
					billcalc = 3;
					a.Billing_Amount__c = (a.Service_Amount__c * billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice * billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c * billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 1);
					a.End_Date__c = a.ServiceDate.addMonths(enddate);
				}else if(a.Billing_Period__c == 'Semi-Annual'){
					billcalc = 6;
					a.Billing_Amount__c = (a.Service_Amount__c * billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice * billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c * billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 1);
					a.End_Date__c = a.ServiceDate.addMonths(enddate);
				}else if(a.Billing_Period__c == 'Annual'){
					billcalc = 12;
					a.Billing_Amount__c = (a.Service_Amount__c * billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice * billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c * billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 1);
					a.End_Date__c = a.ServiceDate.addMonths(enddate);
				}

			}else if(a.PB__c == 'Quarterly'){
				if(a.Billing_Period__c == 'Quarterly'){
					billcalc = 1;
					a.Billing_Amount__c = (a.Service_Amount__c * billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice * billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c * billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 3);
					a.End_Date__c = a.ServiceDate.addMonths(enddate);
				}else if(a.Billing_Period__c == 'Semi-Annual'){
					billcalc = 2;
					a.Billing_Amount__c = (a.Service_Amount__c * billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice * billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c * billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 3);
					a.End_Date__c = a.ServiceDate.addMonths(enddate);
				}else if(a.Billing_Period__c == 'Annual'){
					billcalc = 4;
					a.Billing_Amount__c = (a.Service_Amount__c * billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice * billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c * billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 3);
					a.End_Date__c = a.ServiceDate.addMonths(enddate);
				}else if(a.Billing_Period__c == 'Monthly'){
					billcalc = 3;
					a.Billing_Amount__c = (a.Service_Amount__c / billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice / billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c / billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 3);
					a.End_Date__c = a.ServiceDate.addMonths(enddate);
				}

			}else if(a.PB__c == 'Annual'){
				if(a.Billing_Period__c == 'Annual'){
					billcalc = 1;
					a.Billing_Amount__c = (a.Service_Amount__c * billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice * billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c * billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 1);
					a.End_Date__c = a.ServiceDate.addYears(enddate);
				}else if(a.Billing_Period__c == 'Monthly'){
					billcalc = 12;
					a.Billing_Amount__c = (a.Service_Amount__c / billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice / billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c / billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 1);
					a.End_Date__c = a.ServiceDate.addYears(enddate);
				}else if(a.Billing_Period__c == 'Every Other Month'){
					billcalc = 6;
					a.Billing_Amount__c = (a.Service_Amount__c / billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice / billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c / billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 1);
					a.End_Date__c = a.ServiceDate.addYears(enddate);
				}else if(a.Billing_Period__c == 'Quarterly'){
					billcalc = 4;
					a.Billing_Amount__c = (a.Service_Amount__c / billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice / billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c / billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 1);
					a.End_Date__c = a.ServiceDate.addYears(enddate);
				}else if(a.Billing_Period__c == 'Semi-Annual'){
					billcalc = 2;
					a.Billing_Amount__c = (a.Service_Amount__c / billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice / billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c / billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 1);
					a.End_Date__c = a.ServiceDate.addYears(enddate);
				}

			}else if(a.PB__c == 'One Time' || a.PB__c == 'One Time D'){
				if(a.Billing_Period__c == 'Monthly'){
					billcalc = 1;
					a.Billing_Amount__c = (a.Service_Amount__c * billcalc);
					a.List_Price_Svc__c = (a.No_Ordered__c * a.ListPrice);
					a.List_Price_Bill__c = (a.ListPrice * billcalc);
					a.List_Price_Extd__c = (a.List_Price_Svc__c * billcalc);
					a.Billing_Savings__c = (a.Billing_Amount__c - a.List_Price_Extd__c);
					if(a.Billing_Savings__c >= 0){
						a.Billing_Savings__c = NULL;
					}
					a.Per_Item_Bill__c = (a.Billing_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Bill__c = (a.List_Price_Bill__c - a.Per_Item_Bill__c);
					a.Service_Savings__c = (a.Service_Amount__c - a.List_Price_Svc__c);
					if(a.Service_Savings__c >= 0){
						a.Service_Savings__c = NULL;
					}
					a.Per_Item_Svc__c = (a.Service_Amount__c / a.No_Ordered__c);
					a.Discount_Amount_Svc__c = (a.ListPrice - a.Per_Item_Svc__c);
					enddate = (a.Service_Duration__c.intValue() * 1);
					a.End_Date__c = a.ServiceDate.addDays(enddate);
				}

			}

		}


	}

TEST CLASS
 
/**
 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 *
 * Unit tests are class methods that verify whether a particular piece
 * of code is working properly. Unit test methods take no arguments,
 * commit no data to the database, and are flagged with the testMethod
 * keyword in the method definition.
 *
 * All test methods in an organization are executed whenever Apex code is deployed
 * to a production organization to confirm correctness, ensure code
 * coverage, and prevent regressions. All Apex classes are
 * required to have at least 75% code coverage in order to be deployed
 * to a production organization. In addition, all triggers must have some code coverage.
 * 
 * The @isTest class annotation indicates this class only contains test
 * methods. Classes defined with the @isTest annotation do not count against
 * the organization size limit for all Apex scripts.
 *
 * See the Apex Language Reference for more information about Testing and Code Coverage.
 */
@isTest(SeeAllData=true)
private class testtest {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test

        Date now = date.today();
        integer day = now.day();
        
        // CREATE ACCOUNT
        Account newAccount = new Account(Name = 'Test Account', Phone='817-999-9999' );
        insert newAccount;

         // CREATE CONTACT
        Contact newContact = new Contact(LastName = 'Test Contact', AccountId = newAccount.Id );
        insert newContact; 

        // CREATE OPPORTUNITY
        Opportunity newOpp = new Opportunity(Name = 'Test Opportunity', CloseDate = System.today(), AccountId = newAccount.Id, Contact_Name__c = newContact.Id, StageName = 'Initial Interview' );
        insert newOpp;
        
        // GET STANDARD PRICEBOOK
		Pricebook2  standardPb = [select id, name, isActive from Pricebook2 where IsStandard = true limit 1];

		// CREATE CUSTOM PRICEBOOK
		Pricebook2 pbk1 = new Pricebook2 (Name='Test Pricebook Entry 1',Description='Test Pricebook Entry 1', isActive=true);
		insert pbk1;

		// CREATE PRODUCT
		Product2 prd1 = new Product2 (Name='Test Product Entry 1',Description='Test Product Entry 1',productCode = 'ABC', isActive = true, Pricing_Basis__c = 'Annual' );
		insert prd1;

		// CREATE PRICEBOOKENTRY
		PricebookEntry pbe1 = new PricebookEntry (Product2ID=prd1.id,Pricebook2ID=standardPb.id,UnitPrice=50, isActive=true);
		insert pbe1;


        // CREATE OPPORTUNITYPRODUCTS
        OpportunityLineItem newProdAnnual = new OpportunityLineItem (Billing_Period__c = 'Annual', OpportunityId = newOpp.Id, Service_Duration__c = 1, ServiceDate = now, End_Date__c = now, PricebookEntryId = pbe1.Id, No_Ordered__c = 1, UnitPrice = 50, Quantity = 1);       
        insert newProdAnnual;

        //QUERY
        OpportunityLineItem a =[SELECT ID, Quantity, TotalPrice, Discount, UnitPrice, Billing_Amount__c, List_Price_Svc__c, List_Price_Bill__c, List_Price_Extd__c, Billing_Savings__c, Per_Item_Bill__c, Discount_Amount_Bill__c, Service_Savings__c, Per_Item_Svc__c, Discount_Amount_Svc__c, End_Date__c, ServiceDate from OpportunityLineItem WHERE ID =: newProdAnnual.Id ];


        //VALIDATE DATA WAS UPDATED CORRECTLY

		//Annual
		System.assertEquals(a.Quantity, 1);
		System.assertEquals(a.TotalPrice, 50.00);
		System.assertEquals(a.Discount, 0);
		System.assertEquals(a.Billing_Amount__c, 50.00);
		System.assertEquals(a.List_Price_Svc__c, 50.00);
		System.assertEquals(a.List_Price_Bill__c, 50.00);
		System.assertEquals(a.List_Price_Extd__c, 50.00);
		System.assertEquals(a.Billing_Savings__c, NULL);
		System.assertEquals(a.Per_Item_Bill__c, 50.00);		
		System.assertEquals(a.Discount_Amount_Bill__c, 0.00);
		System.assertEquals(a.Service_Savings__c, NULL);
		System.assertEquals(a.Per_Item_Svc__c, 50.00);
		System.assertEquals(a.Discount_Amount_Svc__c, 0.00);
		System.assertEquals(a.End_Date__c, a.ServiceDate.addYears(1));

    }
}



 
  • March 03, 2015
  • Like
  • 1
Hello- I'm new to visualforce and this is my first project.  Through the online guides and examples I have been able to accomplish almost everything.  I'm trying to increase my code coverage.  It is currently at 66%.

This is my controller:
public with sharing class CommentsController {

	private final Case cas;
		   
    //
    public String MasterRecordId        {get; set;}
    public List<Case_Comments__c> ChildRecords  {get; set;}
    public Case MasterRecord                 {get; set;}

    //SET UP PAGE
    public CommentsController(ApexPages.StandardController stdController) {
    		this.cas = (Case)stdController.getRecord();
		MasterRecordId = ApexPages.currentPage().getParameters().get('id');

		 //CHECK FOR ID
        if(!String.isBlank(MasterRecordId)){

            //GET PARENT
            MasterRecord =
                [
                    SELECT 
                        Id,
                        CaseNumber
                    FROM
                        Case
                    WHERE
                        Id = :MasterRecordId
                ];

            //GET CHILD RECORDS
            ChildRecords =
                [
                    SELECT 
                        Name,
						Id,
                        CreatedDate,
                        CreatedBy.Id,
                        CreatedBy.Name,
                        Time_Spent__c,
                        Comment__c,
                        ParentId__c
                    FROM
                        Case_Comments__c
                    WHERE 
                        ParentId__c = :MasterRecordId order by CreatedDate desc
                ];
        }
    }

}

This is my test class:

@isTest
	public class testCommentsController {
    	static testMethod void testFirstTest() {

        // CREATE ACCOUNT
		Account newAccount = new Account(Name = 'Test Account', Phone='817-999-9999');
		insert newAccount;
      
        // CREATE CONTACT
		Contact newContact = new Contact(LastName = 'Test Contact', AccountId = newAccount.Id);
		insert newContact;  
		       		        
        // CREATE CASE
        Case newCase = new Case(Subject = 'Case Subject', Description = 'Case Description', ContactId = newContact.Id, AccountId = newAccount.Id);
        insert newCase;  
        
        // CREATE CASE COMMENT
        Case_Comments__c newComment = new Case_Comments__c(ParentId__c = newCase.Id, Comment__c = 'Test Comment Body', Time_Spent__c = .5);
        insert newComment;

        ApexPages.StandardController sc = new ApexPages.StandardController(newCase);
        CommentsController cc = new CommentsController (sc);

        PageReference pageRef = Page.Comments;
        pageRef.getParameters().put('id', String.valueOf(newCase.Id));
        Test.setCurrentPage(pageRef);
			
        }
}

I can tell from the test coverage highlights that the case "MasterRecord" and the list "ChildRecords" from the controller are not covered.  I assume that I would need to assert that MasterRecord = the case, and that ChildRecords ParentId__c = MasterRecord Id.  I do not know how to do that.  I do not know how to reference them in my test class or if that is right.  Any help would be appreciated.

  • August 14, 2014
  • Like
  • 0

I'm new and learning Apex.  I'm having an issue with a trigger I wrote.  It hits the script statements govenor and causes the error "System.LimitException: Too many script statements: 200001".  It works with single updates. The error was caused when I tested it by updating 2,000 opportunity records and started after 937 were succesfully updated.

 

There are three revenue fields on Opportunity (Monthly, Prorated, Membership).  When an opportunity is updated, the trigger searchs through its products and determines which field the revenue belongs to using the product description.

 

trigger Test2 on Opportunity (before insert, before update) {

// CREATE PRODUCT LIST FOR THE OPPORTUNITIES
List<OpportunityLineItem> prodList = [select ID, OpportunityId, Description, TotalPrice from OpportunityLineItem where OpportunityId in: Trigger.new];

// DEFINE STRINGS USED TO DETERMINE TYPE OF PRODUCT
String Prorated = 'Prorated';
String Membership = 'Membership';

// LOOP THROUGH OPPORTUNITIES, RESET 3 REVENUE FIELDS TO 0
	for(Opportunity a : Trigger.new){
		a.Prorated_Total__c = 0;
		a.Membership_Total__c = 0;
		a.Monthly_Total__c = 0;

// LOOP THROUGH PRODUCT LIST TO GET REVENUE TYPE AND TOTAL FOR CURRENT OPPORTUNITY
		for(OpportunityLineItem b : prodList){

//  MAKE SURE PRODUCTS BELONG TO CURRENT OPPORTUNITY
			if(a.Id == b.OpportunityId){
// DETERMINES PRODUCT TYPE FROM DESCRIPTION AND ADDS PRICE TO THE CORRESPONDING FIELD ON THE CURRENT OPPORTUNITY
		    	     if(b.Description.contains(Prorated)){
		     	     a.Prorated_Total__c += b.TotalPrice;         
		   	     }else if(b.Description.contains (Membership)){        
			     a.Membership_Total__c += b.TotalPrice; 
		    	     }else{
		             a.Monthly_Total__c += b.TotalPrice;   
	    		     }
			}   
		}
	}
}

  After looking up the error, I'm sure the issue is with the nested loop. Everything else I tried failed and I'm struggling.  Any help is appreciated, thanks.

  • January 07, 2013
  • Like
  • 0

I need help with a trigger I wrote.  It works fine with one record updates but it hits the govenor limits when a batch is procesesd.  I understand that it is because I have the SOQL query in a for loop which is causing the query to fire multiple times when a batch is processed.  The issue is I am not sure how to write it where the Query is outside the loop and the trigger will still work and how to "Bulkify" the portion where I am updating BillingCity and BillingZip to handle the mutiple records in the query.  I'm a beginner/learning and any help would be appreciated.

 

trigger triggerAccountCityState on Account (before insert, before update) {

// IF THE ACCOUNT RECORD BEING INSERTED/UPDATED HAS A BILLINGPOSTALCODE AND DOES NOT HAVE A BILLINGCOUNTRY OF "FOR", EXECUTE TRIGGER
    for(Account acct:Trigger.new){
        if(acct.BillingPostalCode != null && acct.BillingCountry != 'FOR'){     
            string zipcode = acct.BillingPostalCode;
            string zipcity;
            string zipstate;
            
// QUERY THE CUSTOM sOBJECT "ZIP CODE" TO GET THE CITY AND STATE FOR THE ZIP CODE BEING INSERTED/UPDATED
            List <Zip_Code__c> zipList = [select Name, City__c, State__c from Zip_Code__c where Name = :zipcode];

// REPLACE THE EXISTING CITY AND STATE WITH THE DATA RETURNED FROM THE QUERY
                for(Zip_Code__c z : zipList){
                zipcity = z.City__c;
                zipstate = z.State__c;
                }
            acct.BillingCity = zipcity;
            acct.BillingState = zipstate;

// IF NOTHING WAS RETURNED/UDPATED GIVE THE INVALID ZIP CODE ERROR
            for (Integer i = 0; i < Trigger.new.size(); i++) {
                if (Trigger.new[i].BillingCity == null){
                    Trigger.new[i].addError('The Zip Code you have entered is not valid.');
                }
            }
           }

    }

}
  • October 04, 2012
  • Like
  • 0

Hello, I am new to apex.  I'm trying to make a trigger that updates the unit price on an opportunity's products marked with a specific discount when it is set to a certain stage.  For testing purposes I have made a trigger that tries to update the marked prices anytime the opportunity is updated. 

 

The problem I am running into is that I can't get the updated prices to save.  I put debug statements in so I could see what was happening and everything looks ok to me.  I have posted my trigger code and the debug log results below.  Any help would be appreciated.

 

CODE:

 

trigger DEVProrateTrigger on Opportunity (before insert, before update) {

//GET TODAYS DATE AND SUBTRACT IT FROM 30 TO GET THE INTEGER TO DIVIDE THE PRICE BY, "PRICEDIV"
Date now = date.today();
integer day = now.day();
integer month = 30;
integer pricediv = month - day;

//GET A LIST OF THE PRODUCTS FOR THE OPPORTUNITY BEING UPDATED "PRODLIST"
List<OpportunityLineItem> prodList = [select ID, OpportunityId, Price_Adjustment_Reason_Code__c, UnitPrice from OpportunityLineItem where OpportunityId in: Trigger.new];

//DEBUG STATMENT #1 TO SHOW ME THE PULLED LIST OF PRODUCTS, "PRODLIST"
System.Debug(prodList);

//LOOP THROUGH PULLED "PRODLIST" TO FIND ANY WITH A PRICE ADJUSTMENT OF PRORATE AND DIVDE THEIR UNIT PRICE BY THE INTEGER "PRICEDEV"
for(OpportunityLineItem a : prodList){
    if(a.Price_Adjustment_Reason_Code__c == 'PI = Prorated Item'){
        a.UnitPrice = (a.UnitPrice / pricediv);
    }
}

//DEBUG STATEMENT #2 TO SHOW ME "PRODLIST" WITH THE UPDATED VALUES
System.Debug(prodList);

//UPDATE/SAVE THE UPDATED "PRODLIST" TO THE DATABASE
Database.Saveresult[] results =Database.update(prodList);

//DEBUG STATEMENT #3 TO SHOW ME THAT "PRODLIST" WAS UPDATED/SAVED CORRECTLY
System.Debug(results);
}

 

 

DEBUG LOG STATEMENTS

 

#1 - 13:45:16.176 (176394000)|USER_DEBUG|[10]|DEBUG|(OpportunityLineItem:{Price_Adjustment_Reason_Code__c=PI = Prorated Item, OpportunityId=006W0000002VguoIAC, Id=00kW0000002zs2iIAA, UnitPrice=50.00})  -This statement shows the correct data was pulled.

 

#2 - 13:45:16.177 (177312000)|USER_DEBUG|[17]|DEBUG|(OpportunityLineItem:{Price_Adjustment_Reason_Code__c=PI = Prorated Item, OpportunityId=006W0000002VguoIAC, Id=00kW0000002zs2iIAA, UnitPrice=2.38095238095238095238095238095238})  -This statement shows that the unitprice in the list was updated from the loop.

 

#3 - 13:45:16.347 (347895000)|USER_DEBUG|[20]|DEBUG|(Database.SaveResult[getErrors=();getId=00kW0000002zs2iIAA;isSuccess=true;]) -This shows that the updated list with the updated price was saved to the database correctly.

  • June 13, 2012
  • Like
  • 0