I am trying to create a contact , custom object record and community user when a lead status is changed , so issue is i am able to create contact and custom object record(without the code to create community user) , but i am getting an error when i add the code to insert community user.Error:Portal user cannot be created with contact:


Trigger CreateContact on Lead (after insert, after Update) {
  if(Trigger.isInsert) {


public class Lead2TriggerHandler {
public static Boolean firstRunComplete = false;

public static void createContact(List<Lead> newLeads, Map<Id,Lead> oldLeadsMap) {
    if(Lead2TriggerHandler.firstRunComplete) return;

    List<Application__c> apps = new List<Application__c>();
    List<Contact> contacts = new List<Contact>();
    for(Lead acc : newLeads){
        if(acc.Status=='Interviewed' && (oldLeadsMap == null || oldLeadsMap.get(acc.Id).Status != 'Interviewed')){
        Contact con = new Contact(LastName = acc.lastname,
                                 FirstName= acc.FirstName,

        Application__c a = new Application__c(Name = acc.Lead_Program_Name__c,
                                             Program__c= acc.Program__c,
                                             Last_Name__c= acc.LastName,
                                             First_Name__c= acc.FirstName,
                                             Program_Session__c= acc.Program_Session__c);
      insert contacts;
      insert apps;

       Lead2TriggerHandler.firstRunComplete = true;
  • 1
    You have multiple problems in your code. First of all you insert a list of contacts but you are using con.Id which should not be accessible out of the scope of first for loop, unless you are creating a contact instance somewhere that is not mentioned in your example. If you can put a correct version of your code, I can help.
    – oozkan
    Feb 14, 2019 at 14:21
  • @oozkan please refer to updated code , my requirement is i want to create contact , custom object record and community user upon lead conversion:
    – Harry_G
    Feb 15, 2019 at 7:54

1 Answer 1


Error itself says that you cannot create portal user from contact. You need to create portal user from account.

If user belongs to community means user will surely associate with account.

Just visit below blogs link,it may helpfull for you (For understanding the creation)


  • I am not sure that's correct , may be i said it wrong in first place , the error said 'Cannot create portal user without contact'.
    – Harry_G
    Feb 14, 2019 at 13:03
  • Create a linkage between account and contact.. that would be the solution.. if you create contacts also in account level manager external permission should be enable otherwise you can't create portal user.. Feb 14, 2019 at 13:19

You must log in to answer this question.

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