0

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:

Trigger CreateContact on Lead (after insert, after Update) {
  if(Trigger.isInsert) {
      Lead2TriggerHandler.createContact(Trigger.new);
   }
}

Class:

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,
                                 RecordTypeId='012f4000000Jda1AAC');
        contacts.add(con);

        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);
         apps.add(a);
      }
      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 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 at 7:54
0

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)

http://burnignorance.com/salesforce-tips/create-a-customer-portal-user-in-test-class-2/

  • 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 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.. – Annappa PH Feb 14 at 13:19

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.