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 tell me how we make this trigger test class ,i make but no coverage increse this time its coverage is 0(zero)

“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);
                    }
                }
            }
        }
    }
}”
Daniel B ProbertDaniel B Probert
you basically in the first instance need to enure you are creating the matching createria required for verifing that this trigger is going to work.

@isTest
public class Test_OppTrg{
    public static testMethod void TestTrgOpp(){

    Account acc = new account(name='demo);
   insert acc;
  opportunity opp = new opportunity(accountid=acc.id,amount=15.00,stagename='Close Won Signed' //add additional required fields)
 insert opp;
opp.amount = 25.00;
update opp;
 opportunitylineitem oppline = new opportunitylineitem(opportunityid=opp.id, //additional field that you need);
 insert oppline;

//this should be enough to get you some coverage for complete coverage you'll need to test different cases like inserting an opportuntity / updating opp that doesn't have the stagename closed won - signed.
}
}

hope that gets you started..
vivek singh08481200707119766vivek singh08481200707119766
 Hi 
Daniel B Probert ,how we use invoice in test class please send bulkify test class
Daniel B ProbertDaniel B Probert
not sure i follow from what I understand your trigger is checking if the opportunity is closed won then if it is it's creating the invoice.

if that's the case then you don't need to reference this in the test class other than to do a system.assetequals check to verfiy that the invoice has been created?

i noticed i got something wrong in what i've sent though the stage should be set to something else on insert - then the oppline items needs to be add then the opportunity should be updated to the correct stage..

@isTest
public class Test_OppTrg{
    public static testMethod void TestTrgOpp(){

    Account acc = new account(name='demo);
   insert acc;
  opportunity opp = new opportunity(accountid=acc.id,amount=15.00,stagename='pending' //add additional required fields)
 insert opp;

 opportunitylineitem oppline = new opportunitylineitem(opportunityid=opp.id, //additional field that you need);
 insert oppline;

opp.amount = 25.00;
opp.stagebane='closed won - signed';
update opp;

integer invtest = [select count() from Invoice__c];
system.assetequals(invtest, 1);


}
}

i might be wrong though - i don't have an invoice object in my order to test with..