I converted a lead into contact which is taking from web-to-lead.

But i am struggling at converting the contact to portal user using the code.

please can any one provide the apex code for converting the contact to portal user. if not, I think so, I have to create a user using apex code and then put the contact ID in it, which is not best way.

please give me suggestions?

Thanks and regards KS Kumaar


There is no apex function to 'convert' a contact to partner contact, automatically creating the associated user.

Also you'll notice that if you click on 'Enable partner user' in the Contact screen on the Salesforce GUI, it will still take you to the new User screen (with some fields defaulted). So even SFDC follows a 2-step approach, which you will need to simulate in APEX.

So you need to stick to the solution of creating a user (with the right portal profile) and putting the ContactId in there.

E.g. assuming you have the Id of the contact as contactId:

//Create user
Profile portalProfile = [select Id from Profile where UserType = 'PowerPartner' limit 1];
User theUser = new User(
    Username = 'test123@test.com',
    ContactId = contactId,
    ProfileId = portalProfile.Id,
    Alias = 'test123',
    Email = 'test123@test.com',
    EmailEncodingKey = 'UTF-8',
    LastName = 'McTesty',
    CommunityNickname = 'test123',
    TimeZoneSidKey = 'America/Los_Angeles',
    LocaleSidKey = 'en_US',
    LanguageLocaleKey = 'en_US'
  • Thank You Guy......but i have one doubt. please explain it. I found a class called Site and a method with respect Site class is createPortalUser(user, accountId, password). Could you please explain about it?
    – KS Kumaar
    Jun 17 '16 at 9:16
  • That's right, but unfortunately: "This method is only valid when a site is associated with a Customer Portal." It doesn't involve the 'Contact' object but is specifically created for Customer Portals. Jun 17 '16 at 9:30
  • so finally, we do not have any code for it. All we need to do is we can get an user by clicking on Enable portal user only?????????
    – KS Kumaar
    Jun 17 '16 at 9:37
  • yes there is code for it. I updated my answer to show. Jun 17 '16 at 9:41
  • I knew that we can add user by performing DML operation. Anyway thank you for your help. Your help is appreciated.
    – KS Kumaar
    Jun 17 '16 at 9:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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