Skip to main content
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;

    }

}
1 件の回答
  1. 2021年3月22日 6:51
    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.
0/9000