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
Ashish Kumar YadavAshish Kumar Yadav 

how to prevent duplicate record insert in apex class?

how can we avoid duplicate insert and give the message to user record already exist.find below method.


    public PageReference AssignIndividual(){
        try{
            if(sam.Scheme_Master__c == NULL){
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'Please select the Scheme from "Scheme Name"'));
                return null; 
            }
            else if(sam.Account__c== NULL && sam.Retailer_Account__c== NULL){
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'Please Check Distributor/Retailer Name'));
            }
            else {
              
                System.debug('---> assign individual');
                Map<Id,Account> mapAccount = New Map<Id,Account>([select id, name from account]);
                Set<Id> accIds = new Set<Id>();
                lstAssignIndividual = new List<Scheme_Assign_Master__c>();
                scheme_masterobj=new Scheme_Master__c();
                System.debug('Retailer_Account__c--->'+sam.Retailer_Account__c);
                System.debug('Scheme_Master__c--->'+sam.Scheme_Master__c);
                System.debug('Account__c--->'+sam.Account__c);
                if(sam.Scheme_Master__c != null){
                    if(sam.Retailer_Account__c != null){
                        try{
                            Scheme_Assign_Master__c objSAM = new Scheme_Assign_Master__c();
                            objSAM.Retailer_Account__c = sam.Retailer_Account__c;
                            objSAM.Scheme_Master__c = sam.Scheme_Master__c;
                            lstAssignIndividual.add(objSAM);
                            accIds.add(sam.Retailer_Account__c);
                        }catch(Exception e){
                            System.debug('exception'+e.getLineNumber());
                            System.debug('exception message'+e.getMessage());
                        }
                    }
                    if(sam.Account__c != null){
                        Scheme_Assign_Master__c objSAM = new Scheme_Assign_Master__c();
                        objSAM.Account__c = sam.Account__c;
                        objSAM.Scheme_Master__c = sam.Scheme_Master__c;
                        lstAssignIndividual.add(objSAM);
                        accIds.add(sam.Account__c);
                    }
                }
                
              
                if(lstAssignIndividual.size() > 0){
                    insert lstAssignIndividual;
                    
                }
               
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,' Assign Individual Scheme to Distributor / Retailer Successfully.'));
                return null; 
            }
        }
        catch(Exception e){
            System.debug('Exception'+e.getLineNumber());
            System.debug('exception'+e.getMessage());
        }
        return null;
    }
    
    
ShirishaShirisha (Salesforce Developers) 
Hi Ashish,

Greetings!

I would suggest you to check the duplicates based on the field and if exists then use the record.Otherwise,create new one.

Please refer the sample code provided in the below thread:

https://salesforce.stackexchange.com/questions/234144/prevent-duplicate-record-to-get-created

Please mark it as best answer if it helps you to fix the issue.

Thank you!

Regards,
Shirisha Pathuri