function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Joe BrodarJoe Brodar 

Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, User: id value of incorrect type: 005U0000003GvJKIA0: [User__c]

I am attempting to match up users coming in from an external POST request to Salesforce users via a SOQL query of the name that returns a user record, then use the ID from the user record to insert a custom object record with a User Lookup field. However I am getting the following error: 
Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, User: id value of incorrect type: 005U0000003GvJKIA0: [User__c]
This error is being thrown because the ID needs to be 15 characters, but is, in practice, coming out as 18 characters. I have tried a few different methods of limiting the string to 15 chars, but to no avail. This is frustrating because it works just fine in the sandbox environment, it is only when deployed to production that this error comes up.

Methods I have tried:

1. No string correction
// match names list to users and create records for each user
        finalCount = names.size();
        for (Integer j = 0; j < finalCount; j++) {
        
            User matchUsr = [SELECT Id, Name
                                FROM User
                                WHERE Name = :names[j]
                                LIMIT 1];
            String usrId = matchUsr.Id;
            
            indivs.add(new Late_Tasks_by_User__c(User__c=usrId,Dependent_Tasks__c=deps[j],Actual_Late_Tasks__c=lates[j],Date__c=datej));
            usrId = '';
            echoOut += names[j] + ', ';
        }

2. Substring the usrId string to (0,15) 
// match names list to users and create records for each user
        finalCount = names.size();
        for (Integer j = 0; j < finalCount; j++) {
        
            User matchUsr = [SELECT Id, Name
                                FROM User
                                WHERE Name = :names[j]
                                LIMIT 1];
            String userMatch = matchUsr.Id;
            String usrId = userMatch.subString(0, 15);
            
            indivs.add(new Late_Tasks_by_User__c(User__c=usrId,Dependent_Tasks__c=deps[j],Actual_Late_Tasks__c=lates[j],Date__c=datej));
            usrId = '';
            echoOut += names[j] + ', ';
        }

3. Take one character from the ID string of the user record at a time (0-14), and add to new usrId string
// match names list to users and create records for each user
        finalCount = names.size();
        for (Integer j = 0; j < finalCount; j++) {
        
            User matchUsr = [SELECT Id, Name
                                FROM User
                                WHERE Name = :names[j]
                                LIMIT 1];
            String userMatch = matchUsr.Id;
            String usrId = '';
            for (Integer xc = 0; xc < 15; xc++) {        
                usrId += userMatch.subString(xc, xc+1);
            }
            
            indivs.add(new Late_Tasks_by_User__c(User__c=usrId,Dependent_Tasks__c=deps[j],Actual_Late_Tasks__c=lates[j],Date__c=datej));
            usrId = '';
            echoOut += names[j] + ', ';
        }

None of these have produced a working result. All three continue to return an 18 character string as the usrId variable. I have found several threads on this forum regarding this topic, but the only solutions that are listed with those questions are either syntax error with the specific code, or someone saying that you have to subString the ID. If anyone has any solutions or ideas beyond that, please let me know.

Thanks,
Joe
Best Answer chosen by Joe Brodar
MJ Kahn / OpFocusMJ Kahn / OpFocus
In Salesforce, Ids can be either 15 characters (case sensitive) or 18 characters (case insensitive). See here (https://help.salesforce.com/apex/HTViewSolution?urlname=How-do-unique-IDs-work-in-Salesforce-1327108651310& (https://help.salesforce.com/apex/HTViewSolution?urlname=How-do-unique-IDs-work-in-Salesforce-1327108651310&" target="_blank)) for details. But that's not your issue.

In your case (no pun intended), the error is more likely because, at line 15, your User__c field is a lookup to some object other than a User. If you try to put a User Id into a field that looks up to some other type of object, you'll get a "id value of incorrect type" error.

"Why does it work in the sandbox, but not in Production?" you ask. Probably because the User__c field does look up to a User in the sandbox, but it's defined differently in Production.

All Answers

MJ Kahn / OpFocusMJ Kahn / OpFocus
In Salesforce, Ids can be either 15 characters (case sensitive) or 18 characters (case insensitive). See here (https://help.salesforce.com/apex/HTViewSolution?urlname=How-do-unique-IDs-work-in-Salesforce-1327108651310& (https://help.salesforce.com/apex/HTViewSolution?urlname=How-do-unique-IDs-work-in-Salesforce-1327108651310&" target="_blank)) for details. But that's not your issue.

In your case (no pun intended), the error is more likely because, at line 15, your User__c field is a lookup to some object other than a User. If you try to put a User Id into a field that looks up to some other type of object, you'll get a "id value of incorrect type" error.

"Why does it work in the sandbox, but not in Production?" you ask. Probably because the User__c field does look up to a User in the sandbox, but it's defined differently in Production.
This was selected as the best answer
Joe BrodarJoe Brodar
Hmm, the User__c field is a lookup field related to users, which shouldn't be defined differently in Production v. Sandbox environment. I will look into that to be safe, though.

Thank you for your reply!
Joe BrodarJoe Brodar
I'll eat my humble pie now. That was absolutely the reason for the issue. Thank you MJ!