1

Trying to write a trigger to set a lookup field (Account) on the Lead object, which is based on Account ID as a string that is passed through from an external form)... I have a working trigger (100% coverage), and what seems to be a working test class, but when I deploy in production, the validation fails due to 0% code coverage. No other exceptions. I know this should be simple! I also know it's a bad idea to pass a static ID in the test, but don't know how else to test this? If successful, will be using a custom Lead conversion tool to create new custom objects that are automatically associated with the Account ID with limited user input. Thanks in advance!!

Trigger Code:

trigger LeadSetAccount on Lead (before insert) {
// Initiate a For Loop
    for (Lead newLead : Trigger.new) {
// If a Lead has a value in the Account ID field, then execute
        if (newLead.Account_ID__c!=null) {
// Set Account ID based on the Account_ID string
    String AccountString = newLead.Account_ID__c;
    Id AccountID = AccountString;
    newLead.Account__c = AccountID;
        }
    }
}

Test Class:

@IsTest
public class TestLeadSetAccount {

//Sets method as testMethod, so any new data generated by this test will be removed after execution
    static testmethod void insertLead() {
//Name new Lead
         Lead newLead = new Lead();
// Set values for new Lead
         newLead.Company = 'Treadway';
         newLead.FirstName = 'Marty';
         newLead.LastName = 'Treadway';
         newLead.Status = 'Open';
         newLead.Account_ID__c = '001c000000gULKV';
// Insert new Lead
         insert newLead;
    }   
}
0

I don't understand why coverage is a problem here; I would have through that using an Account ID that doesn't exist would cause a test failure with some code coverage not 0% coverage. Double check that both the trigger and the test are being deployed.

It is common to insert the necessary objects in a test. In this case the test might be coded like this:

@isTest
public class TestLeadSetAccount {
    @isTest
    static void insertLead() {
         Account a = new Account(Name = 'Acme');
         insert a;
         Lead l = new Lead(
                 Company = 'Treadway',
                 FirstName = 'Marty',
                 LastName = 'Treadway',
                 Status = 'Open',
                 Account_ID__c = String.valueOf(a.Id)
                 );
         insert l;
         System.assertEquals(
                 a.Id,
                 [select Account__c from Lead where Id = :l.Id].Account__c
                 );
    }   
}

But wouldn't it be simpler to set the Account ID directly in the Lead.Account__c in the first place and avoid the need for the trigger?

  • Thanks for the reply on this - I did not deploy the test class in production, just the trigger. How do I deploy a test without the trigger first being deployed? (I know, total newb here!) I will give your suggestions a try! Also, I have tried to simply pass the Account_ID string value directly to the Lead.Account__c lookup field, but it is blank after each form submission. Thanks for your input! – Marty Treadway Nov 16 '14 at 21:27
  • 1
    @MartyTreadway You need to deploy them together (effectively in one update). – Keith C Nov 16 '14 at 21:32
  • Keith C, thank you so much! Your suggestions worked... Of course, I wasn't including my test class during validate, so it couldn't work. But also appreciate your suggestion to include creating the needed object in the test class. I owe you at least a 12-pack! – Marty Treadway Nov 16 '14 at 22:04

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.