+ Start a Discussion
Orion@gmail.comOrion@gmail.com 

Writing Test Method Help!

Please help write test code for the following trigger:

 

 

trigger ApplyContractDiscount on QuoteLineItem (before insert, before update) 
{         
    for (QuoteLineItem isContract:trigger.new) 
    {      
        // Build SQL string returning the contract with the highest end date
        string strSQL1 = 'SELECT c.AccountId, c.Account.Id, c.Discount_Level__c, c.EndDate FROM Contract c WHERE c.AccountId = ';
        strSQL1 += '\'';
        strSQL1 += isContract.Account_ID_Formula__c;
        strSQL1 += '\'';
        strSQL1 += ' ORDER BY c.EndDate DESC LIMIT 1';
            
        List<Contract> L = Database.query(strSQL1);
    
        if (L.size() > 0) //Does a contract exist?
        {
            Contract A = Database.query(strSQL1);
            isContract.Discount = A.Discount_Level__c;
        }

        isContract.UnitPrice = isContract.ListPrice;
        isContract.Discount = 0;
    }
}

 

 

mikefmikef

In your test class insert a QuoteLineItem and then query for the related contract and then assert test for the value of UnitPrice and Discount.

 

BUT before you do that you should bulk your trigger. You have two queries in a for loop, so you can only bulk upload 9 QuoteLineItem records with the data loader before you hit the query limit.

 

There are lots of examples of bulking your trigger, plus it will improve performance.