1

My JIT code creates one User object and another Person Account object (my environment uses person accounts). I need to establish a relationship between them and I believe this has to be done using a command like:

u.ContactId = [select PersonContactId  FROM account where ID =: accountId].Id

However, I am getting the next error at the insert command:

10:34:29:496 FATAL_ERROR System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Contact ID: id value of incorrect type: 0015500000Es8udAAB: [ContactId]

Can someone please tell me what am I missing? Thanks Jose

1 Answer 1

1

You are querying the PersonContactId but assigning the Id of the Account. Try

u.ContactId = [select PersonContactId FROM account where ID =: accountId].PersonContactId

Hope it helps.

4
  • No, you shouldn't query to a list arbitrarily. If accountId is not null, there will be exactly one row. The system guarantees it.
    – sfdcfox
    Commented May 10, 2016 at 17:37
  • @sfdcfox What if the assignment to accountId variable is something like customrecord.Account__c and the lookup is empty for some records. That's why i included that as an edit rather than posting it as answer in first place. Commented May 10, 2016 at 17:41
  • @VigneshwaranG That's when you use a map so you can aggregate your queries. It's rarely ever acceptable to use this pattern, because either you meant to use a map, or you want an error condition to arise because something is wrong. For example, say you have a master business account that you tie certain types of contacts to. If the account is deleted, or someone creates a duplicate, you want to be notified about it so you can take action. Using that pattern helps to mask those kinds of problems.
    – sfdcfox
    Commented May 10, 2016 at 17:50
  • Got it @sfdcfox. Let me remove the edit from my answer. Commented May 10, 2016 at 18:01

You must log in to answer this question.

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