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
Harrison LinnHarrison Linn 

Help with an Apex test class

New to coding and would really appreciate any help that can be provided.

I am trying to write a test class for a function I have written that creates Opportunities for each account in a list.  On compile I am receiving the error 'Method does not exist or incorrect signture: void CreateOpptys(Id, List) from the type multiPicklistCtrl.  Can someone please tell me what I am doing wrong?

This is my test class:

static testmethod void CreateOpptysTest(){
        Id RecordTypeIdPropAccount = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Property').getRecordTypeId();
        
        Account pAcct = new Account(Name = 'Test Property account', RecordTypeId=RecordTypeIdPropAccount);
        insert pAcct;
        
        PropertySpace__c pSpace = new PropertySpace__c(Name='Test Property Space', Property__c = pAcct.Id, SquareFootage__c = 2000);
        insert pSpace;

        List<Account> accts = new List<Account>();
        for (Integer i=0; i<20; i++) {
            accts.add(new Account(Name = 'Test Account -' +i, RecordTypeId=RecordTypeIdPropAccount));
        }
        insert accts;
        
        accts = [Select Id from Account];
        
        Test.startTest();
          multiPicklistCtrl.CreateOpptys(pSpace.id,accts);
          Test.stopTest();
    }

Here is the function I am trying to write the test for:

@AuraEnabled
    public static void CreateOpptys(Id propertyId, List<Id> selectedAccts){
        system.debug('From Create Oppty propertyId1>>>'+propertyId);
        system.debug('From Create Oppty selectedAccts>>>'+selectedAccts);
        //Need to search through existing Opportunities and if there is an active Opty created for the Account and Property, do not create another Oppty.
        PropertySpace__c propSpace = new PropertySpace__c();
        Id propSpaceId;
        if(string.valueof(propertyId).startsWith('006')){
            opportunity opp= [SELECT PropertySpace__c FROM Opportunity WHERE Id = :propertyId];
               propSpace= getProp(opp.PropertySpace__c);
            propSpaceId = opp.PropertySpace__c;
        }
        else{
            propSpace = [SELECT Name,SquareFootage__c, Property__c FROM PropertySpace__c WHERE Id = :propertyId];
            propSpaceId = propertyId; 
        }
            
        Id recTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Vacancy Fill').getRecordTypeId();
        
         
        list<Opportunity> lstOpptys = new List<Opportunity>();
        for(integer i=0; i< selectedAccts.size(); i++){
            Opportunity o = new Opportunity(Name = propSpace.Name +i,
                                            CloseDate = date.today().addDays(90),
                                            StageName = 'Prospect',
                                            RecordTypeId = recTypeId,
                                            AccountId = selectedAccts[i]);

        lstOpptys.add(o);
        }    
        insert lstOpptys;
        
            
    }

 
Best Answer chosen by Harrison Linn
Dushyant SonwarDushyant Sonwar
Hi Harrison,

I forgot that your second argument is of list type, so set won't work

I have made some changes , this time i am using the list to fill the ids from account list.
List<Id> listOfAccountIDs = new List<Id> ();

for( Account accObj: accts){
 listOfAccountIDs.add(accObj.Id);
}
multiPicklistCtrl.CreateOpptys(pSpace.id,listOfAccountIDs);

This should work fine.

Please let me know if you still find any trouble.
Happy to help you :)

Thanks,

All Answers

Dushyant SonwarDushyant Sonwar
Hi Harrison,

Your method second argument is of list Id type and you are passing a list of sobject.
Replace this code
multiPicklistCtrl.CreateOpptys(pSpace.id,accts);
with below code
 
Set<Id> setOfAccountIDs = new Set<Id> ();

for( Account accObj: accts){
 setOfAccountIDs.add(accObj.Id);
}
multiPicklistCtrl.CreateOpptys(pSpace.id,setOfAccountIDs);

Hope this helps.
FARSANA PSFARSANA PS
Hi Harrison,
Try this,I hope this test class will give you 100% coverage.
static testmethod void CreateOpptysTest(){
        Id RecordTypeIdPropAccount = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Property').getRecordTypeId();
        
        Account pAcct = new Account(Name = 'Test Property account', RecordTypeId=RecordTypeIdPropAccount);
        insert pAcct;
        
        PropertySpace__c pSpace = new PropertySpace__c(Name='Test Property Space', Property__c = pAcct.Id, SquareFootage__c = 2000);
        insert pSpace;

        Opportunity testOpportunity = new Opportunity( StageName = 'Needs Analysis', PropertySpace__c=pSpace.id,CloseDate = TODAY()+15, Account = pAcct.id, Name = 'Test Opportunity');
        insert testOpportunity;

        List<Account> accts = new List<Account>();
        for (Integer i=0; i<20; i++) {
            accts.add(new Account(Name = 'Test Account -' +i, RecordTypeId=RecordTypeIdPropAccount));
        }
        insert accts;
        
       
        Map<id,Account> AccountMap=new Map<id,Account>([Select Id from Account]);
        Set <id> AccountIds = new Set<id>();
        AccountIds = AccountMap.keySet();
        
        Test.startTest();
          multiPicklistCtrl.CreateOpptys(pSpace.id,AccountIds);
          list<opportunity> opportunityList=[select id,name from opportunity where stage='Prospect'];
          System.assertNotEquals(0, opportunityList.size());
          multiPicklistCtrl.CreateOpptys(testOpportunity.id,AccountIds);
        Test.stopTest();

        Test.startTest();
          multiPicklistCtrl.CreateOpptys(testOpportunity.id,AccountIds);
          list<opportunity> opportunityList=[select id,name from opportunity where stage='Prospect'];
          System.assertNotEquals(0, opportunityList.size());
        Test.stopTest();
    }
hope this helps...
Harrison LinnHarrison Linn
Thank you for the suggestions, Dushyant I tried your changes and get an updated by similar messge:  'Method does not exist or incorrect signature: void CreateOpptys(Id, Set) from the type multiPicklistCtrl. Here is the updated code:

static testmethod void CreateOpptysTest(){
        Id RecordTypeIdPropAccount = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Property').getRecordTypeId();
        
        Account pAcct = new Account(Name = 'Test Property account', RecordTypeId=RecordTypeIdPropAccount);
        insert pAcct;
        
        PropertySpace__c pSpace = new PropertySpace__c(Name='Test Property Space', Property__c = pAcct.Id, SquareFootage__c = 2000);
        insert pSpace;

        List<Account> accts = new List<Account>();
        for (Integer i=0; i<20; i++) {
            accts.add(new Account(Name = 'Test Account -' +i, RecordTypeId=RecordTypeIdPropAccount));
        }
        insert accts;
        
        accts = [Select Id from Account];
        
        Test.startTest();
          Set<Id> setOfAccountIDs = new Set<Id> ();

        for( Account accObj: accts){
         setOfAccountIDs.add(accObj.Id);
        }
        multiPicklistCtrl.CreateOpptys(pSpace.id,setOfAccountIDs);
          Test.stopTest();
    }

Farsana I tried your code as well, and received several errors, with the following updates it too still give me the following errors : 28: Method does not exist or incorrect signature: void CreateOpptys(Id, Set) from the type multiPicklistCtrl, 31 Method does not exist or incorrect signature: void CreateOpptys(Id, Set) from the type multiPicklistCtrl, 35: Method does not exist or incorrect signature: void CreateOpptys(Id, Set) from the type multiPicklistCtrl . Here is the updated code:

@isTest
private class multiPicklistCtrlTest {
    
    static testmethod void CreateOpptysTest(){
        Id RecordTypeIdPropAccount = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Property').getRecordTypeId();
        
        Account pAcct = new Account(Name = 'Test Property account', RecordTypeId=RecordTypeIdPropAccount);
        insert pAcct;
        
        PropertySpace__c pSpace = new PropertySpace__c(Name='Test Property Space', Property__c = pAcct.Id, SquareFootage__c = 2000);
        insert pSpace;
        
        Opportunity testOpportunity = new Opportunity( StageName = 'Needs Analysis', PropertySpace__c=pSpace.id,CloseDate = date.today().addDays(15), AccountId = pAcct.id, Name = 'Test Opportunity');
        insert testOpportunity;

        List<Account> accts = new List<Account>();
        for (Integer i=0; i<20; i++) {
            accts.add(new Account(Name = 'Test Account -' +i, RecordTypeId=RecordTypeIdPropAccount));
        }
        insert accts;
        
        Map<id,Account> AccountMap=new Map<id,Account>([Select Id from Account]);
        Set <id> AccountIds = new Set<id>();
        AccountIds = AccountMap.keySet();
        list<opportunity> opportunityList = new list<opportunity>();
        
        Test.startTest();
          multiPicklistCtrl.CreateOpptys(pSpace.id,AccountIds);
          opportunityList=[select id,name from opportunity where StageName='Prospect'];
          System.assertNotEquals(0, opportunityList.size());
          multiPicklistCtrl.CreateOpptys(testOpportunity.id,AccountIds);
        Test.stopTest();

        Test.startTest();
          multiPicklistCtrl.CreateOpptys(testOpportunity.id,AccountIds);
          opportunityList=[select id,name from opportunity where StageName='Prospect'];
          System.assertNotEquals(0, opportunityList.size());
        Test.stopTest();
    }
Dushyant SonwarDushyant Sonwar
Hi Harrison,

I forgot that your second argument is of list type, so set won't work

I have made some changes , this time i am using the list to fill the ids from account list.
List<Id> listOfAccountIDs = new List<Id> ();

for( Account accObj: accts){
 listOfAccountIDs.add(accObj.Id);
}
multiPicklistCtrl.CreateOpptys(pSpace.id,listOfAccountIDs);

This should work fine.

Please let me know if you still find any trouble.
Happy to help you :)

Thanks,
This was selected as the best answer
Harrison LinnHarrison Linn
That fixed it, thank you so much!