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
Ramana123Ramana123 

when I am inserting a Lead with the same email filed it has to throw an exception and count muse be incremented in already existed email Record

In Debug logs the count is incrementing but in the record the count is not incrementing 

public class DuplicateLeadExceptionHelper {
    public static void leadException(List<Lead> leadList){
        integer count=0;
        List<Lead> updateRecords = new List<Lead>();
        Map<String,Lead> existingRecords = new Map<String,Lead>();
        for (Lead rec : [select LastName,Email,DuplicateRecordsExceptionCount__c 
                         from Lead]) {
                             existingRecords.put(rec.LastName,rec);
                         }
        System.debug('//'+existingRecords.keyset());
        for(Lead leadRec : leadList){
            if(Trigger.isbefore && Trigger.isInsert){
                if(existingRecords.containskey(leadRec.LastName)){
                    leadRec.addError('Error! Duplicate Email Field, already this LastName exists in Name');
                    Lead countrec= existingRecords.get(leadRec.LastName);
                    if(countrec.DuplicateRecordsExceptionCount__c==null)
                        countrec.DuplicateRecordsExceptionCount__c=1;
                    else
                        countrec.DuplicateRecordsExceptionCount__c=countrec.DuplicateRecordsExceptionCount__c+1;
                    updateRecords.add(countrec); 
                }
            }
        }
        system.debug('///'+updateRecords);
        update updateRecords;
    }
}
ANUTEJANUTEJ (Salesforce Developers) 
Hi Ramana,

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

I think this is another question regarding the same issue mentioned over in the above link as stated you won't be able to show an error and update the field on new record that is being inserted.
 
from the manual
SObject Class | Apex Developer Guide | Salesforce Developers (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_sobject.htm)

addError(errorMsg)
Marks a trigger record with a custom error message and prevents any DML operation from occurring.

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.