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
NikithaNikitha 

Hi I m new to salesforce, I m try trigger whenever contact is created new user should be created. after done coding I got err says

TriggeronContact: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, only portal users can be associated to a contact: [] Trigger.TriggeronContact: line 32, column 1

my code is
trigger TriggeronContact on Contact (After insert) {
    Set<ID> contactids = new Set<ID>();
    for(Contact c : Trigger.new){
        if(c.LastName != null ){
            Contactids.add(c.Id);
        }
    }
    List<Contact> conlist =[Select id, email,FirstName,LastName  from Contact where Id IN : contactids];
    List<User> userList = new List<User>();
    for(Contact con : trigger.new){
        string nick = con.email!=null?con.email.substring(0, con.email.indexOf('@')):''; 
        nick += Datetime.now().getTime();
        List<Profile> profileList = [Select Id from Profile where Name=: 'System Administrator' limit 1];
        
        User newuser = new User(Alias = con.FirstName,
                                email = con.Email,
                                emailencodingkey = 'UTF-8',
                                firstname = con.FirstName,
                                lastname = con.LastName,
                                localesidkey = 'en_US',
                                contactId = con.Id,
                                timezonesidkey='America/Los_Angeles',
                                IsActive = true,
                                username = con.Email,
                                CommunityNickname = nick,
                                ProfileId =  profileList[0].Id,
                                LanguageLocaleKey = 'en_US');
                          userList.add(newuser);
}
    insert userList;

}


how i need to clear this err msg
SwethaSwetha (Salesforce Developers) 
As suggested in https://salesforce.stackexchange.com/questions/93990/receiving-error-while-creating-salesforce-portal-user-in-test-class, can you change your SOQL query to reflect Portal Profile associated with your Portal User instead of system admin and see if that fixes the issue for you?

 List<Profile> profileList = [Select Id from Profile where Name=: 'System Administrator' limit 1];

to 

 List<Profile> profileList = [Select Id from Profile where Name=: 'Portal Profile' limit 1];

If this information helps, please mark the answer as best.Thank you
Malika Pathak 9Malika Pathak 9

Hi Swathi,

Please find the solution.

I think you are creating user for each Contact then You no need to provide lookup for Contact " contactId = con.Id," and 
" LICENSE_LIMIT_EXCEEDED" this error is occurring because of you don't have permission to create more user so for System administrator there would be already some user so delete those users from System administrator.

And Try the below code after that error would not come.

if(trigger.isAfter && trigger.isInsert){
        Set<ID> contactids = new Set<ID>();
        for(Contact c : Trigger.new){
            if(c.LastName != null ){
                Contactids.add(c.Id);
            }
        }
         Profile profileId = [Select Id from Profile where Name=: 'System Administrator' limit 1];

        List<Contact> conlist =[Select id, email,FirstName,LastName  from Contact where Id IN : contactids];
        List<User> userList = new List<User>();
        for(Contact con : trigger.new){
            string nick = con.email!=null?con.email.substring(0, con.email.indexOf('@')):''; 
            nick += Datetime.now().getTime();
            
            User newuser = new User(Alias = con.FirstName,
                                    email = con.Email,
                                    emailencodingkey = 'UTF-8',
                                    firstname = con.FirstName,
                                    lastname = con.LastName,
                                    localesidkey = 'en_US',
                                    timezonesidkey='America/Los_Angeles',
                                    IsActive = true,
                                    username = con.Email,
                                    CommunityNickname = nick,
                                    ProfileId =  profileId.Id,
                                    LanguageLocaleKey = 'en_US');
            userList.add(newuser);
            
            
        }
        insert userList;
        system.debug('userList'+userList);
        
    }

Please let me know it is working or not and if you it is helpful for you then Please mark best Answer.

Thanks

 

NikithaNikitha
Hi Malika Even after changing my code my getting the same error message - TriggeronContact: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, only portal users can be associated to a contact: [] Trigger.TriggeronContact: line 31, column 1 only portal users can be associated to a contact: [] what this line means
NikithaNikitha
Hello Swetha, After changing 'System Administrator' as 'Portal Profile' I got this error - TriggeronContact: execution of AfterInsert caused by: System.QueryException: List has no rows for assignment to SObject Trigger.TriggeronContact: line 8, column 1
Malika Pathak 9Malika Pathak 9
Have you inserted the user with contact lookUp. I mean no need to to assign contactid in user i.e delete this line from your code " contactId = con.Id,"