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 

Can any one help me writing test class for the trigger?

Below is my trigger: 

trigger opportunityinsertupdate on Opportunity (before insert,before update) {
    if(checkRecursive.runOnce()) {
    Set<Id> accountIds = new Set<Id>();
        for(Opportunity currentOpportunity: Trigger.New) {
            accountIds.add(currentOpportunity.AccountId);
        }

        Map<Id, Account> accountMap = new Map<Id, Account>([Select Id, Super_Region__c from Account Where Id in:accountIds]);
        boolean flag=True;
        
      
 
            if(Trigger.isInsert){
                for(Opportunity opp: trigger.New){
                    system.debug('opp.name.===='+opp.name);
                    if(opp.name != null){
                        if(opp.name.startsWith('FO-')){
                            opp.CampaignId = '701U0000000QsAA';
                        }
                    }    
                 }
                for(opportunity o : trigger.new) {
                    if(accountMap.get(o.AccountId) != null)    {
                 
                        if(((o.Record_Type_Name__c == 'NC Power')||(o.Record_Type_Name__c == 'NC Oil & Gas')||(o.Record_Type_Name__c == 'NC Nuclear')) &&(o.stagename == 'Closed Won') && (o.FS_Included__c == false) && accountMap.get(o.AccountId).Super_Region__c == 'Asia/India') {
                            o.Name = o.Name + '- FS Opp';
                            o.stagename = 'Sales Lead';
                            o.amount = 1;
                            o.CurrencyIsoCode = 'USD';
                            o.Target_ShipDate__c = o.CloseDate.addmonths(3);
                            flag = false;
                            o.FS_Included__c = true;
                        }
                    }    
                    }
                }
                
      }   
         
     if(trigger.isUpdate && flag)  {
            for(opportunity o1:trigger.new) {
                if(accountMap.get(o1.AccountId) != null) {
    
                        if(((o1.Record_Type_Name__c == 'NC Power')||(o1.Record_Type_Name__c == 'NC Oil & Gas')||(o1.Record_Type_Name__c == 'NC Nuclear')) &&(o1.stagename == 'Closed Won') && (o1.FS_Included__c == false) && accountMap.get(o1.AccountId).Super_Region__c == 'Asia/India' && o1.Check__c == false) {
                            o1.Name = o1.Name + '- FS Opp';
                            o1.stagename = 'Sales Lead';
                            o1.amount = 1;
                            o1.CurrencyIsoCode = 'USD';
                            o1.Target_ShipDate__c = o1.CloseDate.addmonths(3);
                            o1.Check__c = true;
                            o1.FS_Included__c = true;
                        }
                }        
                }
            }
        }
}

}
PratikPratik (Salesforce Developers) 
Hi Rajesh,

Here are guidelines to cover the bove mentioned code:

1. Insert account, opportunities records
2. The campaign id, as you have used the hard code value use the same
3. Insert opportunities records with different recordtypes mentioned in If statements like 'NC Power', 'NC Oil & Gas' etc. also Stage value and Super_Region values.
4. Query on those opportunities in the test class
5. Cover both the If and Else part with positive and negative scenarios.

Hope this will help to achieve desired code coverage.

Thanks,
Pratik