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
SV MSV M 

Prevent Duplicates

Hi, I have a wrapper class where it displays contacts related to accounts along with checkboxes. I am adding new records into it with a custom button. I want to add an error message if there are duplicates in record names. Can someone help me to solve this.

//Wrapper Class Code
public class AccountContactWrapper {
    List<Contact> selectedRecords {get;set;}
    public List<WrapperClass> wrapperList {get;set;}
    public List<Account> accList {get;set;}
    public AccountContactWrapper (ApexPages.StandardController stdController) {
        accList = [SELECT Id, Name FROM Account];
        wrapperList = new List<WrapperClass>();
        for(Contact c : [SELECT FirstName, LastName, Phone FROM Contact WHERE AccountId =: ApexPages.currentPage().getParameters().get('id')]) {
            wrapperList.add(new WrapperClass(c));
        }
    }
    //Deleting SelectedRecords
    public void deleteRecords() {
        List<Contact> selectedRecords = new List<Contact>();
        for (WrapperClass wrap : wrapperList) {
            if(wrap.selected==true) {
                selectedRecords.add(wrap.con);
            }
        }
        delete selectedRecords;
    }
    //Creating New Contact Record
    public void createNewContact() {
        wrapperList = createNewContact(wrapperList);
    }
    public List<WrapperClass> createNewContact (List<WrapperClass> insertList) {
        WrapperClass newRecord = new WrapperClass();//To display new Row
        Contact newContactRecord = new Contact();//Creates new Record
        newContactRecord.AccountId = ApexPages.currentPage().getParameters().get('id');
        newRecord.con = newContactRecord;
        insertList.add(newRecord);
        return insertList;
    }
    //Saving New Records
    public void saveRecords() {
        List<Contact> selectedRecords = new List<Contact>();
        for(WrapperClass wp : wrapperList) {
            if(wp.selected) {
                selectedRecords.add(wp.con);
            }
        }
        insert selectedRecords;
    }
    public class WrapperClass {
        public Contact con {get;set;}
        public Boolean selected {get;set;}
        public WrapperClass (Contact c) {
            con=c;
            selected=false;
        }
        public WrapperClass(){}
    }
}

//VF Page

<apex:page standardController="Account" extensions="AccountContactWrapper">
    <apex:form >
        <apex:pageBlock id="block">
            <apex:pageBlockButtons location="bottom">
                <!--<apex:commandButton value="Edit" action="{!processedRecords}"/>-->
                <apex:commandButton value="Delete Selected Records" action="{!deleteRecords}"/>
                <apex:commandButton value="Create Contact" action="{!createNewContact}"/>
                <apex:commandButton value="Save" action="{!saveRecords}"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection >
                <apex:pageBlockTable value="{!wrapperList}" var="wrap" id="table">
                    <apex:column >
                        <apex:inputCheckbox value="{!wrap.selected}"/>
                    </apex:column>
                    <apex:column headerValue="First Name">
                        <apex:inputField value="{!wrap.con.FirstName}"/>
                    </apex:column>
                    <apex:column headerValue="Last Name">
                        <apex:inputField value="{!wrap.con.LastName}"/>
                    </apex:column>
                    <apex:column headerValue="Phone">
                        <apex:inputField value="{!wrap.con.Phone}"/>
                    </apex:column>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
ShirishaShirisha (Salesforce Developers) 
Hi Sai,

Here is the sample code which will help you in writing the code for duplicate records:

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_Database_DuplicateError.htm

Kindly 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.

Warm Regards,
Shirisha Pathuri