You need to sign in to do that
Don't have an account?

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
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
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.
Let me know if that helps you.
Best regards,
BALAJI
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