4

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<Contact> listContact){
    Insert listContact;
    Map<Id, Contact> contactMap = new Map<Id, Contact>(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<Relatie__c> rel= new List<Relatie__c>(); 
    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<Id> contactIds) {
    Profile profileId = [SELECT Id FROM Profile WHERE Name = 'Community' LIMIT 1];
    UserRole ur = [SELECT Id FROM UserRole Where name = 'Partner user'];
    List<User> uList= new List<User>();
    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<Contact> conts=new list<Contact>();
        for(Integer i=0;i<50;i++){
            contact con=new contact();
            con.LastName = 'Contact'+i;
            con.AccountId = acc.id;
            conts.add(con);
        }
        insert conts;  

        System.Test.startTest();
        Addcontroller obj = new Addcontroller();
        obj.saveContacts(conts);
        System.AssertNotEquals(Null,)
        System.test.stopTest();

    }
}
1
  • Separate thing, but move your assertion to after you stop your test, so any executed future methods can finish before you start asserting things. Feb 14, 2018 at 5:37

1 Answer 1

7

Your saveContacts method is marked as static.. to call that from the test class you don't need an object instance. you can directly call the method without the instance.

Addcontroller.saveContacts(conts);

Also, in your saveContacts method, you are inserting and updating the contacts which you can combine together. get the current users contact accountId and assign it to the contacts before the insert. no need of 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;
2
  • I had the relationship in the same loop but this causes a mixed dml error. If I call the method directly on what do I assert?
    – Thomas
    Feb 14, 2018 at 6:07
  • you need to query the records back and compare the data in your assert. Feb 14, 2018 at 6:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .