You need to sign in to do that
Don't have an account?
Dhananjay 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:
and Below is the Test Class that I am trying to Cover:
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?
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?
Thanks,
Ravi
In my case the account created in test class is always gives me null query result in controller class,not in apex class.
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.
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()