INVALID_TYPE_ON_FIELD_IN_RECORD Exception in Salesforce

Sometime you can get stuck with some very simple code.

insert new Object__c(
    Name = 'Test Object',
    Field__c = variableParsedFromClientSide;
);

And you got this

INVALID_TYPE_ON_FIELD_IN_RECORD, Field__c: value not of required type: : [Field__c]

Hm, as an experienced developer you see this message and say: “Hey, that’s simple, just some sort of field’s inconsistency.. haha..” – then you look at field mappings, checking all field/variable assignments and finally all seems to be fine. But error message get raised again. Hm, let’s check again and you will see that actually nothing really strange or mysterious here you just need to check variable which you’ve parsed from client side. I collected possible reasons and listed it below, all of them are related to parsing data

  1. assignment null to checkbox field or text ‘null’ to any field type
  2. assignment text value ‘True’ or ‘False’ to checkbox/boolean
  3. assignment number in text format to numeric variable
  4. assignment of incorrect Id field, for example you’re trying to use GUID in native salesforce ID field
[apex][apex errors][apex exception]