Another Salesforce blog!!!

Salesforce, Apex

Salesforce – Making the User Object standard field Unique.


PS: make sure to add

<apex:pageMessages id="pageMsg"/>

on the VFP

trigger ExtesionTrigger on User (before insert , before update)
{
    Set<String> setExtension = new Set<String>();
    for(User u : trigger.new)
        {
            
            Boolean isNewExt = setExtension.add(u.Extension);
            //This will ensure that in bulk insert you are not inserting two users with same extension
            if(!isNewExt)
                u.addError('Dulicate Extension');
        }
        
    List<User> ul = [Select id from User where Extension in: setExtension];
    
    if(ul.size() > 0)
        {
            trigger.new.get(0).addError('Dulicate Extension');
        }
    

}

Leave a Reply

Your email address will not be published. Required fields are marked *

*