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
pavan kumar 1864pavan kumar 1864 

how to get 100 % coverage for trigger invoking batch class and batch class has if conditions

This is the batch class..
Global class UPDATINGFROMOPPORTUNITYTOACCOUNT implements database.batchable<sObject>{
    
    public string ferchingaccountrecords;
    public list<Account> listofaccount = new list<Account>();
   
    public UPDATINGFROMOPPORTUNITYTOACCOUNT(list<Account> IdAccount)
    {
        listofaccount = IdAccount;
        ferchingaccountrecords = 'select id,name,Agreement_type__c,    Partner_involved__c,Self_servied__c,Edition_type__c from account where type = \'Customer - Direct\' and id in : listofaccount';
    }
    
    
    Global database.querylocator start (database.BatchableContext bc)
    {
        
        return database.getquerylocator(ferchingaccountrecords);
    }
    
    
    Global void execute(database.BatchableContext bc,list<account>accountfetched)
    {
        
        list<account> newupdatedaccount = new list <account>();
        map<id,opportunity> mapofaccountopportunity = new map<id,opportunity>();
        
        for(opportunity opportunityrecords : [select id,Agreement_type__c,type,accountid,Partner_involved__c,Edition_type__c from opportunity 
                                              where accountid in :accountfetched 
                                              and
                                              (type != 'Services Only' or type !='Paid POC' or type !='Change-Order' or type != 'Amendment')
                                              order by CloseDate desc limit 1]) 
        {
            mapofaccountopportunity.put(opportunityrecords.accountid,opportunityrecords);                         
            
        }
        
        account newaccount = new account();
        
        
        for(id accountid : mapofaccountopportunity.keyset()){
            
            
            opportunity opportunityrecord = mapofaccountopportunity.get(accountid);
            
            if(opportunityrecord.Type=='New Logo' || opportunityrecord.Type=='Upgrade' || opportunityrecord.Type=='Renewal' || opportunityrecord.Type=='Cross-Sell')
            {
                if(opportunityrecord.Partner_involved__c=='True')
                {
                    newaccount.Partner_involved__c =  true;
                    
                }
                else
                {
                    newaccount.Partner_involved__c = false;
                }
            }
            if(opportunityrecord.Edition_type__c!= null)
            {
                newaccount.Edition_type__c = opportunityrecord.Edition_type__c;
            }
            else
            {
                newaccount.Edition_type__c = '';
            }
            
            if(opportunityrecord.Agreement_type__c!= null)
            {
                if(opportunityrecord.Agreement_type__c == 'Self-Service')
                {
                    newaccount.Self_servied__c = true;
                    
                }
                else
                {
                    newaccount.Self_servied__c = false; 
                    newaccount.Agreement_type__c = opportunityrecord.Agreement_type__c;
                }
            }
            else
            {
                newaccount.Self_servied__c = false; 
                newaccount.Agreement_type__c = '';
            }
            
            newaccount.Id = opportunityrecord.AccountId;
            
        }
        
        if(newaccount!=null)
        {
            newupdatedaccount.add(newaccount);
            update newupdatedaccount;
        }
        
    }
    
    Global void finish(database.BatchableContext bc)
    {
        
    }
    
-------------------------------------------------
this is the trigger which is invoking batch class

trigger updateaccountfromopportunity on Account (before update) {
    if(trigger.isexecuting && trigger.isbefore && trigger.isupdate)
    {
        if(!system.isbatch()){    
    list<Account> IdAccount = new list<Account>();
    for(Account acc:trigger.new){
                    IdAccount.add(acc);
            }
    if(IdAccount.size() > 0){
        
        database.executeBatch(new UPDATINGFROMOPPORTUNITYTOACCOUNT(IdAccount)); // Calling batch class.
    }

        }}
}

---------------------------------------------------------
this is the test class

@istest
public class updateaaccountfromopportunity {
    
    static list<account>acountrecordlist = new list<account>();
    @testsetup
    public static void method()
    {
        
        
        account acountrecord = new account();
        acountrecord.name= 'pavabn';
        acountrecord.Agreement_type__c='Order Form';
        acountrecord.Partner_involved__c =True;
        acountrecord.Self_servied__c=True;
        acountrecord.Edition_type__c='Enterprise';
        acountrecord.type='Customer - Direct';
        
        insert acountrecord;
        acountrecordlist.add(acountrecord);
        
        
        opportunity opportunityrecord = new opportunity();
        
        opportunityrecord.name= 'pavabn';
        opportunityrecord.Agreement_type__c='Order Form';
        // aaa.Partner_involved__c =True;
        opportunityrecord.Edition_type__c='Enterprise';
        opportunityrecord.type='Customer - Direct';
        opportunityrecord.accountid= acountrecord.id;
        opportunityrecord.type= 'New Customer';
        opportunityrecord.StageName='Qualification';
        opportunityrecord.CloseDate= system.today()+5;
        opportunityrecord.Stage__c='Stage 3';
        insert opportunityrecord;
        
        account accountquery = [select Agreement_type__c,Self_servied__c,Edition_type__c,Partner_involved__c from account where id = :acountrecord.id];
        
        accountquery.Edition_type__c='professional';
        update accountquery;
        
        opportunity opportunityquery = [select Agreement_type__c,Edition_type__c,type,Partner_involved__c from opportunity where accountid = : accountquery.id];       
        
        account lea = [select Agreement_type__c,Self_servied__c,Edition_type__c,Partner_involved__c from account where id = : accountquery.id];
        
        system.assertEquals('Order Form',lea.Agreement_type__c);
        system.assertEquals(true,lea.Self_servied__c);
        system.assertEquals('professional',lea.Edition_type__c);
        system.assertEquals(true,lea.Partner_involved__c);
        
        
        
        UPDATINGFROMOPPORTUNITYTOACCOUNT ada = new UPDATINGFROMOPPORTUNITYTOACCOUNT(acountrecordlist);
        //   insert aaa;
        
        Test.startTest();
        id aakak = database.executebatch(ada,10);
        
        Test.stopTest(); 
    }
    
    public static testmethod void method2()
    {
        account leads = [select Agreement_type__c,Self_servied__c,Edition_type__c,Partner_involved__c from account];
        
        leads.Edition_type__c='professional';
        update leads;
        
        opportunity opp = [select Agreement_type__c,Edition_type__c,type,Partner_involved__c from opportunity where accountid = : leads.id];
        
        account lea = [select Agreement_type__c,Self_servied__c,Edition_type__c,Partner_involved__c from account where id = : opp.accountid];
        
        system.assertEquals('Order Form',lea.Agreement_type__c);
        system.assertEquals(true,lea.Self_servied__c);
        system.assertEquals('professional',lea.Edition_type__c);
        system.assertEquals(true,lea.Partner_involved__c);
        
        
        
        UPDATINGFROMOPPORTUNITYTOACCOUNT ada = new UPDATINGFROMOPPORTUNITYTOACCOUNT(acountrecordlist);
        //   insert aaa;
        
        Test.startTest();
        id aakak = database.executebatch(ada,10);
        
        Test.stopTest(); 
    }} 
-------------------------------------------------------------------

im not getting to write test class can anyone help me out?
PriyaPriya (Salesforce Developers) 

Hello,

This question has been posted already, so marking it as the duplicate. 

https://developer.salesforce.com/forums/ForumsMain?id=9062I000000DLhU

Regards,

Priya Ranjan

Salesforce Support