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
rajesh kumar 50rajesh kumar 50 

Test class for the trigger?

i was facing problem with the test class for the following trigger.

trigger opportunityinsertupdate on opportunity(before insert,before update) {
boolean flag=True;
    if(trigger.isInsert) {
         for(opportunity o :[Select Id, Name, Record_Type_Name__c, FS_Included__c, account.Super_Region__c, Check__c, 
                            stagename, amount, CurrencyIsoCode, Target_ShipDate__c
                            From Opportunity Where Id IN :trigger.new]) {
            if(((o.Record_Type_Name__c == 'NC Power')||(o.Record_Type_Name__c == 'NC Oil & Gas')) && (o.FS_Included__c == false) && (o.account.Super_Region__c == 'Asia/India')) {
                o.stagename = 'Sales Lead';
                o.amount = 1;
                o.CurrencyIsoCode = 'USD';
                o.Target_ShipDate__c = o.Target_ShipDate__c.addmonths(3);
                flag = false;
            }
        }
    }
   
    if(trigger.isUpdate && flag)  {
        for(opportunity o1 :[Select Id, Name, Record_Type_Name__c, FS_Included__c, account.Super_Region__c, Check__c, 
                            stagename, amount, CurrencyIsoCode, Target_ShipDate__c
                            From Opportunity Where Id IN :trigger.new]){
            if(((o1.Record_Type_Name__c == 'NC Power')||(o1.Record_Type_Name__c == 'NC Oil & Gas')) && (o1.FS_Included__c == false) && o1.account.Super_Region__c == 'Asia/India' && o1.Check__c == false) {
                o1.stagename = 'Sales Lead';
                o1.amount = 1;
                o1.CurrencyIsoCode = 'USD';
                o1.Target_ShipDate__c = o1.Target_ShipDate__c.addmonths(3);
                o1.Check__c = true;
            }
       
        }
    }
}

can any one tell how to caver this trigger
Tushar sharmaTushar sharma
You need to insert new Record In  opportunity with all your field and then update the record it will cover your trigger code coverage
Balaji BondarBalaji Bondar
Hi Rajesh,
You will not be able to get the 100% code coverage for the trigger due to flag = true.
You have create a class with static variable as below to have the same implemenation and to have 100% code coverage>
 
public class p { 
   public static boolean flag = true; 
}
Important :
If this is what you were looking for then please mark it as a "SOLUTION" or You can Click on the "Like" Button if this was beneficial for you.