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

537    Asked by sanjayPandey in Salesforce , Asked on Aug 8, 2021

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.

Answered by rakesh Jha

To avoid this error, you can directly call the method without the instance.

Addcontroller.saveContacts(conts);

Also, in your saved contacts method, you are inserting and updating the contacts which you can combine together. get the current user contact accountId and assign it to the contacts before the insert. no need for a separate update.

You can also move the Relationship creation into the same loop so you can avoid the second for loop.

  User currentUser = [SELECT Id, Contact.AccountId FROM User WHERE Id =: UserInfo.getUserId() LIMIT 1]; for(Contact con : listContact){ con.AccountId = currentUser.Contact.AccountId; Relatie__c rc = new Relatie__c(Account__c = currentUser.contact.AccountId, Contactpersoon__c = con.id ) ; rel.add(rc); } insert listContact; insert rel;


Your Answer

Interviews

Parent Categories