+ Start a Discussion
Sfdc SivaSfdc Siva 

NeedHelp for DuplicateUsername

Hi All,
Can any one please help us to avoid the below error  "System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_USERNAME, Duplicate Username.The username already exists in this or another Salesforce organization.Usernames must be unique across all Salesforce organizations."

Below is my trigger:

trigger PartnerUserTrigger on Partner_Access_Request__c(after update)
{
    List<User> lst_User = new List<User>();
    Set<Id> set_Id = new Set<Id>();
    Set<Id> setrej_Id = new Set<Id>();
    map<id,string> cremail = new map<id,string>();
    Map<Id,User> mp_User = new Map<Id,User>();
    list<Contact> con_List = new List<Contact>();
    List<String> sendTo = new List<String>();
    List<String> sendToCC = new List<String>();
    List<String> sendToCCorg = new List<String>();
    Map<String,Contact> contactMap =new Map<String,Contact>();
    for(User usr : [Select Id,ContactId,isActive from User where ContactId!=null and isActive=true])
    {
        mp_User.put(usr.ContactId,usr);
    }
    User user_Recd = [Select Id,LastName,Username,Profile.Name,Email,Alias,CommunityNickname,TimeZoneSidKey,LocaleSidKey,ContactId,EmailEncodingKey,ProfileId,LanguageLocaleKey from user where Profile.Name='Partner Sales Leader' And ContactId!=null limit 1];
    for(Partner_Access_Request__c Partner_Recd : Trigger.new)
    {
        if(Partner_Recd.Status__c=='Approved' && Partner_Recd.Status__c!=Trigger.oldMap.get(Partner_Recd.id).Status__c)
        {
            set_Id.add(Partner_Recd.id);
        }
        if(Partner_Recd.Status__c=='Rejected' && Partner_Recd.Status__c!=Trigger.oldMap.get(Partner_Recd.id).Status__c)
        {
            setrej_Id.add(Partner_Recd.id);
        }
    }    
    if(set_Id!=null && !set_Id.isEmpty())
    {
        for(Partner_Access_Request__c Partner_User_Recd : [Select Id,Partner_Contact__c,Status__c,CreatedBy.Email,Partner_Contact__r.Partner_User__c,Partner_Contact__r.Email,Partner_Contact__r.Name,Partner_Contact__r.Title,Partner_Contact__r.Department,Partner_Contact__r.FirstName,Partner_Contact__r.LastName,Partner_Contact__r.MailingCity,Partner_Contact__r.MailingState,Partner_Contact__r.MailingCountry,Partner_Contact__r.MailingPostalCode,Partner_Contact__r.MailingStreet from Partner_Access_Request__c where Id IN:set_Id])
        {             
            if(mp_User!=null && !mp_User.containsKey(Partner_User_Recd.Partner_Contact__c))
            {
                User PartnerUser_Recd = new user();
                PartnerUser_Recd.Username = Partner_User_Recd.Partner_Contact__r.Email;
                PartnerUser_Recd.FirstName = Partner_User_Recd.Partner_Contact__r.FirstName;
                PartnerUser_Recd.LastName= Partner_User_Recd.Partner_Contact__r.LastName;
                PartnerUser_Recd.Email = Partner_User_Recd.Partner_Contact__r.Email;    
                        
                PartnerUser_Recd.Alias = Partner_User_Recd.Partner_Contact__r.Name.subString(0,3);
                PartnerUser_Recd.ContactId = Partner_User_Recd.Partner_Contact__c;
                PartnerUser_Recd.Title = Partner_User_Recd.Partner_Contact__r.Title;
                PartnerUser_Recd.Department = Partner_User_Recd.Partner_Contact__r.Department;
                PartnerUser_Recd.TimeZoneSidKey= user_Recd.TimeZoneSidKey; 
                PartnerUser_Recd.LocaleSidKey= user_Recd.LocaleSidKey;
                PartnerUser_Recd.EmailEncodingKey =user_Recd.EmailEncodingKey;
                PartnerUser_Recd.ProfileId=user_Recd.ProfileId;
                PartnerUser_Recd.LanguageLocaleKey =user_Recd.LanguageLocaleKey;             
                PartnerUser_Recd.City = Partner_User_Recd.Partner_Contact__r.MailingCity;
                PartnerUser_Recd.State = Partner_User_Recd.Partner_Contact__r.MailingState;
                PartnerUser_Recd.Country= Partner_User_Recd.Partner_Contact__r.MailingCountry;
                PartnerUser_Recd.PostalCode = Partner_User_Recd.Partner_Contact__r.MailingPostalCode;
                PartnerUser_Recd.Street = Partner_User_Recd.Partner_Contact__r.MailingStreet;
                PartnerUser_Recd.isActive = true;
                if(Partner_User_Recd.Partner_Contact__r.Firstname!=null)
                    PartnerUser_Recd.CommunityNickname = Partner_User_Recd.Partner_Contact__r.Firstname.subString(0,2);
                if(Partner_User_Recd.Partner_Contact__r.lastname!=null)
                    PartnerUser_Recd.CommunityNickname =PartnerUser_Recd.CommunityNickname +Partner_User_Recd.Partner_Contact__r.lastname.subString(0,3);
                cremail.put(Partner_User_Recd.Partner_Contact__c,Partner_User_Recd.CreatedBy.Email); 
                //PartnerUser_Recd.CommunityNickname = '';
                lst_User.add(PartnerUser_Recd);
                Partner_User_Recd.Partner_Contact__r.Partner_User__c = true;
                con_List.add(Partner_User_Recd.Partner_Contact__r);
            }
             //}
        }
    }
    List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
    if(lst_User!=null && !lst_User.isEmpty())
    {
        system.debug('@@@@'+ lst_User);
    insert lst_User;
    }
    if(con_List!=null && con_List.size()>0)
        update con_List;
    for(PartnerUserEmail__c pue : PartnerUserEmail__c.getall().values())
    {
        sendToCCorg.add(pue.name);
    }
    for(user u: lst_User)
    {
      
          sendTo.Add(cremail.get(u.contactid));
  
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setToAddresses(sendTo);
     
        mail.setSubject('New Partner Access provided');
        String body = 'Hi' ;
        if(u.FirstName!=null)
            body=body+u.FirstName;
        if(u.LastName!=null)
            body=body+u.LastName;
        body=body+'<br/><br/>';      
        body=body+'Your request to grant new partner user access to the traditional partner portal has been provided.<br/><br/>';
              
        body=body+'Thanks,<br/>';
        body=body+'Support Team';
        mail.setHtmlBody(body);
        mails.add(mail);
    }
 
    if(setrej_Id.size()>0)
    {  
        for(Partner_Access_Request__c Partner_User_Recd : [Select Id,Partner_Contact__c,Status__c,CreatedBy.email,Partner_Contact__r.Partner_User__c,Partner_Contact__r.Email,Partner_Contact__r.Name,Partner_Contact__r.Title,Partner_Contact__r.Department,Partner_Contact__r.FirstName,Partner_Contact__r.LastName,Partner_Contact__r.MailingCity,Partner_Contact__r.MailingState,Partner_Contact__r.MailingCountry,Partner_Contact__r.MailingPostalCode,Partner_Contact__r.MailingStreet from Partner_Access_Request__c where Id IN:setrej_Id])    
        {
            sendTo.add(Partner_User_Recd.Partner_Contact__r.Email);
            sendToCC.add(Partner_User_Recd.CreatedBy.Email); 
            sendToCC.addall(sendToCCorg);    
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setToAddresses(sendTo);
            mail.setCcAddresses(sendToCC);
            mail.setSubject('New Partner Access Denied');
            String body = 'Hi ';
            if(Partner_User_Recd.Partner_Contact__r.FirstName!=null)
                body=body+Partner_User_Recd.Partner_Contact__r.FirstName;
            if(Partner_User_Recd.Partner_Contact__r.LastName!=null)
                body=body+Partner_User_Recd.Partner_Contact__r.LastName;
            body=body+'<br/><br/><br/>';
            body=body+'Your request for Partner portal access has been denied. <br/><br/><br/><br/>';
            body=body+'Thanks,<br/>';
            body=body+'Support Team';
            mail.setHtmlBody(body);
            mails.add(mail);
        }
    }
    if(mails.size()>0)
    {
        Messaging.sendEmail(mails);
    }
}

Thanks
Hemant_JainHemant_Jain
Try appending a string while setting the username, like:

PartnerUser_Recd.Username = Partner_User_Recd.Partner_Contact__r.Email+ '.newUser';

Someway, the useraname needs to be unique.
Rajesh3699Rajesh3699
Hi,

Use the <apex:pageMessages> in the visualforce page, and in the class add a logic to check whether the contact has associated with the new email Id ( taken as user name). If found true display the apex message.


 ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'A contact with this email address already exists.');
  ApexPages.addMessage(myMsg);

Thank You,
Adiga.