2

Please help me as I am new to salesforce. i want to create a user record when a contact is inserted. Not all contact but contact which are being converted from lead. i have created a custom checkbox field on contact 'ConvertedFromContact__c' which is mapped to lead custom field. this is to check if contact being converted from lead. but in trigger how to create a user. (need to assign profile and licence)
below is the code written by me

trigger createUserFromLead on Contact (after insert) {

    List<User> userList = new List<User>();

    for(Contact c: Trigger.New){
        if(c.ConvertedFromContact__c == checked){
            User u = new user();

        }
    }
}
2
  • You have nowhere inserted user record in your code. Your code is just checking if the checkbox field is true and then you have initialized a variable (u) to create new user. You will need need to get the Roles and Profiles which you need to assign to user. Then assign all the mandatory field values which we provide while creating a user record. Then insert those records.
    – Ruchi
    Sep 15, 2016 at 7:28
  • Which Community licences are you using as it will affect the fields that you need to set on your user records?
    – Dave Humm
    Sep 15, 2016 at 7:50

2 Answers 2

3

You can try below code.

Step 1- A trigger on Contact Object, which will get executed after inserting a Contact Record.

Step 2 - Fetch only those contact where ConvertedFromContact__c is true and pass those contactIds to future method of Handler class.

Step 3 - We are creating a method which is defined as future, as we are working on Setup(User) and Non Setup(Contact) object. Future method will be invoke asynchronously to create a User records for related Contacts.

// Trigger Code

trigger createUserFromLead on Contact (after insert) {

    List<User> userList = new List<User>();
    Set<Id> contactIds = new Set<Id>();
    for(Contact c: Trigger.New){
       if(c.ConvertedFromContact__c){
            contactIds.add(c.id);
        }
    }

    if(contactIds.size() > 0){
        CreateUserHandler.createUserFromContact(contactIds);
    }
}

// Handler Code

public with sharing class CreateUserHandler {

@future
public static void createUserFromContact(Set<Id> contactIds)
{
    List<User> userList = new List<User>();
    List<Profile> profileList = [Select Id from Profile where Name=: 'System Administrator' limit 1];
    List<UserRole> roleList = [Select Id from UserRole where Name=: 'CEO' limit 1];
    for(Contact contactObj : [Select Id, Name, Email,Username__c from Contact where Id IN: contactIds limit 50000]){
        User uObj = new User();
        uObj.Username = contactObj.Username__c;
        uObj.Email = contactObj.Email;
        uObj.Alias = contactObj.Name;
        uObj.UserRoleId = roleList[0].Id;
        uObj.ProfileId = profileList[0].Id;
        uObj.IsActive = true; 
        uObj.TimeZoneSidKey = 'GMT';
        uObj.LanguageLocaleKey = 'en_US';
        uObj.EmailEncodingKey = 'UTF-8';
        uObj.LocaleSidKey = 'en_US';
        uObj.ContactId = contactObj.Id;
        userList.add(uObj);
    }
    try{
           insert userList;  // insert the user record
    }catch(Exception e){
          // Catch Exception
     }
}

}

References for Future Method in Salesforce

2
  • thanks alot Devendra for this solution. but i want the user's username and email to be same as it is mentioned in the contact object. as i had made the email field in contact mandatory and want to use the same email id. Sep 15, 2016 at 8:50
  • With the help of contacIds, you need to query Contact records again and need to use in for loop. Please check the updated the code.
    – Devendra
    Sep 15, 2016 at 9:19
0

Try this,

ProfileID = [Select Id From Profile where (condition) Limit 1].Id;
User u = new user();
u.contactId = contact.Id;
u.LastName = 'Test lastname';
u.Email = '[email protected]';
u.Alias = 'Talias';
u.Username = '[email protected]';
u.CommunityNickname = 'test12';
u.LocaleSidKey = 'en_US';
u.TimeZoneSidKey = 'GMT';
u.ProfileID = ProfileID;
u.LanguageLocaleKey = 'en_US';
u.EmailEncodingKey = 'UTF-8';
insert u;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .