6

I'm wondering if anybody can help me out here please.

Problem:

I have updated a trigger (on Lead object) with some new code and I also updated its test class. Running the test class in my Sandbox is fine, all tests pass successfully. My problem arises when I try to validate/deploy this same trigger & test class to my production system. My test class for the trigger that I'm trying to deploy isn't failing, the failure is happening in 2 other test classes (which are associated with other before insert triggers on the Lead object), but all failures say the same thing:

Run Failures:

TestLeadCampaignAssignment.testBannerAdLead System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, DeDupeLead: execution of BeforeInsert caused by: System.ListException: List index out of bounds: 0

Trigger.DeDupeLead: line 131, column 1 Trigger.DeDupeLead: line 49, column 1: []

The line of code in my trigger that this seems to be referring to is the following:

List<User> myUser = [Select Id, User.Profile.Id, Name, IsActive 
                      from User Where Id=:dupeOwnerId];
System.debug('User Id is: ' + myUser[0].Id);

I have tried everything to get to the bottom of this but I can't seem to figure out what is happening.

I can deploy my trigger from one Sandbox to another without any issues, I have tried to populate my destination sandbox with some sample data and then deploy again but I can't seem to reproduce this error.

I have also tried changing the api version of my test class to 23.0 as recommended by another post and insert @isTest(seealldata) at the top of my test class.

Is there anybody out there that can help me to debug this please? I don't really know what else to do so if anybody has encountered similar problems then please feel free to advise me on what else to do/check as I seem to be running out of options.

6
  • Welcome to SFSE, Brian. How are you generating your test data? That is likely the problem.
    – Mike Chale
    Dec 19, 2013 at 14:44
  • Hi Mike, thanks for responding. I am generating my test data using a user that I'm setting up in my test class. I can post a sample of my test class if you like, but it seems to be this other before triggers' test classes the deployment failure is occurring in... Dec 19, 2013 at 15:54
  • Have you tried printing out your test user's id as well as dupeOwnerId? I suspect you'll see a mismatch. Maybe something like a workflow rule is reassigning the record?
    – Mike Chale
    Dec 19, 2013 at 15:58
  • I'm now printing out both Id's (in sandbox debug log) as advised Mike but because this failure occurs during deployment I can't see how to access the debug logs to see what id's get printed out. All that I'm given is the results and if I click into the 'Debug Logs' tab I can't make sense of them. Unless there's somewhere else I should be looking for them. Sorry if I'm being a bit silly here.. Thanks. Dec 19, 2013 at 16:11
  • Are you using a Change Set, IDE, or ANT? What if you run all tests in the sandbox? Also, salesforce.stackexchange.com/questions/5097/…
    – Mike Chale
    Dec 19, 2013 at 16:15

1 Answer 1

4

For your test class it is better if you create a user in your test class and then use the system.runAs(user); to run your test class as the newly created user.

this piece of code here Where Id=:dupeOwnerId is giving the issue probably because the id is not found in production or the id is different between production and sandbox.

you don't get the error when deploying between sandboxes because test code doesn't run when deploying from sandbox to sandbox

Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 
  User u = new User(Alias = 'standt', Email='[email protected]', 
  EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
  LocaleSidKey='en_US', ProfileId = p.Id, 
  TimeZoneSidKey='America/Los_Angeles', UserName='[email protected]');

  System.runAs(u) {
     // The following code runs as user 'u' 
     System.debug('Current User: ' + UserInfo.getUserName());
     System.debug('Current Profile: ' + UserInfo.getProfileId()); 
  }
2
  • I've been using this pattern for a long time and... now I'm trying to revert it! System.runAs indeed resolves some issues, like mixing setup and data DML. On the other hand it vastly increases execution time of unit tests, that in theory should be as quick as possible. I'm not stating that System.runAs is bad itself, however it should be used with care where it's really required.
    – endrju
    Dec 19, 2013 at 15:08
  • Hi Sven, thanks for your response. I have been setting up a user in my test class and running my tests as that user. I also tried setting up a test class user in the test class where the deployment failure is occurring but unfortunately this doesn't solve the problem. Dec 19, 2013 at 15:52

You must log in to answer this question.

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