2010-03-05 12 views
30

Bazen Salesforce testlerinde, belirli bir kullanıcı türü olarak sınamanın bir bölümünü çalıştırmak için Kullanıcı nesneleri oluşturmanız gerekir.Kullanıcıları oluşturan Salesforce testlerinde MIXED_DML_OPERATION hatası nasıl engellenir?

MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa): User, original object: Account

Not hata değildir yaptığı:

Ancak Salesforce Yaz 08 güncellemeden bu yana, aşağıdaki hata aynı test kurşun hem Kullanıcı nesneleri ve (örneğin Hesaplar gibi), normal nesneler yaratmak için çalışır Testleri Eclipse/Force.com IDE'den çalıştırdığınızda gerçekleşir, ancak Salesforce'a dağıttığınızda ve ardından testleri Salesforce'un içinden çalıştırdığınızda gerçekleşir.

Nasıl bu hatayı önlemek için benim testleri-yazma yeniden do? Sanırım, burada henüz üzerinde

static testMethod void test_mixed_dmlbug() {   
    Profile p = [select id from profile where name='(some profile)']; 
    UserRole r = [Select id from userrole where name='(some role)']; 
    User u = new User(alias = 'standt', email='[email protected]', 
      emailencodingkey='UTF-8', lastname='Testing', 
      languagelocalekey='en_US', 
      localesidkey='en_US', profileid = p.Id, userroleid = r.Id, 
      timezonesidkey='America/Los_Angeles', 
      username='[email protected]'); 
    Account a = new Account(Firstname='Terry', Lastname='Testperson'); 
    insert a; 

    System.runAs(u) { 
     a.PersonEmail = 'tes[email protected]'; 
     update a; 
    } 

} 

cevap

38

Pek Salesforce kişiler:

Burada hatayı neden olan bir testin basit bir örnek.

Bir çözüm buldum, neden çalıştığını bilmiyorum, ancak çalışıyor. Normal nesnelere erişim testin

Tüm parçalar böyle açıkça geçerli kullanıcı kullanan bir System.runAs sarılmış olması gerekir:

User thisUser = [ select Id from User where Id = :UserInfo.getUserId() ]; 
System.runAs (thisUser) { 
    // put test setup code in here 
} 

Yani, örnek text_mixed_dmlbug yöntemi söz konusu göz önüne alındığında, daha sonra MIXED_DML_OPERATION hatalarının gerçekleşmesini engeller.

+0

Bekle olur? Ben sadece yeni Kullanıcı örneğini başlatabilir ve doğrudan system.runAs() –

+0

'da kullanabilirsiniz. Fakat birden fazla/yeni kullanıcıya ihtiyacım varsa ne yapmalıyım? Yeni kullanıcılar için kullanıcı kimliklerini incelemek isteyip istemediğimi görmek için birden çok system.runAs() ile oynayacağım. – tggagne

+0

Hala işe yarayacak - kullanıcı eklemeniz gerekmez. Ayrıca, geçerli kullanıcı seçecekseniz system.runAs() kullanımı nedir? –

12

Bir geçici çözüm bulmuşsunuz gibi görünüyor. Sadece bu hatayı nereden aldığınızı anlamaya çalıştım. demek, Summer 08 Release notes (Bu bağlantının PDF olduğunu) Ayrıca

sObjects That Cannot Be Used Together in DML Operations

Some sObjects require that you perform DML operations on only one type per transaction. For example, you cannot insert an account, then insert a user or a group member in a single transaction. The following sObjects cannot be used together in a transaction:

* Group1 
* GroupMember 
* QueueSObject 
* User2 
* UserRole 
* UserTerritory 
* Territory 

Important The primary exception to this is when you are using the runAs method in a test.

:

In previous releases, in a single transaction that involved triggers, you could perform DML operations on more than one type of sObject, for example, you could insert an account, then insert a user. As of Summer '08, you can only perform DML operations on a single type of sObject from the following list of sObjects.

For example, you cannot insert an account, then insert a user, or update a group, then insert a group member.

  • Group
  • GroupMember
  • QueueSObject
  • User
  • UserRole
  • UserTerritory
  • Territory

In addition, User and Territory now support the insert and update DML operations, and UserRole now supports the insert, update delete and upsert DML operations.

Apex DML operations are not supported on the following sObjects:

  • AccountTerritoryAssignmentRule
  • AccountTerritoryAssignmentRuleItem
  • UserAccountTeamMember
+1

Evet, hatanın nedeni budur, fakat SF dokümantasyonundan, kısıtlamanın test yöntemlerine uygulanıp uygulanmadığı çok açık değildir. testler ile bu hatayı almak gibi görünüyor ve bazı yok.Proformansforce forumları hakkında bu konuda tartışma biraz var, ama hiçbiri çok açık bu yüzden ben burada sormak istedim ... Teşekkürler. – codeulike

+1

Ve her ne kadar text "Önemli Bir istisna bunun, runAs yöntemini bir sınamada kullandığınızdır." artık başvurulan sayfada değil, bu geçici çözüm hala çalışıyor. – Legolas

6

Bu davranış aslında belgelenmiştir

Sana (http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dml_non_mix_sobjects.htm başına) Bu sorunla çalışan düşünüyorum salesforce belgelerinde: http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_dml_non_mix_sobjects.htm?SearchType. bu sadece belgelerinde buldum

+0

Bu hata hala test yöntemlerinde gerçekleşiyor. – tponthieux

+0

runAs metodu test yöntemlerinin içinde uygun şekilde. –

+0

Bu sırada yardım metni değişti ama hala bu cevap pastırmamı bugün kurtardı! Görünüşe göre, UserRoleId belirtmezseniz, sorun değil. Ve UserRoleId ayarlamak testleri mutlu Eclipse geçmek ama web test koşucu başarısız ... thedailywtf.com, burada geliyorum ... – eyescream

0

"Bir testte RUNAS yöntemini kullanırken Önemli Bu birincil istisna değildir " demek nereye Oku: RunAs geçici çözüm olduğu gibi

Other Uses of runAs

You can also use the runAs method to perform mixed DML operations in your test by enclosing the DML operations within the runAs block. In this way, you bypass the mixed DML error that is otherwise returned when inserting or updating setup objects together with other sObjects . See sObjects That Cannot Be Used Together in DML Operations.

Yani görünüyor bir geçici çözüm değil ama karışık DML sorununa gitmenin tek yolu olarak Salesforce tarafından üstlenilir.

Umut bu biz hiç bir kullanıcı kaydı eklemek gerekiyor,

Reference