+ Start a Discussion
cnovocnovo 

How to ignore contacts created by Partner

We have a trigger to look for duplicate contacts that our developer created before he left the company. It has been working fine, but I was hoping to update it to not take into account contacts that are created by a partner user. So if one of our users is trying to insert a contact that has the same email address as a contact for an Account that is owned by one of our partners...it won't give our user a duplicate error. I was hoping someone might be able to help me out with the code that is needed to accomplish this. This is what our trigger looks like today. Any help would be greatly appreciated.

trigger ContactBefore on Contact (before insert, before update) {
    /* Prevent duplicate contacts 
    *  duplicate qualifier fields: last name, email
    *  ignor duplicate check if the ignoreDuplicatePrevention field equels true
    */
    
    ID Userid;
    Map<Id, User> owners;
    Set<Id> ownerIdSet = new Set<Id>();
    Map<String, Contact> contactMap = new Map<String, Contact>();
    Set<String> emailSet = new Set<String>();
    String compositeKey = null;
    
    Userid=userinfo.getuserid();
    User u=new User(ID=Userid);
    system.debug('*** UserID= '+Userid);
    system.debug('*** isInsert= ' + trigger.isInsert);
    system.debug('*** isUpdate= ' + trigger.isUpdate);
    
    for(Contact c : trigger.new) {
        ownerIdSet.add(c.OwnerId);  
    }
    owners = new Map<Id, User>([SELECT UserType FROM User WHERE Id IN :ownerIdSet]);
        
    for(Contact c : system.trigger.new) {
        if(!c.Ignore_Duplicate_Prevention__c && ((trigger.isInsert && Userinfo.getUserType() == 'Standard') || (trigger.isUpdate && owners.get(c.OwnerId).UserType == 'Standard')) && !Utilities.isNullOrEmpty(c.Email)) {
            compositeKey = c.Email;
            // Make sure we don't treat an email address that
            // isn't changing during an update as a duplicate.
            if(trigger.isInsert || c.Email != trigger.oldMap.get(c.Id).Email) {
            
                // Make sure another new contact isn't also a duplicate
                if(contactMap.containsKey(compositeKey)) {
                if(u.Isportalenabled==false){
                    c.Email.addError('Another new contact has the same email address.');
                    contactMap.get(compositeKey).Email.addError('Another new contact has the same email address.');
                    }
                } else {
                    contactMap.put(compositeKey, c);
                    emailSet.add(c.Email);
                }
            }
        }
    }
    
    if(!emailSet.isEmpty()) {
        for(Contact c : [SELECT Email FROM Contact WHERE Email IN :emailSet]) {
            compositeKey = c.Email;
            if(contactMap.containsKey(compositeKey)) {
                contactMap.get(compositeKey).Email.addError('A contact with this email address already exists.');
            }
        }
    }
    
    
    
}
RamuRamu (Salesforce Developers) 
You can use userinfo class methods to find the user's profile and by using if else statement you can ignore the contacts created by particular partner user profile.