5

I've created a Trigger on the User object and I'm trying to test creation, update, deletion and undeletion of a User.

Starting with Insert DML, I've got the following code:

Profile p = [SELECT Id FROM Profile WHERE Name = 'Recruitment Consultant'];

User u = new User(
  Alias = 'testu',
  Email = 'test@example.com',
  EmailEncodingKey = 'UTF-8',
  LastName = 'Test',
  LanguageLocaleKey = 'en_US',
  LocaleSidKey = 'en_US',
  TimeZoneSidKey='America/Los_Angeles',
  ProfileId = p.Id,
  UserName='test@example.com'
);

INSERT u;

Which should, in theory, check check the Trigger's Insert methods.

trigger UserTrigger on User (before insert, before update, before delete, after insert, after update, after delete, after undelete) {
  if (Trigger.isBefore) {
    if (Trigger.isInsert) {

    }
    // More stuff happens after this...

For some reason though, I'm getting the following error:

(UserTriggerTest) Invalid constructor syntax, name=value pairs can only be used for SObjects

I tried doing it in the following format instead:

User u = new User();

u.Alias = 'testu';
[...]

INSERT u;

But then I'm getting an error that the variable Alias doesn't exist.

I'm confused here because I can't see a problem with what I'm doing! Any help would be appreciated.

Thanks.

Edit I'll include the full class and test class here, it's literally a skeleton for future triggers, but you'll get the idea:

Trigger

trigger UserTrigger on User (before insert, before update, before delete, after insert, after update, after delete, after undelete) {
  if (Trigger.isBefore) {
    if (Trigger.isInsert) {

    }
    else if (Trigger.isUpdate) {

    }
    else if (Trigger.isDelete) {

    }
  }
  else {
    if (Trigger.isInsert) {

    }
    else if (Trigger.isUpdate) {

    }
    else if (Trigger.isDelete) {

    }
    else if (Trigger.isUndelete) {

    }
  }
}

Test Class

@isTest
private class UserTriggerTest {
  @isTest static void testInsert() {
    Profile p = [SELECT Id FROM Profile WHERE Name = 'Recruitment Consultant'];

    User u = new User(
      Alias = 'testu',
      Email = 'test@unitingambition.com',
      EmailEncodingKey = 'UTF-8',
      LastName = 'Test',
      LanguageLocaleKey = 'en_US',
      LocaleSidKey = 'en_US',
      TimeZoneSidKey='America/Los_Angeles',
      ProfileId = p.Id,
      UserName='test@unitingambition.com'
    );

    INSERT u;
  }

  @isTest static void testUpdate() {

  }

  @isTest static void testDelete() {

  }

  @isTest static void testUndelete() {

  }
}
5
  • can you share full code of test class ? Nov 23 '15 at 16:35
  • @MohithShrivastava More or less did do that, included that in my question. Ignore the empty methods for now, I just wanted to make a start on a simple insert!
    – Dan Jones
    Nov 23 '15 at 16:39
  • 3
    Do you per chance have an Apex Class whose name is User?
    – Adrian Larson
    Nov 23 '15 at 16:40
  • I will go with @AdrianLarson that may be you have class named User in your codebase Nov 23 '15 at 16:43
  • @AdrianLarson I actually do. Didn't think that would cause a problem! I deleted that and it compiled fine. Rookie error. If you put this as an answer I'd be delighted to give you the rep and mark it as answered!
    – Dan Jones
    Nov 23 '15 at 16:45
4

This seems to be most likely caused by a naming conflict. If you created an Apex Class as follows, you would no longer be able to construct User records as you are trying to do:

public class User
{
    public User()
    {
        // default constructor takes no parameters
        // there is no way to pass in name=value pairs via Apex
    }
}
3
  • Makes perfect sense, that's exactly what I had. I'll be more careful with naming conventions going forward! Thank you.
    – Dan Jones
    Nov 23 '15 at 16:48
  • 2
    @Poet You can also reference User via Schema.User if you do have a local name conflict. Granted, I'd recommend you avoid naming any class after a standard object/class, but you can always use that as a quick fix if the problem is localized.
    – sfdcfox
    Nov 23 '15 at 17:20
  • 1
    @sfdcfox Good to know, although I think I'd agree with you and will avoid using any naming conflicts.
    – Dan Jones
    Nov 24 '15 at 9:18

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.