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
Ashwin sfdc 3Ashwin sfdc 3 

getting "invalid cross reference id" error on running the below code in test class .

getting "invalid cross reference id" error on running the below code in test class .

@isTest 
public class TestForceForecasting {
    static testMethod void insertNewUser() {
       
       User userToCreate = new User();
       
       userToCreate.FirstName = 'Davidsh';
       userToCreate.LastName  = 'Liush';
       userToCreate.Email     = 'dashwinsfdc99@gmail.com';
       userToCreate.Username  = 'sfdc-shdreamer@gmail.com';
       userToCreate.Alias     = 'fatty';
      userToCreate.TimeZoneSidKey  = 'GMT';
       userToCreate.LocaleSidKey  = 'en_US';
       userToCreate.EmailEncodingKey  = 'UTF-8';
       userToCreate.LanguageLocaleKey = 'en_US';
        userToCreate.ProfileId='00e28000005Mf8h';
       insert userToCreate;
       
       }
       }
Arkadiy GolovkoArkadiy Golovko
So, I just wanted everyone to know that I struggled with the "invalid cross reference ID" error too when trying to update users.  The error was on each line of the list so it was impossible to determine if it was one record or all of the records.  When I re-exported the list (from the test sandbox this time instead of Production) I was able to do the upload.  There were 5 records in the Production environment that were not in the sandbox.  It would have been difficult to figure out which records on the list of over 800 that were causing the error.
Amit Chaudhary 8Amit Chaudhary 8
Always try to avoid the hardcoding . Try to update your code like below
@isTest 
public class TestForceForecasting 
{
    static testMethod void insertNewUser() 
	{
       
	   Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];

       User userToCreate = new User();
       
       userToCreate.FirstName = 'Davidsh';
       userToCreate.LastName  = 'Liush';
       userToCreate.Email     = 'dashwinsfdc99@gmail.com';
       userToCreate.Username  = 'sfdc-shdreamer@gmail.com';
       userToCreate.Alias     = 'fatty';
       userToCreate.TimeZoneSidKey  = 'GMT';
       userToCreate.LocaleSidKey  = 'en_US';
       userToCreate.EmailEncodingKey  = 'UTF-8';
       userToCreate.LanguageLocaleKey = 'en_US';
       userToCreate.ProfileId = p.id;
       insert userToCreate;
       
    }
}