2

I need to automatically create a customer portal (CP) user whenever a CP admin (ie, CP user with delegated external user admin rights) creates a contact in the CP.

The CP admin logs into the portal via a Force.com Site, so I've assumed the Site class instance will figure out this Site-CP association. But when I use the Site.createPortalUser() in my trigger, it throws this

System.TypeException: You are already logged in.

I use the Site class as I need to use the returned userId for other things, therefore I am unable to use @future method.

Any ideas? Here's short version of the trigger in question:

trigger CreateCPUser on Contact (before insert) {
    if (UserInfo.getUserType() == 'CustomerSuccess' || UserInfo.getUserType() == 'PowerCustomerSuccess') {
        for (Contact contact : Trigger.new) {
            // Instantiate a new user.
            User newuser = PortalUserUtility.createCPUser(contact);

            // Throws the error.
            String newuserId = Site.createPortalUser(newuser, contact.AccountId, null, true);

            // Need to use the returned User Id.            
            contact.Customer_Portal_User__c = newuser.Id; 
        }   
    }       
}

EDIT I understand the ability to add a user can be made available in the customer portal via the "Manage External User" button but unfortunately, the button is unwanted.

1 Answer 1

2

You can only call Site.createPortalUser from a site context. If you're in a trigger, you no longer have the site context.

If you're interested in creating customer portal users from contacts, check out the blog Provisioning Customer Portal Users with Apex

5
  • This solution works great for creating users and without the use of Site class. However, the use of @future here prevents me from retrieving the ID of the user.
    – Jan Julian
    Commented Aug 9, 2013 at 20:56
  • what do you need the new user id for? Can your future method do what you need to do with the user id after it's created? The reason it doesn't return is that the user isn't created right away.
    – Ryan Guest
    Commented Aug 9, 2013 at 21:01
  • I need to populate the lookup field on the contact with the portal user. See last statement in the sample trigger above.
    – Jan Julian
    Commented Aug 9, 2013 at 21:07
  • @jmrjulian You can requery the userId in future method and populate and update the contact . Commented Aug 9, 2013 at 22:40
  • The future method will fail as you can't mix DML operations on user and contact.
    – Jan Julian
    Commented Aug 12, 2013 at 20:13

You must log in to answer this question.

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