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 

This is trigger and i make its test class but not running and code coverage is 0 how we increse test class code coverage plese tell me

Trigger
_______________________
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);
                    }
                }
            }
        }
    }
}

____________________________________

TEST CLASS

@isTest
     public class Opportunity invoiceTest
    {
     static testMethod void insertOpportunity()
    {
      Account objAccount = new Account();
      objAccount.Name = 'Vivek';
      objAccount.Type = 'Supplier';
      objAccount.Phone = '1234';
      objAccount.CurrencyIsoCode = 'AUD';
      insert objAccount;
     
      opportunity objopportunity= new opportunity ();
      objopportunity.Name = 'Hindi2014';
      objopportunity.CloseDate = system.Today();
      objopportunity.Account = 'objAccount';
      objopportunity.Vendor__c = 'objaccount.id';
      objopportunity.StageName = 'Closed Won - Signed';
      objopportunity.Billing_Terms__c = 'On Completion';              
      objopportunity.Type = 'New Business';
      insert Objopportunity;
    
     
     
   opportunitylineitem objopportunitylineitem = new opportunitylineitem();
    insert objopportunitylineitem ;
   
    Invoiceobjinvoice=new Invoice();
    objInvoices.Account_Invoice__c='Neo@Ogilvy';
    objInvoices.Amount__c='2400';
    objInvoices.Invoice_link__c='google';
    objInvoices.Invoice_Status__c='Paid';
    objInvoices.Opportunity_invoices__c='DD Test 2015';
    objInvoices.Site_or_Partner__c='RAP';
    Invoiceobjinvoice;
   
  }
}


 

ShashForceShashForce
Hi,

Did you "Run Tests"? If you still see no coverage after running tests, that is probably because you are inserting a opportunitylineitem test record "after" inserting opportunity. In the test method, please try to do an update on the same opportunity that you created, after the opportunitylineitem is inserted, and use some system.assert() statements as a best practice.

Thanks,
Shashank