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
DaGunsterDaGunster 

Important !

I have a trigger that works perfect in development. It's been done for about 1 week now.

I cannot get it deployed because I cannot build the test.

Hey, I've asked for help from this forum and no help - that worked
I've poured over every scrap of documentation.

I found an example and went back to it.
This time, I'm going to make a package out of your example
So here goes ...
This is all from your documentation.
First - the Trigger
Second - the Class.
Your documentation says this is what you need.
I made a trigger and compiled it - ok.
I made the class and compiled it - ok.
Went to make a package so I can get this into  P R O D U C T I ON   -   and FAILURE !!!!!!!!!!!!!!

From your PDF - The_World's_First_On-Demand_Programming_Language


The Trigger:

trigger blockDuplicates_tgr on Lead bulk(before insert, before update) {
/*
* begin by building a map which stores the (unique) list of leads
* being inserted/updated, using email address as the key.
*/
Map<String, Lead> leadMap = new Map<String, Lead>();
for (Lead lead : System.Trigger.new) {
if (lead.Email != null) { // skip null emails
/* for inserts OR
* updates where the email address is changing
* check to see if the email is a duplicate of another in
* this batch, if unique, add this lead to the leadMap
*/
if ( System.Trigger.isInsert ||
(System.Trigger.isUpdate &&
lead.Email != System.Trigger.oldMap.get(lead.Id).Email)) {

if (leadMap.containsKey(lead.Email)) {
lead.Email.addError('Another new lead has the same email address.');
} else {
leadMap.put(lead.Email, lead);
}
}
}
}

/* Using the lead map, make a single database query,
* find all the leads in the database that have the same email address as
* any of the leads being inserted/updated.
*/
for (Lead lead : [select Email from Lead where Email IN :leadMap.KeySet()]) {
Lead newLead = leadMap.get(lead.Email);
newLead.Email.addError('A lead with this email address already exists.');
}
}


 Now : The Test Class.

public class testBlockDuplicatesLeadTrigger {

static testMethod void testDuplicateTrigger(){

Lead[] l1 =new Lead[]{
new Lead( Email='homer@fox.tv', LastName='Simpson', Company='fox' )
};
insert l1; // add a known lead

Lead[] l2 =new Lead[]{
new Lead( Email='homer@fox.tv', LastName='Simpson', Company='fox' )
};
// try to add a matching lead
try { insert l2; } catch ( System.DmlException e) {
system.assert(e.getMessage().contains('first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, A lead with this email address already exists'),
e.getMessage());
}

// test duplicates in the same batch
Lead[] l3 =new Lead[]{
new Lead( Email='marge@fox.tv', LastName='Simpson', Company='fox' ),
new Lead( Email='marge@fox.tv', LastName='Simpson', Company='fox' )
};
try { insert l3; } catch ( System.DmlException e) {
system.assert(e.getMessage().contains('first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Another new lead has the same email'),
e.getMessage());

}

// test update also
Lead[] lup = new Lead[]{
new Lead( Email='marge@fox.tv', LastName='Simpson', Company='fox' )
};
insert lup;
Lead marge = [ select id,Email from lead where Email = 'marge@fox.tv' limit 1];
system.assert(marge!=null);
marge.Email = 'homer@fox.tv';

try { update marge; } catch ( System.DmlException e) {
system.assert(e.getMessage().contains('irst error: FIELD_CUSTOM_VALIDATION_EXCEPTION, A lead with this email address already exists'),
e.getMessage());
}
}


}

  AGAIN : the above compiled.
CREATE PACKAGE

With the two items above saving correctly, let's create a package.
Create a new unmanaged package and add the two items above.
Click 'upload'.

 oops

- - - Here is what it says

There are problems that prevent this package from being uploaded.

  
Item TypeNameProblem
Apex ClassLink: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, blockDuplicates: execution of BeforeInsert

caused by: System.NullPointerException: Attempt to de-reference a null object

Trigger.blockDuplicates: line 14, column 32
Class.testBlockDuplicatesLeadTrigger.testDuplicateTrigger: line 6, column 9




    HELLO - HOUSTON - WE HAVE A PROBLEM.  Your own example doesn't work !!!!!!!!!!!!!!!!!!!!!!

    I have spent the last 3-4 weekends on SalesForce. I've been working on this thing overtime about 3 nights a week.
    Without getting our trigger up into production - I WILL report to my management that Salesforce is a FAILURE.

    We are a LARGE account. My suggestion to executive level is
    (1) You write the test for free, or
    (2) You allow the trigger to work without a test, or
    (3) We cancel the Purchase order and look at other CRM products.

    We have called our account executive who put us in touch with a 'specialist'.

   The specialist said that you don't even use tests anymore !

    Something is going to give here.

    Can you at LEAST make your example work so that I have SOMETHING to go by.

    Time and Date: Saturday, April 19, 2008 at 4:30pm
  
WITHOUT A DEPLOYMENT OF THE ALREADY SUCCESSFULLY DEVELOPED TRIGGER - THIS DEPLOYMENT OF SALESFORCE IS A FAILURE.

Will you at least get your example to work?  What are we going to do.

Monday, I go to my managment and raise big alarms and declare a failure.
SuperfellSuperfell
Sorry you're not having much luck. I'm sure its not much of a consolation, but i was able to create the trigger & test you have listed below fine, created a package and installed it in another org. feel free to install it from this link if you want.
https://login.salesforce.com/?startURL=%2Fp%2Fmforced%2FMultiforceImportStageManager%3Fp0%3D04t300000002Wje

I suspect that the tests are failing because you have additional triggers on lead, all of the lead triggers will be executed by the tests, not just the ones you're trying to upload. (and when installed, all the lead triggers in the target org will be executed by the tests as well). If you look at the error message it says

"System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, blockDuplicates: execution of BeforeInsert"

note that that is says blockDuplicates, and not blockDuplicates_trg, as the trigger below is called, which is why i suspect you have additional triggers.


I saw a number of people give you advise on writing tests last week, perhaps you could describe your current problem with those tests? (do the tests pass, do you need more code coverage?, is it a packaging/installation problem, if so, what's the problem?)