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
annoyingmouseannoyingmouse 

Trigger (the Wonder Horse)

Hi all,

 

goodness, I'm really hitting these boards at the minute!

 

I've a custom Address object and I need to prevent duplications. I've created a formula field which concatenates everything so I need a trigger to check that the result of the formula field is unique before I insert or update.

 

So far I've got this:

 

 

trigger uniqueAddress on Address__c (before insert, before update) {
    List<Address__c> c = [select OneLineAddress__c from Address__c where OneLineAddress__c = ];
    If(c.size() > 0){
        "Address already exists within the Application!";
    }
}

 

I'm stuck for what to put after the "OneLineAddress__c ="

 

Any ideas out there? I'm sure it's something really simple but I've got no idea about how to access the object calling the trigger...

 

Cheers,

 

Dom

Best Answer chosen by Admin (Salesforce Developers) 
annoyingmouseannoyingmouse

Cracked it, thanks to you both!

 

trigger addressDuplicatePreventer on Address__c(before insert){
    for (Address__c addr : System.Trigger.new) {
        List<Address__c> c = [select Id from Address__c where OneLineAddress__c = :addr.OneLineAddress__c];
        If(c.size() > 0){
            addr.addError('This address already exists within the application, please search for it.');
        }
    }
}

All Answers

bob_buzzardbob_buzzard

The objects being inserted/updated will be in the trigger.new list.

James LoghryJames Loghry

try using addError() on the record.  It should prevent the duplicate (in Trigger.new list) from being inserted.

 

If you're using something such as data loader, you will see the given error message pop up corresponding to the record.

 

Also, be sure to test this thoroughly in your unit tests.

 

 

trigger uniqueAddress on Address__c(before insert){

 

  Map<String,Address__c> oneAddresses = new List<String>();

  for(Address__c addr : Trigger.new){

    /*Create a string based on your formula logic here,

      because the formula will not be available in this trigger..  After you have the string, add it to oneAddresses list*/

    //oneAddresses.put(addr.OneLineAddress__c,addr);

  }

 

  Address__c current = null;

  for(Address__c addr : [Select OneLineAddress__c From Address__c Where OneLineAddress__c in :oneAddresses.keySet()]){

    if(addr.OneLineAddress__c != null){

      current = oneAddresses.get(addr.OneLineAddress__c);

      if(current != null){

        current.addError("Your error message or Label goes here...");

      }

    }

  }

}

annoyingmouseannoyingmouse

Thank you both, in terms of testing I'd love to, if I ever got it to compile in the first place ;-)

 

I've tried this so far (amongst other ways of doing it! Honestly I come from PHP and Javascript and I'm ewildered with this Java C# mash-up (no offence intended)):

 

 

trigger addressDuplicatePreventer on Address__c(before insert, before update){
    Map<String, Address__c> addressMap = new Map<String, Address__c>();
    for (Address__c addr : System.Trigger.new) {
        if ((addr.OneLineAddress__c != null) && (System.Trigger.isInsert || (addr.OneLineAddress__c != System.Trigger.oldMap.get(addr.Id).OneLineAddress__c))) {
            if (addressMap.containsKey(addr.OneLineAddress__c)) {
                addr.OneLineAddress__c.addError('This address is a duplicate');
            } else {
                addressMap.put(addr.OneLineAddress__c, addr);
            }
        }
    }
    Address__c current = null;
    for(Address__c addr : [Select OneLineAddress__c From Address__c Where OneLineAddress__c in :addressMap.keySet()]){
        if(addr.OneLineAddress__c != null){
            current = oneAddresses.get(addr.OneLineAddress__c);
            if(current != null){
                current.addError("This address is a duplicate");
            }
        }
    }
}

 

Any ideas of what I'm doing wrong would be appreciated.

 

I've looked at http://www.salesforce.com/docs/developer/cookbook/Content/apex_dedupe.htm amngst other sources and I'm starting to feel rather silly :-(

 

Cheers,

 

Dom

bob_buzzardbob_buzzard

As this is a before insert trigger, the OneLineAddress__c field won't be populated (assuming this is the formula field) as that is done by the database when you retrieve a record.  You'll need to replicate your formula in the code I'm afraid.

annoyingmouseannoyingmouse

Cracked it, thanks to you both!

 

trigger addressDuplicatePreventer on Address__c(before insert){
    for (Address__c addr : System.Trigger.new) {
        List<Address__c> c = [select Id from Address__c where OneLineAddress__c = :addr.OneLineAddress__c];
        If(c.size() > 0){
            addr.addError('This address already exists within the application, please search for it.');
        }
    }
}
This was selected as the best answer