1

One of my object is having Lookup Relationship with Account. I want a functionality where we can not allow to have 2 records with the same Name, same Related Account, and same Owner.

I have tried 2 approaches so far :

  1. Creating a custom text field, which is unique, and using a workflow to populate that field by concatenating Name, Account and Owner. "Name&Account__c&OwnerID.

  2. Created a trigger on before insert, before update events.

Eventually, both of these aren't working when I am cloning a new record for the very first time.

So when I do a clone, My code doesn't detect the duplicity.

But, when I do Clone + Clone on the cloned record. the duplicity gets detected this time around.

Apex Trigger :

trigger DetectDuplicateJobLeads on Partner_Opportunity__c (before insert, before update) {
DetectDuplicateJobLeadsHandler obj1 = new DetectDuplicateJobLeadsHandler();
if(Trigger.isInsert && Trigger.isBefore)
{
    obj1.onBeforeInsert(Trigger.new);
}
if(Trigger.isUpdate && Trigger.isBefore)
{
    obj1.onBeforeUpdate(Trigger.old,Trigger.new);
}

}

Apex Handler :

public with sharing class DetectDuplicateJobLeadsHandler {
public void onBeforeInsert(List<Partner_Opportunity__c> lstTriggerNew)
{
    Integer count = 0;
    Set<String> setPOName = new Set<String>();
    Set<String> setPOAccount = new Set<String>();
    Set<String> setPOOwner = new Set<String>();
    for (Partner_Opportunity__c newPO : lstTriggerNew)
    {
        setPOName.add(newPO.Name);
        if(newPO.Account__c != null){
            setPOAccount.add(newPO.Account__c);
        }
        setPOOwner.add(newPO.OwnerId);
    }
    System.debug('Debug Log for setPOName'+setPOName);
    System.debug('Debug Log for setPOAccount'+setPOAccount);
    System.debug('Debug Log for setPOOwner'+setPOOwner);
    if(setPOAccount.size() > 0){
        List<Partner_Opportunity__c> lstExistingPO = [SELECT 
                                                        Id,
                                                        Account__c,
                                                        OwnerId 
                                                  FROM 
                                                        Partner_Opportunity__c 
                                                  WHERE 
                                                        Name IN : setPOName 
                                                  AND
                                                        Account__c IN : setPOAccount
                                                  AND
                                                        OwnerId IN : setPOOwner
                                                  ];
        System.debug('Debug Log for lstExistingPO'+lstExistingPO.size());             
        if(lstExistingPO.size()> 0){
            count = 1;
        }
    }
    System.debug('Debug Log for count'+count);
    for(Partner_Opportunity__c objPO: lstTriggerNew){
        if(count == 1){
            System.debug('Duplicate Found');
            objPO.addError('The Record Cannot be saved because there is an existing Record with same Name, Account and Owner');
        }           
    }
}

public void onBeforeUpdate(List<Partner_Opportunity__c> lstTriggerOld,List<Partner_Opportunity__c> lstTriggerNew)
{
    Integer count = 0;
    Set<ID> newPOId = new Set<Id>();
    Set<String> newPOName = new Set<String>();
    Set<String> oldPOName = new Set<String>();
    Set<String> newPOAccount = new Set<String>();
    Set<String> oldPOAccount = new Set<String>();
    Set<String> newPOOwner = new Set<String>();
    Set<String> oldPOOwner = new Set<String>();
    Set<String> dumStrName = new Set<String>();
    Set<String> dumStrAccount = new Set<String>();
    Set<String> dumStrOwner = new Set<String>();
    for(Partner_Opportunity__c objPO : lstTriggerOld){
        oldPOName.add(objPO.Name);
        oldPOAccount.add(objPO.Account__c);
        oldPOOwner.add(objPO.OwnerId);
    }
    for(Partner_Opportunity__c objPO : lstTriggerNew){
        newPOName.add(objPO.Name);
        newPOId.add(objPO.Id);
        newPOAccount.add(objPO.Account__c);
        newPOOwner.add(objPO.OwnerId);
    }
    for(Partner_Opportunity__c objPO: [SELECT 
                                             Id,
                                             Name,
                                             Account__c,
                                             OwnerId 
                                       FROM 
                                             Partner_Opportunity__c 
                                       WHERE 
                                             Name  
                                       IN : 
                                             newPOName 
                                       AND
                                             Id 
                                       Not IN : 
                                             newPOId]){
        dumStrName.add(objPO.Name);
        dumStrAccount.add(objPO.Account__c);
        dumStrOwner.add(objPO.OwnerId);
    }

    System.debug('Debug Log for dumStrName'+dumStrName);
    System.debug('Debug Log for dumStrAccount'+dumStrAccount);
    System.debug('Debug Log for dumStrOwner'+dumStrOwner);

    for(Partner_Opportunity__c objPO : lstTriggerNew){
        if(dumStrName.contains(objPO.Name) && dumStrAccount.contains(objPO.Account__c) && dumStrOwner.contains(objPO.OwnerId)){
            System.debug('Duplicate found');
            objPO.addError('Duplicacy detected. Another record exists with the same Name, Account and Owner.');
        }else{
            System.debug(' Not Duplicate');
            dumStrName.add(objPO.Name);
            dumStrAccount.add(objPO.Account__c);
            dumStrOwner.add(objPO.OwnerId);
        }
    }
}

}

1 Answer 1

2

You may have issue with workflow rule criteria:

I have taken this approach and it's works great.

  • Creating a custom text field, which is unique, and using a workflow to populate that field by concatenating Name, Account and Owner. Name&Account__c&OwnerID.

Rule criteria: Evaluate the rule when a record is created, and every time it's edited

5
  • Its already what I've done, but it is going to create issue while updating a record. Commented Sep 16, 2017 at 18:24
  • 1
    what is your use case? Commented Sep 16, 2017 at 18:41
  • No two records of a custom object can have the same name, owner, and account(lookup). Commented Sep 18, 2017 at 7:35
  • If two records can have same name, owner and account then what is your duplicate rule? How you are defining uniqueness? Commented Sep 18, 2017 at 13:55
  • Please refer to the code above. Commented Sep 20, 2017 at 9:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .