You need to sign in to do that
Don't have an account?
Harrison 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;
}
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;
}
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.
This should work fine.
Please let me know if you still find any trouble.
Happy to help you :)
Thanks,
All Answers
Your method second argument is of list Id type and you are passing a list of sobject.
Replace this code with below code
Hope this helps.
Try this,I hope this test class will give you 100% coverage. hope this helps...
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();
}
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.
This should work fine.
Please let me know if you still find any trouble.
Happy to help you :)
Thanks,