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
Ajay LAjay L 

Update is failing in the test coverage

Class:
    Map<String,Integer> ScoreMap = new Map<String,Integer>{'A4'=>7,'B4'=>7,'C4'=>5,'D4'=>4,'E4'=>4,'A3'=>8,'B3'=>7,'C3'=>6,'D3'=>5,'E3'=>5,};

if(sOperation== 'sUpdate')
                {
                    Contact oldC = OldConTriggerMap.get(c.Id);
                   
                    if((oldC.Score_OL__c!= c.Score_OL__c || 
                        oldC.Score_MC__c!= c.Score_MC__c ||
                        oldC.Score_PF__c!= c.Score_PF__c&& ScoreMap.ContainsKey(c.Score_MC__c))
                        )
                    {
                        c.Score_OL__c = ScoreMap.get(c.Score_MC__c);
                    }
                }
Test Class:
//Create a new account
        Account acc=new Account(Name='Account37',Phone='1142234334',Type = 'Prospect',
        Score_PF__C = 'B',
        );
        insert acc;
     //Create new contact
        Contact con = new Contact();
        con.LastName='ContactCS6737';
        con.AccountId=acc.Id;        
        con.Score_MC__c = 'B1';            
        insert con;
        
        con.Score_MC__c='B3';
        update con;
        Contact updatedCon=[SELECT Id,LastName,Score_MC__c from Contact WHERE Id=:con.Id];
        System.assertEquals('B3', con.Score_MC__c);

None of the lines are covered.

Regards,

Ajay

Vivek DVivek D
Are you getting an error or the code is not covered ?
If code is not covered then you might wanna check your IF condition. If error then there might be different problem
Ajay LAjay L
Thanks for reply Vivek.

No errors, Code is not getting covered.

All the below lines are not covered:

Contact oldC = OldConTriggerMap.get(c.Id);
                   
                    if((oldC.Score_OL__c!= c.Score_OL__c || 
                        oldC.Score_MC__c!= c.Score_MC__c ||
                        oldC.Score_PF__c!= c.Score_PF__c&& ScoreMap.ContainsKey(c.Score_MC__c))
                        )
                    {
                        c.Score_OL__c = ScoreMap.get(c.Score_MC__c);
                    }

Regards,
Ajay
Vivek DVivek D
Then here
if( sOperation == 'sUpdate') 
is the problem 
Ajay LAjay L
What should I do here to avoid the issue?

Regards,
Ajay
Vivek DVivek D
This if( sOperation == 'sUpdate')  condition is always false so the code in not getting covered. Make it true in your test class so that it will cover the rest of it
Ajay LAjay L
Interesting. However that variable in actual class as I am passing as parameter.

public static void OverallLeadScoreUpdate (Map<Id,Contact> OldConTriggerMap , List<Contact> NewConTriggerList, string sOperation)

How do I set that as true?

Do I need to call actual method into my test class?

Reagrds,
Ajay
Ajay LAjay L
@Vivek D,

Finally I could figured out the issue. I have the recursive flag in my trigger. That is stopping to fire the update for some reason. If I remove that recursive variable, then the test class is covering 100%. Can you please look into this and let me know why this recusrive causing the issue?

This is how I have the trigger:
trigger ContactTrigger on Contact (before insert, before update, after delete, after insert , after update, after undelete) {

 
  
    if(Trigger.isAfter){
        
        if(Trigger.isInsert || Trigger.isUnDelete){            
            
            SomeClass.SomeMethod(Trigger.new,Trigger.oldMap,'sinsert');
           
        }
        if(Trigger.isUpdate ){
            SomeClass.SomeMethod(Trigger.new,Trigger.oldMap,'sUpdate');                     
        }    
        if(Trigger.isDelete){
            SomeClass.SomeMethod(Trigger.new,Trigger.oldMap,'sDelete');     
        }
        
        if(Trigger.isInsert){
            SomeClass.SomeMethod(Trigger.new,Trigger.oldMap,'sinsert');
        }

        MyUtil.setAlreadyModified(); 
        
         
      }

    if(Trigger.isBefore){
    if (!MyUtil.isAlreadyModified()) 
    {
        if(Trigger.isInsert){
            SomeClass.SomeMethod(Trigger.new,Trigger.oldMap,'sinsert');          
        }

        if(Trigger.isUpdate){            
            SomeClass.SomeMethod(Trigger.new,Trigger.oldMap,'sUpdate');                      
        }
        
       }
     }
   
 }

My Utili Class: for Recusrive variable

public class MyUtil{


    private static boolean alreadyModified = false;
  
    
    public static boolean isAlreadyModified() {
        return alreadyModified;
    }
    
   
    public static void setAlreadyModified() {
        alreadyModified = true;
    }
}

Regards,
Ajay