0

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

6
  • 1
    Are you sure Do_Not_Contact__pc is the right field name? Have you set up Person Accounts in your org? Can you include a screenshot of your field configuration? – Adrian Larson Feb 22 '18 at 22:39
  • I'm certain that Do_Not_Contact__pc is the right field name. When I change Do_Not_Contact__pc to Do_Not_Contact__c on line 2 I get an error Variable does not exist: Do_Not_Contact__c – Briscoooe Feb 22 '18 at 22:43
  • Please edit your post to include a clear screenshot of your field configuration. – Adrian Larson Feb 22 '18 at 22:46
  • I have edited to include a screeenshot – Briscoooe Feb 22 '18 at 22:53
  • 1
    No the entire field configuration. You should be able to click into that field's detail view and see more information. – Adrian Larson Feb 22 '18 at 22:55
1

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=...);
2
  • Just out of curiosity, why was I able to view and change the value of the __pc field? The execution only failed when I tried to update – Briscoooe Feb 22 '18 at 23:27
  • @Briscoooe Apex Code doesn't validate if the person account fields are valid, because it doesn't know if the account is a person account. It's the DML runtime that has to determine if the fields submitted are valid or not. – sfdcfox Feb 23 '18 at 0:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.