• Sreejesh nair
  • NEWBIE
  • 0 Points
  • Member since 2010

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 3
    Replies
I need to delete some opportunities with past close dates, is there a way to do this all at once?
  • March 19, 2010
  • Like
  • 0

I have a trigger that fires when contact object is updated and updates the related user's isActive checkbox when the spesific checkbox in the contact has been changed.

 

Because the user and contact cannot be updated in the same transaction contact trigger calls the future method that updates the user's isActive field.  This is simplified trigger code.

 

 

trigger InsertUpdateContactTrigger on Contact (after update) {
List<String> contactsToUpdate = new List<String>();
for (Contact c : trigger.new){
if(trigger.newMap.get(c.Id).IsActive__c != c.IsActive__c){
contactsToUpdate.add(c.Id);
}
}

AsyncUserUpdateHelper.updateContactUserInfo(contactsToUpdate);
}

This works fine in practice in other word when I update the contact in UI trigger fires and user is updated. I have also written a testMethod for trigger and test passes when I run it in Eclipse IDE. I am also able to deploy the trigger in production through IDE and during deployment tests run successfully. The problem is that when I try to 'Run Test' or 'Run All Tests' in Setup->Develop->Apex Classes tests fail because of MIXED_DML_OPERATION error from the row 4.

1 private static testMethod void testUpdateContact(){
2 //QUERY CONTACTS THAT HAVE PORTAL USER
3 Test.startTest();
4 update portalContacts;
5 Test.stopTest();
6 //ASSERTIONS
7 }

Does using startTest and stopTest case the both trigger and future method to run in same transaction and if so how can I test the trigger that calls @future method? And why the tests run fine when deploying the through IDE but not when running them directly from salesforce?

  • January 27, 2010
  • Like
  • 0
I am having the following error in my unit tests. I was unable to find any information on this error in the documentation. Would like to know if possible whether this is a current limitation in Apex, that is not documented, or I am missing something.

This woks fine in pre-summer 08 orgs, but is failing in my Summer 08 enabled sandboxes.

Here is my code

Code:
static testMethod void InsertInvestmentAccountBulkTriggerBypass(){


User user = [Select Id From User Where Id =: UserInfo.getUserId()];
user.Bypass_Triggers__c = 'SCS_Investment_Account_Time_Series__c;Asset';

update user;

System.assert(GlobalSettings.bypassTriggers('Asset'));
System.assert(GlobalSettings.bypassTriggers('SCS_Investment_Account_Time_Series__c'));

Test.startTest();
Map<Id, Asset> IAMap = unitTests.createTestAsset(200);
System.assertEquals(200, IAMap.size());
Test.stopTest();
}




public static Map<Id, Asset> createTestAsset(Integer numberOfRecords){

Map<Id, Asset> IAMap = new Map<Id, Asset>();
Asset [] IAs = new List<Asset>();

Account client = unitTests.createTestAccount();
Product2 fund = [Select Id from Product2 LIMIT 1]; //TODO: extract out as a helper method

for (Integer i=0; i<numberOfRecords; i++){
Asset IA = new Asset();

IA.Name = 'unitTest_IA';
IA.AccountId = client.Id;
IA.Product2Id = fund.Id;

IAs.add(IA);
}


public static Account createTestAccount(){

Account a = new Account();
a.Name = 'Test Account';
a.Employee_Id__c = '123456';
insert a;

System.assertEquals(1, [Select count() from Account where Id = : a.Id]);

return a;
}

insert IAs;

for(Asset ia : IAs){
IAMap.put(ia.Id, ia);
}

return
}

Full error message:

System.DmlException: Insert failed. First exception on row 0; first error: MIXED_DML_OPERATION, DML operation on non-setup object is not supported after you have updated a setup object: Account

Stack trace:
Class.unitTests.createTestAccount: line 94, column 9
Class.unitTests.createTestAsset: line 146, column 26
Class.TimeSeriesUnitTests.InsertInvestmentAccountBulkTriggerBypass: line 27, column 32



Basically, I need to update a current user record, prior to testing triggers. Problem is that I am not allowed to update a user record and then execute DML statements on other objects.


Message Edited by AxxxV on 05-28-2008 02:29 PM
  • May 28, 2008
  • Like
  • 0