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 = '[email protected]',
  EmailEncodingKey = 'UTF-8',
  LastName = 'Test',
  LanguageLocaleKey = 'en_US',
  LocaleSidKey = 'en_US',
  TimeZoneSidKey='America/Los_Angeles',
  ProfileId = p.Id,
  UserName='[email protected]'
);

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 = '[email protected]',
      EmailEncodingKey = 'UTF-8',
      LastName = 'Test',
      LanguageLocaleKey = 'en_US',
      LocaleSidKey = 'en_US',
      TimeZoneSidKey='America/Los_Angeles',
      ProfileId = p.Id,
      UserName='[email protected]'
    );

    INSERT u;
  }

  @isTest static void testUpdate() {

  }

  @isTest static void testDelete() {

  }

  @isTest static void testUndelete() {

  }
}
5
  • can you share full code of test class ? Commented Nov 23, 2015 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
    Commented Nov 23, 2015 at 16:39
  • 3
    Do you per chance have an Apex Class whose name is User?
    – Adrian Larson
    Commented Nov 23, 2015 at 16:40
  • I will go with @AdrianLarson that may be you have class named User in your codebase Commented Nov 23, 2015 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
    Commented Nov 23, 2015 at 16:45

1 Answer 1

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
    Commented Nov 23, 2015 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
    Commented Nov 23, 2015 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
    Commented Nov 24, 2015 at 9:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .