function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ 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
Jose
 
BALAJI CHBALAJI CH
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
http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_dml_non_mix_sobjects.htm?SearchType

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');
Database.insert(portalAccount1);

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

//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'
);
Database.insert(user1);

Let me know if that helps you.

Best regards,
BALAJI
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);
        Database.insert(uAccount);
 

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

         Database.insert(uContact);


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
Jose