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
Shiv ShankarShiv Shankar 

Error - INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []

Hello friends , i am writing a test class. in this test class i need to create a user - for this i wrote following code

User u = new User();
        Profile pf = [Select id, Name from Profile where Name =:'APAC' limit 1];
           system.debug('-------'+pf);
            u.LastName = 'Nicomatic2';
            u.Email = 'test@gmail.com.sandbox';
            u.CompanyName = 'test.com';
            u.Title = 'Test User';
            u.Username = 'test@gmail.com.sandbox';
            u.Alias = 'ni';
            u.CommunityNickname = 'nicomatic21';
            u.TimeZoneSidKey = 'America/Mexico_City';
            u.LocaleSidKey = 'en_US';
            u.EmailEncodingKey = 'ISO-8859-1';
            u.ProfileId = pf.Id;
            u.LanguageLocaleKey = 'en_US';       
            insert u;

 During excecution of this i get's error : Error - INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: [] in insert U; line.

In system.debug line i am geeting profile name and id but still it's not working. But when i put 'standar user' Or 'System Admin' than it works. Why ? 

AlagarAlagar

change the below line..

 

u.profileId = pf[0].Id;

 

 

Mark the answer as solution if this solves ur issue..

 

 

 

Bindhyachal Kumar SinghBindhyachal Kumar Singh

Hi Shivshankar,

 

have you profile in your organization which have name  APAC

 

Use following code for get Profile :   Remove colon (:)

 

Profile pf = [Select id, Name from Profile where Name ='APAC' limit 1];
Shiv ShankarShiv Shankar
Yes