You need to sign in to do that
Don't have an account?
Esti Leiser
apex test not running? (Says 0/0 test methods passed)
Hi! I wrote an apex test for a trigger I just wrote, and for some reason when I run the test it says "0/0 test methods passed".
I googled a bunch but couldn't find an answer, so I'm hoping you guys can help. I'm posting my trigger and class. The trigger acts when a lead is converted. What it does is populate a lookup field on the converted opportunity (contact__c) with the id of the converted contact.
Thanks in advance!
Trigger:
I googled a bunch but couldn't find an answer, so I'm hoping you guys can help. I'm posting my trigger and class. The trigger acts when a lead is converted. What it does is populate a lookup field on the converted opportunity (contact__c) with the id of the converted contact.
Thanks in advance!
Trigger:
trigger AddPrimaryContactToConvertedOpp on Lead (After Update) { // THIS TRIGGER WILL OVERWRITE ANY CONTACT DEFINED IN THE CUSTOM FIELD CONTACT__C ON THE OPPORTUNITY OBJECT. // SET THIS FIELD TO READ ONLY OR CHANGE THE FUNCTIONALITY BELOW TO AVIOD DATA BEING OVERWRITTEN BY MISTAKE... // [1] - Build map of converted Oppo Id to converted contactId map <Id,Id> oIdToCIdMap = new map <Id,Id>(); for (Lead l : Trigger.new) if (l.IsConverted && l.convertedOpportunityId != null) oIdToCIdMap.put(l.convertedOpportunityId,l.convertedContactId); // [2] Update the converted Oppos List<Opportunity> oUpdList = new List<Opportunity> (); for (ID oId : oIdToCIdMap.keySet()) oUpdList.add(new Opportunity(id = oId, contact__c = oIdToCIdMap.get(oId))); update oUpdList; // could be Database.update(oUpdList,false) if you want partial successes }Test class:
@isTest public class TestAddPrimaryCToConvertedOppTrigger { static void testInsertLead(){ Lead L1 = new Lead(LastName='last',FirstName='first',company='company'); insert L1; Database.LeadConvert lc = new Database.LeadConvert(); lc.setLeadId(L1.id); test.startTest(); Database.LeadConvertResult lcr = Database.convertLead(lc); Contact C1 = [SELECT Id FROM Contact WHERE Id = :L1.ConvertedContactId]; Opportunity O1 = [SELECT Id FROM Opportunity WHERE Id = :L1.ConvertedOpportunityId]; system.AssertEquals(O1.Contact__c,C1.ID); test.stopTest(); } }
static void testInsertLead(){
change this line to
Now the test ran, and failed :). The error message is:
System.DmlException: ConvertLead failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, convertedStatus is required: [Status]
Any ideas? Thanks!!
udpate <leadStatus> to one value from status picklist on lead
Updated apex: The error is on line 18 (starts with CONTACT C1) - it says "System.QueryException: List has no rows for assignment to SObject"
Any ideas? Thanks!
@isTest(SeeAllData=true) for your class
Looks like you dont have any picklist values is set as converted in Lead Object Status picklist field.
go to Customize -> Lead -> Fields -> Status field.
choose any picklist value and make sure atleast one of them have converted checkbox is checked.
Are you referring to the "Lead Status" field? The Closed - Converted picklist choice has the converted checkbox checked.
Also, the error is not on the line where it chooses the converted status, but on a later line -
System.debug('Success : ' + lcr.isSuccess() );
for (Database.Error err : lcr.getErrors()) {
System.debug('Error: ' + err.getMessage() );
}
the log you have given does not show any thing related to the system.debug i asked you to put.
So try using this
you have to use lcr
Now what?
Opportunity O1 = [SELECT Contact__c FROM Opportunity WHERE Id = :lcr.getOpportunityId()];
Just made that change, and........
IT PASSED!!!
Thank you SO much for your help and your neverending patience!
Have a great day...