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
kumar.fdc81.3902978579608325E12kumar.fdc81.3902978579608325E12 

Test class code coverage

Hi all,
I written one trigger and test class, but code coverage is 33%. can any one help me

Trigger:-
---------
trigger LeadPreventUpdatePhone on Lead (before update) {         
    string UserName = userinfo.getUserName();
    if(UserName.left(4) == 'sfdc'){
    for (Lead Lead : System.Trigger.new) {   // This line is not covered the code
        Lead oldLead = Trigger.oldMap.get(Lead.ID); // This line is not covered the code
        if(Lead.Phone != oldLead.Phone){ // This line is not covered the code
            Lead.Phone = oldLead.Phone; // This line is not covered the code
        }
    }
    }


Test class:-
--------------
@isTest
public class LeadPreventUpdatePhoneTestClass
{

    @isTest static void LeadPreventUpdatePhone()
    {
       Profile p = [SELECT Id FROM Profile WHERE Name='SumT Support'];
       User u = new User(Alias = 'Test', Email='Test@testorg.com',
       EmailEncodingKey='UTF-8', LastName='Test', LanguageLocaleKey='en_US',
       LocaleSidKey='en_US', ProfileId = p.Id,
       TimeZoneSidKey='America/Los_Angeles', UserName='xsfdc@testorg.com');

       System.runAs(u) {
         // The following code runs as user 'u'
       System.debug('Current User: ' + UserInfo.getUserName());
       System.debug('Current Profile: ' + UserInfo.getProfileId());
    
    
       Lead objLead=new Lead();
       objLead.OwnerId=u.ID;
       objLead.LastName='sampath';
       objLead.Company='Testing';
       objLead.Job_Function__c='VP1';
       objLead.Department__c='HR1!';
       objLead.Lead_Source_Category__c='Partner1';
       objLead.Status='Open';
       objLead.Email='sampath.kumar@octaware.com';
       objLead.phone='515222632';
       insert objLead;
       Lead la = [select phone from Lead where id = :objLead.id limit 1];
       System.debug('Original Lx:'+ la.phone);
       System.assert(la.Phone == '515222632');       
       la.phone = '123456789';
       update la;
      
        Lead lb = [select phone from Lead where id = :objLead.id limit 1];
        System.debug('Changed lb:'+ la.phone);
//COMMENT:  then we assert that the field we updated equals the field it was updated from
        System.assert(la.Phone == '123456789');
        }
      
    }
  }
Best Answer chosen by kumar.fdc81.3902978579608325E12
PrakashbPrakashb
Hi kumar,

UserName.left in your test case will return xsfd and not SFDC.

Kindly update the username to start with SFDC.

All Answers

PrakashbPrakashb
Hi kumar,

UserName.left in your test case will return xsfd and not SFDC.

Kindly update the username to start with SFDC.
This was selected as the best answer
kumar.fdc81.3902978579608325E12kumar.fdc81.3902978579608325E12
Thanks Prakash