You can achieve this with Apex triggr. You can create static users with the help of underneath code, play with this code and think how to create dynamic users!!
*************** Apex class *************
public class createUserWhenLeadConvertInoContact { public static void createUser(){ Profile objProfile = [SELECT Id FROM Profile WHERE Name = 'Standard Platform User' LIMIT 1]; //Add User Information User objUser = new User(); objUser.LastName = 'Swathi'; objUser.Alias = 'Swathi'; objUser.Email = 'Swathi@gmail.com'; objUser.Username = 'SwathiSwathiSwathi@gmail.com'; objUser.ProfileId = objProfile.id; objUser.TimeZoneSidKey = 'GMT'; objUser.LanguageLocaleKey = 'en_US'; objUser.EmailEncodingKey = 'UTF-8'; objUser.LocaleSidKey = 'en_US'; //Insert User Insert objUser; system.debug('@@@@@@@@@objUser' +objUser); } }
************ APEX Trigger *************
trigger createUserWhenLeadConvertInoContact on Lead (after insert, after update) {
Hii I tried the code but I m getting err like this 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: []
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];
Greetings!
You need to write the after update trigger on Lead to create the user and please find the sample code to create the user as below: You need to either query the user or hard code the profile to create the user.
Reference:https://developer.salesforce.com/forums/?id=906F000000092p2IAA
Please mark it as best answer if it helps you to fix the issue.
Thank you!
Regards,
Shirisha Pathuri
You can achieve this with Apex triggr. You can create static users with the help of underneath code, play with this code and think how to create dynamic users!!
*************** Apex class *************
public class createUserWhenLeadConvertInoContact {
public static void createUser(){
Profile objProfile = [SELECT Id FROM Profile WHERE Name = 'Standard Platform User' LIMIT 1];
//Add User Information
User objUser = new User();
objUser.LastName = 'Swathi';
objUser.Alias = 'Swathi';
objUser.Email = 'Swathi@gmail.com';
objUser.Username = 'SwathiSwathiSwathi@gmail.com';
objUser.ProfileId = objProfile.id;
objUser.TimeZoneSidKey = 'GMT';
objUser.LanguageLocaleKey = 'en_US';
objUser.EmailEncodingKey = 'UTF-8';
objUser.LocaleSidKey = 'en_US';
//Insert User
Insert objUser;
system.debug('@@@@@@@@@objUser' +objUser);
}
}
************ APEX Trigger *************
trigger createUserWhenLeadConvertInoContact on Lead (after insert, after update) {
List<Lead> ldObj = new List<Lead>();
if((trigger.isInsert || trigger.isUpdate) && trigger.isAfter){
for(Lead leadObj: trigger.new){
if(leadObj.status == 'Closed - Converted'){
ldObj.add(leadObj);
system.debug('@@@@@@@@@@oldLead' + ldObj);
}
}
}
if(ldObj.size()>0){
createUserWhenLeadConvertInoContact.createUser();
}
}
Regards,
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: []
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;
}
what I want to do to clear this error message!
Please refer https://developer.salesforce.com/forums/?id=9060G0000005kOoQAI
Regards,