2010-03-05 12 views
30

बनाते हैं कभी-कभी सेल्सफोर्स परीक्षणों में आपको उपयोगकर्ता के ऑब्जेक्ट्स को एक विशिष्ट प्रकार के उपयोगकर्ता के रूप में परीक्षण के भाग को चलाने के लिए बनाने की आवश्यकता होती है। उस त्रुटि नहीं हैसेल्सफोर्स परीक्षणों में MIXED_DML_OPERATION त्रुटि से कैसे बचें जो उपयोगकर्ता

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

नोट:

हालांकि Salesforce ग्रीष्मकालीन 08 अद्यतन के बाद से, निम्न त्रुटि के दोनों उपयोगकर्ता वस्तुओं और (जैसे खातों के रूप में) सामान्य वस्तुओं एक ही परीक्षण नेतृत्व में बनाने का प्रयास करता तब होता है जब आप ग्रहण/Force.com आईडीई से परीक्षण चलाते हैं, लेकिन ऐसा तब होता है जब आप सेल्सफोर्स पर तैनाती करते हैं और फिर सेल्सफोर्स के भीतर से परीक्षण चलाते हैं।

इस त्रुटि से बचने के लिए मैं अपने परीक्षण दोबारा कैसे लिखूं?

यहाँ एक परीक्षण है कि त्रुटि का कारण बनता है का एक सरल उदाहरण है:

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 = '[email protected]'; 
     update a; 
    } 

} 

उत्तर

38

पर कई नहीं Salesforce लोग यहाँ अभी तक, मुझे लगता है।

मुझे एक समाधान मिला, मुझे नहीं पता कि यह क्यों काम करता है, लेकिन यह काम करता है। कि सामान्य वस्तुओं का उपयोग परीक्षण के

सभी भागों, एक System.runAs कि स्पष्ट रूप से वर्तमान उपयोगकर्ता का उपयोग करता है में लिपटे होने की जरूरत है इस तरह:

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

तो, उदाहरण के text_mixed_dmlbug विधि प्रश्न में दिए गए, बन जाएगा:

static testMethod void test_mixed_dmlbug() { 
    User u; 
    Account a;  
    User thisUser = [ select Id from User where Id = :UserInfo.getUserId() ]; 
    System.runAs (thisUser) { 
     Profile p = [select id from profile where name='(some profile)']; 
     UserRole r = [Select id from userrole where name='(some role)']; 
     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]'); 
     a = new Account(Firstname='Terry', Lastname='Testperson'); 
     insert a; 
    } 
    System.runAs(u) { 
     a.PersonEmail = '[email protected]'; 
     update a; 
    } 

} 

फिर MIXED_DML_OPERATION त्रुटियां बंद हो रही हैं।

+0

प्रतीक्षा, हम सब पर एक उपयोगकर्ता रिकॉर्ड सम्मिलित करने की आवश्यकता है? मुझे लगता है कि आप बस नए उपयोगकर्ता इंस्टेंस में प्रवेश कर सकते हैं और सीधे system.runAs() –

+0

में उपयोग कर सकते हैं, लेकिन अगर मुझे एक से अधिक/नए/उपयोगकर्ता की आवश्यकता है तो मैं क्या करूँ? मैं कई सिस्टम के साथ खेलने के लिए जा रहा हूं .runAs() यह देखने के लिए कि क्या मैं अपने नए उपयोगकर्ताओं के लिए उपयोगकर्ता आईडी को तैयार कर सकता हूं। – tggagne

+0

यह अभी भी काम करेगा - आपको उपयोगकर्ताओं को सम्मिलित करने की आवश्यकता नहीं है। साथ ही, system.runAs() का उपयोग क्या है यदि आप वर्तमान उपयोगकर्ता को चुनने जा रहे हैं? –

12

ऐसा लगता है कि आपको एक कामकाज मिला है। मैं सिर्फ यह कोशिश करना चाहता था कि आप यह त्रुटि क्यों प्राप्त कर रहे हैं।

मुझे लगता है कि आप (http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dml_non_mix_sobjects.htm प्रति) इस मामले की छानबीन चल रहे हैं:

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.

इसके अलावा, Summer 08 Release notes (उस लिंक एक पीडीएफ है) कहते हैं:

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

हां त्रुटि का कारण है, लेकिन यह एसएफ दस्तावेज से बहुत स्पष्ट नहीं है कि प्रतिबंध परीक्षण विधियों पर लागू होता है या नहीं। कुछ लोग ऐसा लगता है कि यह त्रुटि परीक्षणों के साथ मिलती है और कुछ नहीं करते हैं। डेवलपर फोरम पर इसके बारे में काफी चर्चा है, लेकिन इसमें से कोई भी स्पष्ट नहीं है इसलिए मैंने सोचा कि मैं यहां पूछूंगा ... धन्यवाद। – codeulike

+1

और हालांकि पाठ "महत्वपूर्ण यह प्राथमिक अपवाद है जब आप किसी परीक्षण में runAs विधि का उपयोग कर रहे हैं।" संदर्भित पृष्ठ पर अब नहीं है, यह कामकाज अभी भी काम करता है। – Legolas

6

यह व्यवहार वास्तव में प्रलेखित किया गया है बिक्री बल प्रलेखन में: http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_dml_non_mix_sobjects.htm?SearchType। पढ़ें जहां यह कहते हैं कि "महत्वपूर्ण इस के लिए प्राथमिक अपवाद नहीं है जब आप एक परीक्षण में RunAs विधि का उपयोग कर रहे हैं"

+0

यह त्रुटि अभी भी परीक्षण विधियों में होती है। – tponthieux

+0

लेकिन जब उपयोग नहीं करते testAs विधि परीक्षण विधियों के अंदर ठीक से। –

+0

इस दौरान सहायता में पाठ बदल गया है लेकिन अभी भी इस जवाब ने मेरे बेकन को बचाया है! स्पष्ट रूप से आप ठीक हैं अगर आप UserRoleId निर्दिष्ट नहीं करते हैं। और UserRoleId सेट करने वाले परीक्षण ग्रहण में खुशी से गुजरते हैं लेकिन वेब परीक्षण धावक में विफल ... thedailywtf.com, मैं यहां आ गया हूं ... – eyescream

0

बस दस्तावेज में यह पाया:

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.

तो ऐसा लगता है कि RunAs समाधान नहीं है कामकाज नहीं बल्कि बिक्री बल को मिश्रित डीएमएल मुद्दे से जाने का एकमात्र तरीका माना जाता है।

आशा इस मदद करता है

Reference

संबंधित मुद्दे