You need to sign in to do that
Don't have an account?
Nikitha
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
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
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
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.
Please let me know it is working or not and if you it is helpful for you then Please mark best Answer.
Thanks