Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Salesforce
  3. Question
Salesforce

Why nonstatic method cannot be referenced from a static context in testclass?

Asked by Sanjay Pandey Aug 8, 2021 1.2K views 1 answer
Share

About this question

I am working on a testclass but get the error that my method cant be referenced from a non static context. I have a class where I insert a list of contacts followed by a @future method. I get the error on : obj.saveContacts(conts);

Class

public without sharing class AddController { @AuraEnabled public static void saveContacts(List listContact){ Insert listContact; Map contactMap = new Map(listContact); createUser(contactMap.keySet()); system.debug('listContact '+ listContact); User currentUser = [SELECT Id, Contact.AccountId FROM User WHERE Id =: UserInfo.getUserId() LIMIT 1]; for(Contact con : listContact){ con.AccountId = currentUser.Contact.AccountId; } update listContact; system.debug('currentUser '+ currentUser); List rel= new List(); UserRole ur = [SELECT Id FROM UserRole Where name = 'Partner user']; for(Contact con : listContact){ Relatie__c rc = new Relatie__c(Account__c = currentUser.contact.AccountId, Contactpersoon__c = con.id ) ; system.debug('rc '+ rc); rel.add(rc); } insert rel; system.debug('rel '+ rel); } @future public static void createUser(Set contactIds) { Profile profileId = [SELECT Id FROM Profile WHERE Name = 'Community' LIMIT 1]; UserRole ur = [SELECT Id FROM UserRole Where name = 'Partner user']; List uList= new List(); Contact[] contactList = [SELECT id, firstname, lastname, email FROM Contact WHERE Id = :contactIds]; for(Contact con: contactList){ string alias = con.firstName.substring(0,1) + con.lastName.substring(0,1); user u = New user(userName = con.firstName+'.'+con.lastName+'@test.nl' , firstName = con.firstName, lastName = con.lastName, alias = alias, email = con.email, communityNickName = alias, timeZoneSidKey = 'Europe/Amsterdam', LocaleSidKey = 'nl_NL', EmailEncodingKey = 'ISO-8859-1', LanguageLocaleKey = 'nl_NL', ContactID = con.id, ProfileId = profileId.id//, //UserRoleId = ur.id ); uList.add(u); } insert uList; system.debug('uList '+uList); } }

Testclass

@isTest private class AddControllerTest{ @testSetup static void setupTestData(){ Profile pf= [Select Id from profile where Name='System Administrator']; String orgId=UserInfo.getOrganizationId(); String dateString=String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','') ; Integer RandomId=Integer.valueOf(Math.rint(Math.random()*1000000)); String uniqueName=orgId+dateString+RandomId; User u=new User(firstname = 'ABC', lastName = 'XYZ', email = uniqueName + '@test' + orgId + '.org', Username = uniqueName + '@test' + orgId + '.org', EmailEncodingKey = 'ISO-8859-1', Alias = uniqueName.substring(18, 23), TimeZoneSidKey = 'Europe/Amsterdam', LocaleSidKey = 'nl_NL', LanguageLocaleKey = 'nl_NL', ProfileId = pf.Id ); insert u; Account acc = new Account(Name='TestAccount'); insert acc; list conts=new list(); for(Integer i=0;i<50 con=new con.LastName = 'Contact' xss=removed xss=removed>

What is the reason for getting error “nonstatic method cannot be referenced from a static context” and how to resolve it.

Your answer

1 Answer

More Salesforce discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Salesforce Blogs

Guides, tips and career advice on Salesforce from JanBask experts.