1

I have a trigger that runs when a lead is converted, and populates a field on the converted opportunity.

Now I'm trying to write a test class for it, but can't figure out how to access the value of the converted opportunity of the lead.

Can anyone point me in the right direction?

Here's my trigger, if it helps:

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
}

Edit: Here's the class I wrote so far:

    @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();
    }
}

Now my problem is that when I run the test, it says 0/0 Test Methods Passed. I don't know why!

Thanks!

  • Here's a hint for you. Once you create your data and insert it, you can always query for the new values after your trigger has run. If you need more help, post your code to show what you've done in your test class and you're more likely to get some kind of a response. Members like to see that someone has made an effort to solve their problem and isn't just asking for others to write code for them. – crmprogdev Dec 11 '14 at 19:16
  • check the SFDC Object doc salesforce.com/us/developer/docs/object_reference/index.htm on Lead - you'll find what you need – cropredy Dec 11 '14 at 20:16
  • when I try to run the test it says 0/0 test methods passed. What does that mean? Looks like the test isn't running! – user3515 Dec 12 '14 at 16:32
-2

I think you may need to use the SeeAllData=true along with @isTest like @isTest(SeeAllData=true). It helps to store the data during a test and also the existing data from the org. We had an issue similarly since the test data was getting lost during transit between triggers and classes. You can also check this link https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_annotation_isTest.htm

  • 1
    Using @SeeAllData=true is recommended to NEVER be used unless you absolutely must. It's always better to create the data you need in the test class. As well as being a suggestion that you should try to steer people away from it doesn't answer the question he was asking. – dphil Dec 11 '14 at 20:50
  • I added @Test(seeAllData = true), but when I try to run the test it says 0/0 test methods passed. What does that mean? Looks like the test isn't running! – user3515 Dec 11 '14 at 21:32
  • If it's not a good idea, I'll take it out! Either way my tests are not running. – user3515 Dec 11 '14 at 21:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.