+ Start a Discussion
zen_njzen_nj 

how to deploy trigger code on User when DML operation is not allowed on User

Hi all

I have a conundrum here in that I am trying to deploy a trigger to production where when an User entry is created or updated, it will check to see if the Alias field is a duplicate. My trigger/class code works fine for when I am updating, but if I try to add in my test code (for deployment) a logic to insert a User entry, it will fail with message:
System.TypeExecption: DML not allowed on User.

So how exactly can I deploy the code then since if I don't test the insert logic, my code coverage total % would be only like 41%.

Just some clarification, I get the 41% code coverage when running it as a test in our sandbox apex class section.
But when I try to deploy it using the "ant deploy" method, I actually would get an error message:

Failures:
Code coverage issue, class: UserAliasDuplicatePreventerUpdate --- Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required.

The strange thing is that I had done this last year and I think when I deployed it along with the other existing triggers/classes, I was able to deploy it fine. But since then I think I have a newer ant version, as well as our sandbox and production instance have been upgraded. So instead of apiVersion 10.0, I think it is now 11.0. Not sure if that makes a difference or not.

In any case, here is my trigger code followed by my class code.
Any help would be greatly appreciated!

trigger UserAliasDuplicatePreventerUpdate on User (before insert, before update) {

// Trigger will only fire when the Alias has changed
for (Integer i = 0; i < Trigger.new.size(); i++) {

if(trigger.isInsert)
{
EnsureUniqueUserData.checkUniqueUserAlias(Trigger.new);
}
if (trigger.isUpdate)
{
if (Trigger.old[i].Alias != Trigger.new[i].Alias)
{
EnsureUniqueUserData.checkUniqueUserAlias(Trigger.new);
}
}
}
}

My class code is:

public class EnsureUniqueUserData
{

public static void checkUniqueUserAlias (User[] users)
{
for (User u:users)
{
Integer i = [ select count() from User where Alias = :u.Alias];
if (i > 0)
{
u.addError('An User with this Alias already exists!');
}
}
}


static testmethod void testUniqueUserAlias()
{
.
.
.
}