[SalesForce] Can’t update Boolean value of Account object

I cannot update update this Boolean value in Apex. What is going wrong? The if statement, and the fact that the front end representation is a checkbox, proves that it is indeed a boolean value. I am new to Apex so I feel its a basic misunderstanding of how it works. Can anyone help me out?

Here is the code that I'm executing in an Anonymous Window.

Account acc = new Account(Name='Test Name');
if (acc.Do_Not_Contact__pc == false) {
    System.debug('DNC is false');
} else {
    System.debug('DNC is true');
}
insert acc;
acc.Do_Not_Contact__pc = true;
update acc;

It fails on the second to last line, displaying the following message:

System.DmlException: Update failed. First exception on row 0 with id 001W000000fFiVbIAK; first error: INVALID_FIELD_FOR_INSERT_UPDATE, Account: bad field names on insert/update call: Do_Not_Contact__pc: [Do_Not_Contact__pc]

What's particularly frustrating is that when I change the second to last line to

acc.Do_Not_Contact__pc = 'true';

I get an error stating that I cannot assign a String to a Boolean value

Edit 1:
Here is an image showing the Account field configuration

Field Configuration

Edit 2:
Full field configuration
Full field configuration

Best Answer

You need to set the RecordTypeId to a Person Account record type, or you won't be able to use "PC" fields. Also, you cannot use the Name field for a Person Account, because it's used only on Business Accounts. Use FirstName, LastName, etc to set the name.

Id personAccountRTId = [SELECT Id FROM RecordType WHERE IsPersonType=true LIMIT 1].Id;

Account record = new Account(LastName=..., RecordTypeId=personAccountRTId, Do_Not_Contact__pc=...);
Related Topic