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
Dhananjay Patil 12Dhananjay Patil 12 

Account Created in Test Class does not return in query result?

I have created an account in my test class but when it is querying for Account acc,it gives me Null every time. I have created a VF and create a controller class for this.
Below is My Apex Class:
 
public class createAccountTeamMember {
 public List <AccountWrapper> wrappers {get;set;}
 private Integer nextIdent = 0;
 public static boolean isValid = true;
 public static boolean isValid1 {get;set;}
 public static boolean isError {get;set;}
 public static string accId = ApexPages.currentPage().getParameters().get('AccId');
 public Account acct;
 static {
  getAccount();
 }
 public createAccountTeamMember(ApexPages.StandardController Controller) {
     AccountTeamMember a1 = (AccountTeamMember)Controller.getRecord();
     system.debug(' AccountTeamMember a1'+a1);

  wrappers = new List < AccountWrapper > ();
  for (Integer idx = 0; idx < 5; idx++) {
   wrappers.add(new AccountWrapper(nextIdent++));
  }
 }
 public static Account getAccount() {

  try {
      system.debug('accId:'+accId);
      system.debug('Current User:'+UserInfo.getUserId());

     Account acc = [SELECT Id, Name,Global_Key_Account__c,Global_KAM__c FROM Account WHERE Id = : accId AND((Global_Key_Account__c = true AND Global_KAM__c =:UserInfo.getUserId()) OR(Regional_Key_Account__c = true AND Regional_KAM__c =:UserInfo.getUserId()) OR (Country_Key_Account__c = true AND Country_KAM__c =:UserInfo.getUserId()))];
      system.debug('Global Key Account:'+acc.Global_Key_Account__c);
      if (acc != null) {
    isError = false;
    return acc;
   } else {
    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, 'Only Global Key Account Manager,Regional Key Account Manager & Country Key Account Manger can add Team Members on Key Account Only.'));
    return null;
   }
  } catch (Exception e) {
      system.debug('e:'+e);
   isError = true;
   ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR, 'Only Global Key Account Manager,Regional Key Account Manager & Country Key Account Manger can add Team Members on Key Account Only.');
   ApexPages.addMessage(myMsg);
   return null;
  }
 }
 public PageReference save() {
  List < AccountTeamMember > atms = new List < AccountTeamMember > ();
  Set <Id> vAtms = new Set <Id> ();
  for (AccountWrapper wrap: wrappers) {
   if (wrap.at.UserId != null) {
       system.debug('wrapat:'+wrap);
    if (wrap.at.TeamMemberRole != null) {
     wrap.TeamMemberError = '';
     if (vAtms.contains(wrap.at.UserId) == false) {
      if (isValid == true)
       isValid = true;
      wrap.DuplicateError = '';
      vAtms.add(wrap.at.UserId);
      atms.add(wrap.at);
     } else {
      isValid = false;
      wrap.DuplicateError = 'Duplicate Values Not Allowed';
      wrap.TeamMemberError = '';
     }
    } else {
     isValid = false;
     wrap.TeamMemberError = 'Please Input a Value for Team Member';
     isValid1 = true;
     wrap.DuplicateError = '';
    }
   }
  }
  if (!atms.isEmpty() && atms != null && isValid == true) {
   insert atms;
  }
  if (isValid == true) {
   return new PageReference('/' + accId);
  } else
   return null;
 }
 public class AccountWrapper {
  public AccountTeamMember at {get;set;}
  public Integer ident {get;set;}
  public boolean isInsert {get;set;}
  public String TeamMemberError {get;set;}
  public String DuplicateError {get;set;}
  public AccountWrapper(Integer inIdent) {
   ident = inIdent;
   isInsert = true;
   at = new AccountTeamMember(AccountId = accId, AccountAccessLevel = 'Edit');
   TeamMemberError = '';
   DuplicateError = '';
  }
 }
}

and Below is the Test Class that I am trying to Cover:
 
@isTest public class createAccountTeamMemberTest {

@isTest static void testAccountWrapper(){
    Profile p=[Select Id from Profile where Name='Sales Rep'];
    Profile pManager=[Select Id from Profile where Name='Sales Manager'];
    User uManager=TestUtils.createUser('Jon','Favreau','jon.favreau@test.com',pManager.Id,true);
    User usr=TestUtils.createUser('Scarlet','Johnson','scarlet.johnson@test.com',p.Id,true);
    User u=[Select Id from User where Id=:usr.Id];
    User usr1=TestUtils.createUser('Robert','Downey','robert.downey@test.com',p.Id,true);
    TriggerFactory.bypassApex = true;
     Account act=TestUtils.CreateAccount('TestAcc','Customer',true);
    act.Global_Key_Account__c=true;
    act.Global_KAM__c =uManager.Id;
    update act;
      system.debug('Global Key Account:'+act.Global_Key_Account__c);
    system.debug('Manager ID:'+uManager.Id);
    Account ac=[Select Id,Name from Account where Id=:act.Id];
    system.debug('ac:'+ac);
    TriggerFactory.bypassApex = false;
     AccountTeamMember at=new AccountTeamMember(UserId=u.id,TeamMemberRole='Sales',AccountId=act.Id,AccountAccessLevel = 'Edit');
   // system.debug('AccountTeamMember'+at);
        insert at;
    system.debug('at'+at);
    System.RunAs(uManager){
    test.startTest(); 
PageReference pageRef =Page.Add_Account_Team_Member;

pageRef.getParameters().put('AccId', String.valueOf(act.Id));
Test.setCurrentPage(pageRef);
        system.debug('pageRef'+pageRef);
ApexPages.StandardController stdController = new ApexPages.standardController(at);
system.debug('stdController'+stdController);

        createAccountTeamMember.AccountWrapper aw=new createAccountTeamMember.AccountWrapper(5);
        createAccountTeamMember catm = new createAccountTeamMember(stdController);
        createAccountTeamMember.getAccount();
   catm.save();     

    test.stopTest();
    }

    }
}

but in my controller class,it is giving me Account as Null.Not sure why it is not giving me the account that i created in test class. Can someone help me in that?
Ravi Dutt SharmaRavi Dutt Sharma
You are expecting something which is completely opposite of how Salesforce works. Are you saying that you are creating an account record in your test class and that record in not accessible in your test class. Well, that will not happen. If you want the account record to be accessible in your apex class, create a record in your org by going to Accounts tab.

Thanks,
Ravi
Dhananjay Patil 12Dhananjay Patil 12
Well I meant to say,I created an Account Record in my test class,when test execution starts,it performs successful operation but does not cover the sufficient code coverages.When I put debug in controller class,I found that the account that i created in test class is not passing in the query of controller class.My concern is we put some condition in APex class and in test class we create our data in such a way,it automatically covers the code coverages in apex class.
In my case the account created in test class is always gives me null query result in controller class,not in apex class.
Ravi Dutt SharmaRavi Dutt Sharma
Are you able to compile the test class. I am not able to understand below line in your test class :
 
createAccountTeamMember.AccountWrapper aw=new createAccountTeamMember.AccountWrapper(5);

 
Dhananjay Patil 12Dhananjay Patil 12
Actually, I have created Wrapper class in controller class.The purpose of the wrapper class is to add number of rows on my VF page.In this case I am adding 5 rows on my VF page so that I can add 5 Account Team Members at a time.
Dhananjay Patil 12Dhananjay Patil 12
I try to user test.isRunningTest() in controller class inwhich I am creating an account manually for testing purpose and I have put the query  Account acc = [SELECT Id, Name,Global_Key_Account__c,Global_KAM__c FROM Account WHERE Id = : accId AND((Global_Key_Account__c = true AND Global_KAM__c =:UserInfo.getUserId()) OR(Regional_Key_Account__c = true AND Regional_KAM__c =:UserInfo.getUserId()) OR (Country_Key_Account__c = true AND Country_KAM__c =:UserInfo.getUserId()))]; system.debug('Global Key Account:'+acc.Global_Key_Account__c);  

in else block so that at least it does not considered it as null and it works somehow but I am not able to query the account in test.isRunningTest().I dont want to make it hardocded in test.isRunningTest() block.
Ravi Dutt SharmaRavi Dutt Sharma
act.Global_KAM__c =uManager.Id;

I think this line is creating problem. The test class is running by your user and you are setting some other user's id thie Global KAM field. Try using Test.runAs()
Dhananjay Patil 12Dhananjay Patil 12
Global KAM is the Key Account Manager field which specify if the current user is key account manager,it allows to add Account Team Member.If not  I am displaying an error message.so thats why in system.runas() i am addming uManager..