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 

I have problem with the test class for the trigger can any one help me out of this?

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 == 'Controls Field Service')) &&(o.stagename == 'Closed Won') && (o.FS_Included__c == false) && accountMap.get(o.AccountId).Super_Region__c == 'Asia/India') {
                            o.stagename = 'Sales Lead';
                            o.amount = 1;
                            o.CurrencyIsoCode = 'USD';
                            o.Target_ShipDate__c = o.CloseDate.addmonths(3);
                            flag = false;
                        }
                    }
                }
                  
                        opp.CampaignId = '701U0000000Qu1b';
                        && (opp.Super_Region__c == 'ASIA/INDIA')
                        system.debug('---------CampaignId-------'+opp.CampaignId); 
                  
                                                                
                  
    }   
  
     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 == 'Controls Field Service')) &&(o1.stagename == 'Closed Won') && (o1.FS_Included__c == false) && accountMap.get(o1.AccountId).Super_Region__c == 'Asia/India' && o1.Check__c == false) {
                            o1.stagename = 'Sales Lead';
                            o1.amount = 1;
                            o1.CurrencyIsoCode = 'USD';
                            o1.Target_ShipDate__c = o1.CloseDate.addmonths(3);
                            o1.Check__c = true;
                        }
                }
            }
        }
}
}


and below is my test calss written for my trigger:

@isTest(SeeAllData=true)
public class TestOppInsertUpdate {
    static testMethod Void OppInsert() {
        RecordType rtid = new RecordType();
        rtid = [select id,name from RecordType where name = 'NC Power' and SobjectType = 'Opportunity'];
        opportunity opp = [SELECT id,name,Record_Type_Name__c,FS_Included__c,account.Super_Region__c,
                            stagename, amount, CurrencyIsoCode, Target_ShipDate__c From Opportunity
                            where recordtypeId =: rtId.Id and account.super_region__c = 'Asia/India' 
                            and FS_Included__c = false];
        Opportunity o = new Opportunity();                    
        o.stagename = 'Sales Lead';
        o.amount = 1;
        o.CurrencyIsoCode = 'USD';
        o.Target_ShipDate__c = o.CloseDate.addmonths(3);
        flag = false;
        
        test.startTest();
        insert o;
        test.stopTest();
    }
    static testMethod Void Oppupdate() {
        RecordType rtid = new RecordType();
        rtid = [select id,name from RecordType where name = 'NC Power' and SobjectType = 'Opportunity'];
        opportunity opp = [SELECT id,name,Record_Type_Name__c,FS_Included__c,account.Super_Region__c,
                            stagename, amount, CurrencyIsoCode, Target_ShipDate__c From Opportunity
                            where recordtypeId =: rtId.Id and account.super_region__c = 'Asia/India' 
                            and FS_Included__c = false];
        Opportunity o = new Opportunity();                    
        o.stagename = 'Sales Lead';
        o.amount = 1;
        o.CurrencyIsoCode = 'USD';
        o.Target_ShipDate__c = o.CloseDate.addmonths(3);
        flag = false;
        check__C = true;
        
        test.startTest();
        update o;
        test.stopTest();
    }
}

I was not able to cover my trigger so can any one help me writting the test class for the above trigger.
PratikPratik (Salesforce Developers) 
Hi Rajesh,

Here are some tips to cover the trigger mentioned above:

1. You have conditional statements i.e. If , try to cover both the parts of conditional statement.
2. Insert the data in the test class which will follow If part as well as else part so you will get maximum coverage.

Thanks,
Pratik

P.S. If this answers you question, please mark it as "Best Answer" so it will help other community members too.
rajesh kumar 50rajesh kumar 50
Thanks for replying but i cat understand how to caver if conditions in test classes
can u suggest me please
thanks in advance
 
PratikPratik (Salesforce Developers) 
Hi Rajesh,

For example:

 if(((o.Record_Type_Name__c == 'NC Power')||(o.Record_Type_Name__c == 'NC Oil & Gas')||(o.Record_Type_Name__c == 'Controls Field Service')) &&(o.stagename == 'Closed Won') && (o.FS_Included__c == false) 

For the above statement, try putting the records with different recorde type name so it will cover the code portion of the class.

Thanks,
Pratik