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
kiran25kiran25 

How to complete Test class for this scenario

Hi I am New Bie to salesforce development moved from Admin to Development few weeks ago .

I am trying to write a test class for the below code.
 
Public class TriggerHandler_Controller
{         
    
    public void afterInsert(List<RPR__c> objList){
        List<Quote> quoteList = new List<Quote>();
        List<QuoteLineItem> qliList = new List<QuoteLineItem>();
        
        Pricebookentry pbe = [select Id,pricebook2Id,product2Id from PricebookEntry where product2id = '01t28000000cRQsAAM'];
        Id oppId = [select id from opportunity where id =: '0060k00000CuKIw' LIMIT 1].Id;
        
        Set<Id> accId = new Set<Id>();
        List<RPR__c> rprList = new List<RPR__c>();
        for(RPR__c rpr : objList)
        {
            rprList.add(rpr);
            accId.add(rpr.Account__c);
        }
        List<Opportunity> oppList = [SELECT Id,Name FROM Opportunity
                                     WHERE AccountId IN : accId];
        System.debug('Account Set'+accId + ' Opportunity List '+ oppList);
        for(RPR__c rprNew : rprList){
            Quote q = new Quote();
            q.Name = 'Teston New Quote'+rprNew.Name;
            
            q.OpportunityId = oppList[0].Id;
            q.Ship_To_City__c = rprNew.Shipment_From__c;
            q.Pricebook2Id = pbe.pricebook2id;
            quoteList.add(q);
        }
        
        if(!quoteList.isEmpty()){
            insert quoteList;
        }
        
        for(Quote q : quoteList){
            QuoteLineItem qli = new QuoteLineItem();
  
            qli.QuoteId = q.Id;
            qli.quantity = 2;
            qli.pricebookentryId = pbe.id;
            qli.unitprice = 10;
            qli.product2Id = pbe.product2Id;
            qli.Packing_Style__c = q.X1_Packing_Style__c;
            
            qliList.add(qli);
        }
        
        if(!qliList.isEmpty()){
            insert qliList;
        }      
        
        
        
        
    }
    public void beforeUpdate(List<RPR__c> objList){
        
        Set<Id> RPR_Ids = new Set<Id>();
        for(RPR__c obj : objList){
            RPR_Ids.add(obj.Id);
        }
        
        Map<Id, RPR__c> oppMap = new Map<Id, RPR__c>([
            Select (Select IsPending, ProcessInstanceId, TargetObjectId, StepStatus, OriginalActorId, ActorId,Actor.Name, RemindersSent, Comments, IsDeleted, CreatedDate, CreatedById, SystemModstamp 
                    From ProcessSteps where StepStatus IN ('Approved','Rejected') ORDER BY CreatedDate DESC) From RPR__c WHERE Id IN : RPR_Ids]);
        
        for(RPR__c opp: objList) {
            RPR__c opp1 = oppMap.get(opp.Id);
            opp.Approver_Comments__c = '';
            for (ProcessInstanceHistory processStep : opp1.ProcessSteps) {
                opp.Approver_Comments3__c = 'aa'; //This is a dummy value & field only.
                opp.Approver_Comments__c += 'Comments: ' + processStep.comments + ' . Status: ' + processStep.StepStatus + ' . Date: ' + processStep.CreatedDate +' . Commentor Name: ' + processStep.Actor.Name + '\\';
                //opp.Approved_Date__c = DateTime.parse(system.now().format());
                opp.Approver_Comments3__c = 'aa'; //This is a dummy value & field only, DO NOT DELETE, MAY EFFECT REPORT TYPE
            }
            opp.Approver_Comments3__c = 'aa';
        }
        
    }
    
}


The test class with i wrote is showing zero code coverage , please help me out what went wrong and what should be added to get the code coverage for this.

@isTest (SeeAllData=false)
public class TriggerHandlerTest {
    
    @isTest
    public static void test()
    {
        

  RPR__c RPR = new RPR__c();
        //RPR.id = 'a0A0k00000Pk6GgEAJ';
        RPR.Requested_Date__c = Date.newInstance(2010, 07, 15);
        RPR.Approval_Status2__c = 'Draft';
        RPR.Account__c = '0012800000mRNOaAAO';
        RPR.Approver_Comments3__c = 'ok';
        insert RPR;
        
        User submitter = [SELECT Id FROM User WHERE IsActive = true LIMIT 1];
        
        
        //System.runAs(submitter) {
            Approval.ProcessSubmitRequest r = new Approval.ProcessSubmitRequest();
            r.setObjectId(RPR.Id);
            r.setNextApproverIds(new Id[] {UserInfo.getUserId()});
            //r.setNextApproverIds(null);      
            //r.setNextApproverIds('80010000000EAMZ');
            Approval.process(r);
        //}
        
        
        Test.startTest();
        update RPR;
        Test.stopTest();
    }
 






 

CharuDuttCharuDutt
Hii Bhavana
Try Below Test Class
Public class TriggerHandler_Controller
{         
    
    public void afterInsert(List<RPR__c> objList){
        List<Quote> quoteList = new List<Quote>();
        List<QuoteLineItem> qliList = new List<QuoteLineItem>();
        
        Pricebookentry pbe = [select Id,pricebook2Id,product2Id from PricebookEntry where product2id = '01t28000000cRQsAAM'];
        Id oppId = [select id from opportunity where id =: '0060k00000CuKIw' LIMIT 1].Id;
        
        Set<Id> accId = new Set<Id>();
        List<RPR__c> rprList = new List<RPR__c>();
        for(RPR__c rpr : objList)
        {
            rprList.add(rpr);
            accId.add(rpr.Account__c);
        }
        List<Opportunity> oppList = [SELECT Id,Name FROM Opportunity
                                     WHERE AccountId IN : accId];
        System.debug('Account Set'+accId + ' Opportunity List '+ oppList);
        for(RPR__c rprNew : rprList){
            Quote q = new Quote();
            q.Name = 'Teston New Quote'+rprNew.Name;
            
            q.OpportunityId = oppList[0].Id;
            q.Ship_To_City__c = rprNew.Shipment_From__c;
            q.Pricebook2Id = pbe.pricebook2id;
            quoteList.add(q);
        }
        
        if(!quoteList.isEmpty()){
            insert quoteList;
        }
        
        for(Quote q : quoteList){
            QuoteLineItem qli = new QuoteLineItem();
  
            qli.QuoteId = q.Id;
            qli.quantity = 2;
            qli.pricebookentryId = pbe.id;
            qli.unitprice = 10;
            qli.product2Id = pbe.product2Id;
            qli.Packing_Style__c = q.X1_Packing_Style__c;
            
            qliList.add(qli);
        }
        
        if(!qliList.isEmpty()){
            insert qliList;
        }      
        
        
        
        
    }
    public void beforeUpdate(List<RPR__c> objList){
        
        Set<Id> RPR_Ids = new Set<Id>();
        for(RPR__c obj : objList){
            RPR_Ids.add(obj.Id);
        }
        
        Map<Id, RPR__c> oppMap = new Map<Id, RPR__c>([
            Select (Select IsPending, ProcessInstanceId, TargetObjectId, StepStatus, OriginalActorId, ActorId,Actor.Name, RemindersSent, Comments, IsDeleted, CreatedDate, CreatedById, SystemModstamp 
                    From ProcessSteps where StepStatus IN ('Approved','Rejected') ORDER BY CreatedDate DESC) From RPR__c WHERE Id IN : RPR_Ids]);
        
        for(RPR__c opp: objList) {
            RPR__c opp1 = oppMap.get(opp.Id);
            opp.Approver_Comments__c = '';
            for (ProcessInstanceHistory processStep : opp1.ProcessSteps) {
                opp.Approver_Comments3__c = 'aa'; //This is a dummy value & field only.
                opp.Approver_Comments__c += 'Comments: ' + processStep.comments + ' . Status: ' + processStep.StepStatus + ' . Date: ' + processStep.CreatedDate +' . Commentor Name: ' + processStep.Actor.Name + '\\';
                //opp.Approved_Date__c = DateTime.parse(system.now().format());
                opp.Approver_Comments3__c = 'aa'; //This is a dummy value & field only, DO NOT DELETE, MAY EFFECT REPORT TYPE
            }
            opp.Approver_Comments3__c = 'aa';
        }
        
    }
    
}
############################################################
Trigger

Trigger RPRTrigger on RPR__c(After Insert,Before Update){
If(Trigger.IsAfter && Trigger.IsInsert){
TriggerHandler_Controller THC = new TriggerHandler_Controller();
THC.afterInsert(Trigger.New);
}

If(Trigger.IsBefore && Trigger.IsUpdate){
TriggerHandler_Controller THC = new TriggerHandler_Controller();
THC.beforeUpdate(Trigger.New);

}
}
Please Mark It As Best Answer If It Helps
Thank You!
kiran25kiran25
HI CharuDutt,

I think Apex Class was posted in instead of test class . can you resend the comment with Apex test class i already have trigger working with fine 
here . I need Apex test class for the code coverage aspect .









 
CharuDuttCharuDutt
Hii Bhavana
Try below Test Class
@isTest
public class TriggerHandler_ControllerTest {
    @isTest
    public static void unitTestbeforeUpdate(){
        Account Acc = new Account();
        Acc.Name = 'Test Account';
        //Fill All Required Fields
        insert Acc;
        Account Acc2 = new Account();
        Acc2.Name = 'Test Account';
        //Fill All Required Fields
        insert Acc2;
        
        Opportunity opp = new Opportunity();
        opp.Name = 'Test Opportunity';
        opp.StageName = 'Closed Won';
        opp.CloseDate = System.today();
        opp.AccountId = Acc.Id;
        Insert opp;
        
        Opportunity opp1 = new Opportunity();
        opp1.Name = 'Test Opportunity';
        opp1.StageName = 'Closed Won';
        opp1.CloseDate = System.today();
        opp1.AccountId = Acc2.Id;
        Insert opp1;
        
        RPR__c RPR = new RPR__c();
        RPR.Requested_Date__c = Date.newInstance(2010, 07, 15);
        RPR.Approval_Status2__c = 'Draft';
        RPR.Account__c = Acc.Id;
        RPR.Approver_Comments3__c = 'ok';
        insert RPR;
        RPR.Account__c = Acc2.Id;
        update RPR;
        
        Approval.ProcessSubmitRequest r = new Approval.ProcessSubmitRequest();
            r.setObjectId(RPR.Id);
            r.setNextApproverIds(new Id[] {UserInfo.getUserId()});
        	Approval.process(r);
        
        
    }
}
Please Mark It As Best Answer If It Helps
Thank You!
kiran25kiran25
Hi CharuDutt,

This code is still showing 0 % code coverage , can you help to suggest me why its showing zero .

User-added image









 
CharuDuttCharuDutt
Hii Bhavana
If You have Trigger Then make Changes As Shown In Below Code
Trigger

Trigger RPRTrigger on RPR__c(After Insert,Before Update){
If(Trigger.IsAfter && Trigger.IsInsert){
TriggerHandler_Controller THC = new TriggerHandler_Controller();
THC.afterInsert(Trigger.New);
}

If(Trigger.IsBefore && Trigger.IsUpdate){
TriggerHandler_Controller THC = new TriggerHandler_Controller();
THC.beforeUpdate(Trigger.New);

}
}

###############################
if Don't Have Trigger


@isTest
public class TriggerHandler_ControllerTest {
    @isTest
    public static void unitTestbeforeUpdate(){
        Account Acc = new Account();
        Acc.Name = 'Test Account';
        //Fill All Required Fields
        insert Acc;
        Account Acc2 = new Account();
        Acc2.Name = 'Test Account';
        //Fill All Required Fields
        insert Acc2;
        
        Opportunity opp = new Opportunity();
        opp.Name = 'Test Opportunity';
        opp.StageName = 'Closed Won';
        opp.CloseDate = System.today();
        opp.AccountId = Acc.Id;
        Insert opp;
        
        Opportunity opp1 = new Opportunity();
        opp1.Name = 'Test Opportunity';
        opp1.StageName = 'Closed Won';
        opp1.CloseDate = System.today();
        opp1.AccountId = Acc2.Id;
        Insert opp1;
        
        RPR__c RPR = new RPR__c();
        RPR.Requested_Date__c = Date.newInstance(2010, 07, 15);
        RPR.Approval_Status2__c = 'Draft';
        RPR.Account__c = Acc.Id;
        RPR.Approver_Comments3__c = 'ok';
        insert RPR;
        RPR.Account__c = Acc2.Id;
        update RPR;
        
        Approval.ProcessSubmitRequest r = new Approval.ProcessSubmitRequest();
            r.setObjectId(RPR.Id);
            r.setNextApproverIds(new Id[] {UserInfo.getUserId()});
        	Approval.process(r);
        TriggerHandler_Controller THC = new TriggerHandler_Controller(); 
        THC.afterInsert(new list<RPR__c>{RPR});
        THC.beforeUpdate(new list<RPR__c>{RPR});
    }
}

 
kiran25kiran25
I have run the test class on the exisiting trigger it shows 60 % of code coverage now. 

User-added image




User-added image



 
@isTest
public class TriggerHandler_ControllerTest {
    @isTest
    public static void unitTestbeforeUpdate(){
        Account Acc = new Account();
        Acc.Name = 'Test Account';
        Acc.Country__c = 'Malaysia';
        Acc.Short_Description__c = 'test';
        Acc.Ship_to_Destination__c = 'Malaysia';
        Acc.Customer_Type__c = 'New Customer';	
        Acc.Account_Group__c = 'Z103 HQ Existing Customer (Export)';	
        Acc.Primary_Contact_Name__c = 'test';
        Acc.Primary_Contact_Phone__c = '+918309809908';
        Acc.Primary_Contact_Email__c = 'test@gmail.com';
        Acc.Sales_district__c = 'Asia';
        Acc.Currency__c = 'MYR';
        Acc.Incoterm__c = 'CFR';
        Acc.Total_Credit_Facility__c = 1;
        Acc.Total_Credit_Limit__c = 1;
        Acc.Risk_Category__c = 'Low Risk';
        Acc.Term_of_Payment__c = 'Z100 COD (DOC DATE)';
        Acc.Payment_Method__c = 'Cash';
        Acc.BillingCountry = 'Malaysia';
        Acc.Medical__c =TRUE;
        insert Acc;
        Account Acc2 = new Account();
        Acc2.Name = 'Test Account2';
        Acc2.Country__c = 'Malaysia';
        Acc2.Short_Description__c = 'test';
        Acc2.Ship_to_Destination__c = 'Malaysia';
        Acc2.Customer_Type__c = 'New Customer';	
        Acc2.Account_Group__c = 'Z103 HQ Existing Customer (Export)';	
        Acc2.Primary_Contact_Name__c = 'test';
        Acc2.Primary_Contact_Phone__c = '+918309809908';
        Acc2.Primary_Contact_Email__c = 'test@gmail.com';
        Acc2.Sales_district__c = 'Asia';
        Acc2.Currency__c = 'MYR';
        Acc2.Incoterm__c = 'CFR';
        Acc2.Total_Credit_Facility__c = 1;
        Acc2.Total_Credit_Limit__c = 1;
        Acc2.Risk_Category__c = 'Low Risk';
        Acc2.Term_of_Payment__c = 'Z100 COD (DOC DATE)';
        Acc2.Payment_Method__c = 'Cash';
        Acc2.BillingCountry = 'Malaysia';
        Acc2.Medical__c =TRUE;
        insert Acc2;
        
        Opportunity opp = new Opportunity();
        opp.Name = 'Test Opportunity';
        opp.StageName = 'Qualification';
        opp.Glove_Type__c = 'Glove';
        opp.CloseDate = System.today();
        opp.AccountId = Acc.Id;
        Insert opp;
        
        Opportunity opp1 = new Opportunity();
        opp1.Name = 'Test Opportunity';
        opp1.StageName = 'Qualification';
        opp1.Glove_Type__c = 'Glove';
        opp1.CloseDate = System.today();
        opp1.AccountId = Acc2.Id;
        Insert opp1;
        
        RPR__c RPR = new RPR__c();
        RPR.Requested_Date__c = Date.newInstance(2010, 07, 15);
        RPR.Approval_Status2__c = 'Draft';
        RPR.Account__c = Acc.Id;
        RPR.Approver_Comments3__c = 'ok';
        insert RPR;
        RPR.Account__c = Acc2.Id;
        update RPR;
        
        
        Approval.ProcessSubmitRequest r = new Approval.ProcessSubmitRequest();
            r.setObjectId(RPR.Id);
            r.setNextApproverIds(new Id[] {UserInfo.getUserId()});
        	Approval.process(r);
        
        
    }
}


Not sure why it still showing 8 % CharuDutt








 
CharuDuttCharuDutt
Hii Bhavan 
This Happening Because In Apex Class on Line Number 8  You Are using Produsct2Id  = '01t28000000cRQsAAM' WhereAs The Product2Id We Are Inserting From Test Class is Different And Also On Line Number  9  Query Opportunity  Where Id = '0060k00000CuKIw' WhereAs The Opportunty We Are Inserting From Test Class  is Different Remove These lines
 
kiran25kiran25

Hi CharuDutt,

For Quoteline item creation its mandatory to have Pricebook thats the reason i added a default product so that i can create quoteline item upon creation.

If i create a RPR custom object Record , this functionality should create a quote and quoteline object record. 

The code coverage goes up to 66 % if i removed but i am afraid the code will not work like before to have a quote and quoteline item record .

User-added image



Need your guidence on this please 







 
CharuDuttCharuDutt
Hii Bhavana We Can Connect If you Want
 
kiran25kiran25
Hi Charudutt,

yes please , How can we connect i wasnt able send you a direct message , Any meeting will help a lot to join and show you the functionaity 








 
CharuDuttCharuDutt
You Email Me At acssaleforce@gmail.com
CharuDuttCharuDutt
Hii Bhavana

You Can  Email Me At [ acssaleforce@gmail.com ]
kiran25kiran25

CharuDutt,

I am using a shared account named Bhavana ,Kiran here Emailed you from my email account , Need your urgent help on this concern please 

kiran25kiran25
Hi CharuDutt ,
I have emailed you 
kiran25kiran25
Hi CharuDutt,

was awaiting for your response back to the email sent . please let me know when can i expect response to it