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
Mayank07Mayank07 

Failing multiple test classes because of one trigger

I am allocating a customer community user license whenever a lead is converted into contact and it's working fine. below is my code:
trigger CreateCommunityUser on Contact (After insert) {
    
if(Trigger.isInsert){
         for(Contact co : trigger.new){
Contact con = [select id,email,firstName,lastname,accountId from Contact where Id =:co.Id];         
Database.DMLOptions dmo = new Database.DMLOptions();
dmo.EmailHeader.triggerUserEmail = false;       
dmo.EmailHeader.triggerOtherEmail = false;
dmo.EmailHeader.triggerAutoResponseEmail = false;       
dmo.optAllOrNone = false; */

// create portal user
string nick = con.email!=null?con.email.substring(0, con.email.indexOf('@')):'';
nick += Datetime.now().getTime();
User newUser1 = new User(alias=con.firstName, email = con.email, emailencodingkey = 'UTF-8', firstname = con.firstName, lastname = con.lastname, languagelocalekey = 'en_US',localesidkey = 'en_US',contactId = con.Id,timezonesidkey = 'Asia/Dubai',username = con.email,CommunityNickname = nick,ProfileId ='00e6F000001kZ4C', IsActive = true);
newUser1.setOptions(dmo); 
insert newUser1;
         }
     }
}

Because of this code, most of my other test classes are failing with these errors:
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, CreateCommunityUser: execution of AfterInsert
caused by: System.DmlException: Insert failed. First exception on row 0; first error: STRING_TOO_LONG, Alias: data value too large: FirstName (max length=8): [Alias]
Trigger.CreateCommunityUser: line 16, column 1: []

Please guide me where am I going wrong.
Thanks already.
AnudeepAnudeep (Salesforce Developers) 
This error means that the value submitted to Salesforce exceeds the maximum character length set for the Salesforce field.

I recommend checking the length of the FirstName field 

Here is an example of how to check this
 
Schema.DescribeFieldResult F = Account.AccountNumber.getDescribe();
Integer lengthOfField = F.getLength();

There is a help article around this. Although it talks about flows. The resolution is to either correct the limits or remove the fields completely.  

Let me know if this helps. If it does, please mark this answer as Best. It may help others in the community. Thank You!
Maharajan CMaharajan C
Hi Mayank,

Instead of the assigning the entire first name in alias you can reduce it , because if the contact first name is greater than 8 characters then actual contact record insert also will fail . Here you have to change the trigger not the test class.

Try like only first 4 letters of first name or  2 letters from first name and 2 letter from last name. like below.

String aliasstr = con.firstName.length() > 4 ?  con.firstName.substring(0, 3) : con.firstName ;
User newUser1 = new User(alias=aliasstr , email = con.email)


Thanks,
Maharajan.C