function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sreelekhasreelekha 

Regarding Testclass

Hi,

I have written a trigger.But i want to write a testclass for it can you please help.

trigger triglast on QuoteLineItem (after insert,after update) {
public static double total=0;
Set<Id> qtids=new Set<Id>();
List<opportunity> oppl=new List<opportunity>();
List<Quotelineitem> qlil=new List<Quotelineitem>();
for (QuoteLineItem qli : Trigger.new){
   qtids.add(qli.quoteid);
   qlil.add(qli);
   }
  
   Map<ID, Quote> entries = new Map<ID, Quote>( [select id,totalprice,opportunityid,status__c from Quote where id in :qtids]);
   //system.debug(entries);
   //List<Id> qutids=new List<Id>();
   //qutids.add(entries); 

   List<Quote> quotevalues=new List<Quote>();
   quotevalues=entries.values();
   
  
    Opportunity opp=[select id,amount from opportunity where id =:quotevalues[0].opportunityid];
    for(integer i=0;i<qlil.size();i++)
     {
        total=qlil[i].totalprice+total;
     
     }
        if(quotevalues[0].status__c){
        
         opp.amount=quotevalues[0].totalprice+total;
         
        
        }
        
        oppl.add(opp);
        update oppl;
Best Answer chosen by Admin (Salesforce Developers) 
vishal@forcevishal@force

Hi, Basically to cover your trigger codes, all you need is test data that is to be inserted/updated or deleted on the basis of the trigger events.

 

So a test class in which you insert the required test data will be enough as long as your data fulfills all the conditions in your SOQL queries to cover the code.

 

try this code :

 

@isTestprivate class communityTesting
{
    static testMethod void myUnitTest() 
    {            
       // TO DO: implement unit test
        Opportunity opp = new Opportunity();
        opp.Name = 'testOpp';
        opp.StageName = 'Test';
        opp.Amount = 1000;
        opp.CloseDate = system.today();
        insert opp;

        Quote quote = new Quote();
        quote.opportunityid = opp.Id;
        quote.Status__c = true;
        quote.totalprice = 800;
        insert quote;

        QuoteLineItem line = new QuoteLineItem();
        line.quoteid = quote.Id;
        line.totalprice = 500;
        insert line;    
     }
}

 the field values i have given in the test records may change based on the required fields you have in your Org. however once you have properly created test data, your trigger should be covered fine. 

 

Let me know in case of any issues.