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
BatmansfdcBatmansfdc 

How to write a test class for this? can some one please help

User u = [SELECT Id, Contact.AccountId FROM User WHERE Id = :UserInfo.getUserId()][0];
            contact.AccountId = u.Contact.AccountId;
            insert contact;
Raj VakatiRaj Vakati
Account acct = new Account(
            Name =  'AccountCaseUpdate',
            Type = 'Customer'
        );
        insert acct;
        
            Contact con = new Contact(
                LastName = 'Test'+i,
                FirstName ='User',
                AccountID = acct.Id,
                Phone = '121212',   
                Email = 'testuser@test.com'  ,
             ) ;
        insert con ;
        
        Profile p = [SELECT Id FROM Profile WHERE Name='Customer Community User']; 
        
            User u = new User(Alias = 'standt', 
                              EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
                              LocaleSidKey='en_US', ProfileId = p.Id, CommunityNickname='i3ksdbfbsbdfsdbf'+i , Phone='39459374587354',
                              TimeZoneSidKey='America/Los_Angeles', UserName='testuser@test.com');
            u.Email = 'testuser@test.com' ;
            //u.IsPortalEnabled =TRUE ;
            u.ContactId = con.Id;
			
            insert u ;

 
Srikanth sfdc 32Srikanth sfdc 32
Hi

try this code .


        user u1 = [select id,name from user  where id = :UserInfo.getUserId() LIMIT 1];

        system.runas(u1)
       {  
            contact.AccountId = u1.Contact.AccountId;
            insert contact;

       }
BatmansfdcBatmansfdc
Hi Srikanth,

I am getting below error message
System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: User.Contact