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
vivek singh08481200707119766vivek singh08481200707119766 

Please help ,this is my trigger how we make its test class

trigger makeInvoice on Opportunity (after insert, after update) {
   
    for(Opportunity op :trigger.new){
        if(op.StageName == 'Closed Won - Signed'){
           List<OpportunityLineItem> opli = new List<OpportunityLineItem>();
            opli = [select id,Gross__c, Product2id, OpportunityId from OpportunityLineItem where OpportunityId =: op.id order by Product2id];
            if(opli.size() > 0){
                String tmpId = '';
                List<Invoices__c> lstInvoice = new List<Invoices__c>();
                Invoices__c objInvoice = new Invoices__c();
                Integer lm = opli.size();
                Integer i = 1;
                for(OpportunityLineItem ob: opli){
                    if(tmpId == ''){
                        tmpId = ob.Product2id;
                        objInvoice = new Invoices__c();   
                        objInvoice.Opportunity_invoices__c = ob.OpportunityId;
                        objInvoice.Amount__c = ob.Gross__c;                                      
                    }else{
                        if(tmpId == ob.Product2id){
                            objInvoice.Amount__c = objInvoice.Amount__c + ob.Gross__c;
                        }else{
                            lstInvoice.add(objInvoice);
                            tmpId = ob.Product2id;
                            objInvoice = new Invoices__c();   
                            objInvoice.Opportunity_invoices__c = ob.OpportunityId;
                            objInvoice.Amount__c = ob.Gross__c;                           
                        }     
                    }
                    if(i == lm){
                        lstInvoice.add(objInvoice);
                    }
                    i++;
                }
                if(lstInvoice.size() > 0){
                    try{
                        insert lstInvoice;
                        system.debug('!Lst '+lstInvoice);
                    }catch(Exception e){
                        system.debug('!Bingo '+e);
                    }
                }
            }
        }
    }
}
Best Answer chosen by vivek singh08481200707119766
Vinit_KumarVinit_Kumar
Vivek,

I see couple of things in your code which are against the best practices of salesforce :-


1.) You are running your SOQL inside a For loop which is against the best practices.
2.) You are performing DML again inside a For loop whihch is also not recommended.

So,you must move these statements outside the For loop.

As per your test class is concerned,you just need to insert an opportunity record where StageName='Closed Won - Signed' and you are good to go,something like below :-

@IsTest(SeeAllData=false)
public class makeInvoice_test
{
       static testmethod void myUnitTest()
       {
           Opportunity opp =new Opportunity(Name='Test opp',StgeName='Closed Won - Signed',CloseDat=system.today());
//Populate all the field required to insert Opportunit record
		   Test.StartTest();
		   insert opp;
		   Test.StopTest();
       }

}
If this helps,please mark it as best answer to help others :)