+ Start a Discussion

Apex Trigger Coverage

Hi all
I'm having trouble with an Apex trigger that I've created. I used the leadDuplicatePreventer trigger in the Force.com cookbook and edited to involve checking a user alias is unique. When I edited this in my Sandbox environment it worked correctly and I had no problem. Now I'm trying to implement this into my Production environment via Eclipse and I seem to be running into trouble. 1st of all I get an error message that the test coverage is 0% (despite being working in Sandbox) and 2nd when I have tried to edit the trigger to active and I receive messages that the trigger is only saved locally, not to server. How come this happens? Why does it work on Sandbox and not on the Production environment?
The code ;)

trigger UserAliasDuplicatePreventer on User (before insert, before update)

{Map<String, User> userMap = new Map<String, User>();

for (User user : System.Trigger.new)

{if ((user.Alias != null) && (System.Trigger.isInsert || (user.Alias != System.Trigger.oldMap.get(User.Id).Alias)))

{if (userMap.containsKey(user.Alias))

{user.Alias.addError('Another new user has the same alias.');}

else {userMap.put(user.Alias, user);}}}

for (User user : [SELECT Alias FROM User WHERE Alias IN :userMap.KeySet()])

{User newUser = userMap.get(user.Alias);newUser.Alias.addError('A user already has this alias.');}}

I'm still quite new to Apex, but I do understand that at least 75% of your code has to be covered with test lines in order to move it from a sandbox or dev org into production...  check out the chapter on "Deploying Apex Scripts" in the developer guide.
i'm afraid i have no answer, but i can sympathize.  i've had problems deploying triggers to EE as well (DE is no problem).  Can't edit, can't activate, etc.  Also, eclipse tells me that coverage is 94%, then when i try to deploy to EE, it tells me that it's 15%.  Can anyone offer any insight here?
probably because your unit test cases are referencing specific hard coded ids that only exist in the org you developed them in.  then when you run them in the new org, it fails and the coverage is less.  take a more detailed look at the test logfile with logging level set higher and you should see some of the reasons it is not getting the same coverage...
Well at the moment I'm receiving the error INVALID_OPERATION: Cannot specify both runallTests specific RunTests. So I'm stuck with a working trigger in Sandbox and doing nothing in Production. Who said anythings simple :smileytongue:
heh, yeah - i've seen that error a number of times as well.  very frustrating.