Activity Forums Salesforce® Discussions Is there any way through which user is restricted to save duplicate records in salesforce?

  • Mohit

    Member
    September 9, 2016 at 5:37 am

    Hi Pranav,

    You can prevent the duplicate record based on the value of a single custom field, select the Unique and Required checkbox on that field's definition :-

    Steps for selecting unique and required checkbox is :-

    In standard object:-

    1) Click Setup | Customize.
    2) Select the link for the desired object, and click Fields.
    3) Click Edit next to the name of the appropriate field.

    In Custom Object:-
    1) Click Setup | Develop | Objects.
    2) Click the name of the object on which the field appears.
    3) Click Edit next to the name of the field in the Custom Fields and Relationships related list.

    The Unique and Required checkboxes are only available on custom fields.

    If you need to require uniqueness based on the value of two or more fields, or a single standard field, write an Apex before insert and before update trigger.

    trigger sObjectDuplicatePreventer on sObject
    (before insert, before update){

    Map<String, sObject> sObjectMap = new Map<String, sObject>();
    for (sObject obj : System.Trigger.new){
    if ((obj.Email !=null) &&
    (System.Trigger.isInsert ||
    (obj.Email !=
    System.Trigger.oldMap.get(obj.Id).Email))){
    if (sObjectMap.containsKey(obj.Email)){
    obj.Email.addError('Another new lead has the '
    +'same email address.');
    }else{
    leadMap.put(obj.Email, obj);
    }
    }
    }

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos