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
kumar7kumar7 

Test class failed

Hi All,

For this peace of apex class code i have written

 newPortalUser.UserName = newPortalUser.Email + '.portal';
                        string acctnum=[select accountnumber from account where id =:currentUser.Contact.Accountid].accountnumber;
                        if(acctnum == null){
                        newPortalUser.CommunityNickname = currentUser.Contact.AccountID +'_' + newPortalUser.FirstName + newPortalUser.LastName;
                        }else {
                        newPortalUser.CommunityNickname = acctnum +'_' + newPortalUser.FirstName + newPortalUser.LastName;
                        }  

For this i have written test class
========================
 Account acc = new Account (AccountNumber = '123', Name = 'Testing');
             insert acc;

But i am getting error while execution of test class
Error:-
System.QueryException: List has no rows for assignment to SObject

Thanks in adv
Wilfredo Morillo 20Wilfredo Morillo 20
The SOQL query is not returning any records and that's why you are getting this QueryException. Try this:
 
newPortalUser.UserName = newPortalUser.Email + '.portal';
list<Account> accs =[select accountnumber from account where id =:currentUser.Contact.Accountid limit 1];
if( Accs.Size()== 0){
    newPortalUser.CommunityNickname = currentUser.Contact.AccountID +'_' + newPortalUser.FirstName + newPortalUser.LastName;
  }else {
         newPortalUser.CommunityNickname = accs[0].accountnumber +'_' + newPortalUser.FirstName + newPortalUser.LastName;
                        }


 
kumar7kumar7
Hi,

Thanks for your update. But i am getting different error
Error MessageSystem.QueryException: List has no rows for assignment to SObject

Logic i have written like this:

User portalUser;
 portalUser.UserName = portalUser.Email + '.portal';
            list<Account> accs =[select accountnumber from account where id =:portalUser.Contact.Accountid limit 1];
            if( Accs.Size()== 0){
            portalUser.CommunityNickname = portalUser.Contact.AccountID +'_' + portalUser.FirstName + portalUser.LastName;
            }else {
            portalUser.CommunityNickname = accs[0].accountnumber +'_' + portalUser.FirstName + portalUser.LastName;
                    }          

Thanks
Wilfredo Morillo 20Wilfredo Morillo 20
Initialize the List before use it with : 
List<Account> accs = new list<Account>();

accs = [select .........];