+ Start a Discussion
Jose ZunigaJose Zuniga 

Modifying and setting User.ContactId with a Contact.Id

While trying to insert a User accound using Apex I got this error:

16:55:44:236 EXCEPTION_THROWN [38]|System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, Cannot create a portal user without contact: [ContactId]

How can I relate a contact object to the respective User object? I now some contact fields can be modified using the account object. 

Thanks in advance
Hi Jose,

Salesforce categorizes objects into so called setup and non-setup objects. User is a setup object while Account is a non-setup object. Salesforce restricts DML operations so that both kinds of objects can't be manipulated in the same context.
You can refer this link

While inserting User through Apex, you should have Contact ID with a Account. Please find below sample code.
//Create account
Account portalAccount1 = new Account(Name = 'TestAccount');

//Create contact
Contact contact1 = new Contact(
    FirstName = 'Test',
    Lastname = 'McTesty',
    AccountId = portalAccount1.Id,
    Email = System.now().millisecond() + 'test@test.com'

//Create user
Profile portalProfile = [SELECT Id FROM Profile Limit 1];
User user1 = new User(
    Username = 'test12345@test.com',
    ContactId = contact1.Id,
    ProfileId = portalProfile.Id,
    Alias = 'test123',
    Email = 'test12345@test.com',
    EmailEncodingKey = 'UTF-8',
    LastName = 'McTesty',
    CommunityNickname = 'test12345',
    TimeZoneSidKey = 'America/Los_Angeles',
    LocaleSidKey = 'en_US',
    LanguageLocaleKey = 'en_US'

Let me know if that helps you.

Best regards,
Jose ZunigaJose Zuniga
Balaji, first of all, thanks for your answer. I followed your suggestions and here it is the code I am using:

        string SAMLuserLastName     = attributes.get('User.lastname');
        string SAMLuserFirstName    = attributes.get('User.firstname');
        string SAMLuserEmail         = attributes.get('User.email');
        string SAMLupdatedTime        = attributes.get('User.updatedTime');
        string SAMLuserID            = attributes.get('User.uid');
        Account uAccount = new Account(FirstName = SAMLuserFirstName,    
                                       LastName  = SAMLuserLastName,
                                       CTSC_Sponsored_Biostats_Hours__c = 5);

         // ---------------------- for debugging in createUser
         List <Account> Lacc = [select Id, Name, FirstName, CTSC_Sponsored_Biostats_Hours__c from Account];
         System.debug('Accounts: ');
         // -----------------------   
         Contact uContact= new Contact(
             FirstName = SAMLuserFirstName,
            LastName  = SAMLuserLastName,
            AccountId = uAccount.Id,
            Email = SAMLuserEmail


However, I get the next error messages just after the last Database.insert(uContact) command is executed:

11:02:43:463 VF_PAGE_MESSAGE Can not select a person account
11:02:43:463 EXCEPTION_THROWN [97]|System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, Can not select a person account: [AccountId]

I heard that in the newer APIs, the Contact is completely read-only if it is attached to an account (as in, a Person Account) (ref: https://developer.salesforce.com/forums/?id=906F00000008xrHIAQ) but if this is the case, how can I assign the proper value to the User's ContactId?

Thanks in advance